DLL issue using CmdStanPy on Win10 with Jupyter: `ImportError: DLL load failed while importing _ctypes`

I’ve set up CmdStanPy on Windows 10 using Anaconda (in a dedicated env). C++ toolchain is working, and I can execute the bernoulli example via cmdline.

I’d like to set up jupyter in this env to conveniently interface with my Stan code. Installing jupyter is no issue, but when I run jupyter notebook I get the following error message:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\envs\cmdstanpy\Scripts\jupyter-notebook-script.py", line 6, in <module>
    from notebook.notebookapp import main
  File "C:\ProgramData\Anaconda3\envs\cmdstanpy\lib\site-packages\notebook\notebookapp.py", line 75, in <module>
    from .base.handlers import Template404, RedirectWithParams
  File "C:\ProgramData\Anaconda3\envs\cmdstanpy\lib\site-packages\notebook\base\handlers.py", line 35, in <module>
    from notebook.utils import is_hidden, url_path_join, url_is_absolute, url_escape, urldecode_unix_socket_path
  File "C:\ProgramData\Anaconda3\envs\cmdstanpy\lib\site-packages\notebook\utils.py", line 8, in <module>
    import ctypes
  File "C:\ProgramData\Anaconda3\envs\cmdstanpy\lib\ctypes\__init__.py", line 8, in <module>
    from _ctypes import Union, Structure, Array
ImportError: DLL load failed while importing _ctypes: The specified module could not be found.

Pretty sure there is a conflict between cmdstanpy and jupyter on the _ctypes DLL. Does anyone have a clue on how to resolve this?

Huge thanks in advance!

(I’m running cmdstan 2.29.2, cmdstanpy 1.0.1, RTools40 as C++ toolchain, on Win10 with conda 4.11.0)

Found a way to resolve the issue. Haven’t identified the root cause fully but pretty sure there was some version conflict between libraries in my env.

For anyone running into the same issue, here are my steps to installing a functioning instance of cmdstan + cmdstanpy + jupyter on Win10:

  • Set up env: conda create -n cmdstan python=3.8
  • Activate env: conda activate cmdstan
  • Install requirements (see below): pip install -r requirements.txt
  • Install cmdstan: conda install -c conda-forge cmdstan=2.29.2
  • If you haven’t already, set up builder …
  • Download RTools: python -m cmdstanpy.install_cxx_toolchain
  • Set paths (probably required): SET PATH=C:\rtools40\bin;C:\rtools40\mingw_64\bin;%PATH% and SET MAKE=mingw32-make.exe
  • Test your installation in python prompt by running the bernoulli example:
from cmdstanpy import cmdstan_path, CmdStanModel
stan_file = os.path.join(cmdstan_path(), 'examples', 'bernoulli', 'bernoulli.stan')
model = CmdStanModel(stan_file=stan_file)
data_file = os.path.join(cmdstan_path(), 'examples', 'bernoulli', 'bernoulli.data.json')
fit = model.sample(data=data_file)  

That should be it. If at some point cmdstan cannot be found try running from the python prompt: import cmdstanpy, then cmdstanpy.install_cmdstan()

These are the requirements.txt I used (not the minimal set, but all of these will come in handy):



If your setup works for you then it is probably not worth fussing with more, but I just want to note that if you are using conda to install cmdstan there is no need to install RTools or use the install_cmdstan command

Got it, thanks @WardBrian for following up on this!