Fresh Conda installation of cmdstanpy does not work with bernoulli.stan example

Fresh Conda installation of cmdstanpy does not work with bernoulli.stan example.

Have followed the installation instructions to create a new conda environment install of cmdstanpy (Conda 23.5.2, cmdstan 2.33.1, cmdstanpy 1.2.0, python 3.12.1), as per Installation — CmdStanPy 1.2.0 documentation

Have then tried to run the bernoulli.stan example from a python terminal, as per “Hello, World!” — CmdStanPy 1.2.0 documentation.

First encountered an error due to differences between the (default) installation path Conda placed cmdstanpy into within the new environment ‘stanpy’, which was \stanpy\Library\bin, rather than the \stanpy\bin\ path indicated in the documentation, and the location of bernoulli.stan was found in \cmdstan\examples\bernoulli, rather than the \cmdstan\users-guide\examples\ path indicated in the documentation (initially, these path discrepancies raised a ‘no such file’ ValueError when trying to run the example).

After setting the ‘stan_file’ variable used in the example to the correct path, the bernoulli.stan file was clearly found (received ‘INFO - compiling stan file’ message), but then received the following error output:

17:47:27 - cmdstanpy - INFO - compiling stan file C:\ProgramData\Miniconda3\envs\stanpy\Library\bin\cmdstan\examples\bernoulli\bernoulli.stan to exe file C:\ProgramData\Miniconda3\envs\stanpy\Library\bin\cmdstan\examples\bernoulli\bernoulli.exe
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\ProgramData\Miniconda3\envs\stanpy\Lib\site-packages\cmdstanpy\model.py", line 252, in __init__
    self.compile(force=str(compile).lower() == 'force', _internal=True)
  File "C:\ProgramData\Miniconda3\envs\stanpy\Lib\site-packages\cmdstanpy\model.py", line 502, in compile
    self._exe_file = compilation.compile_stan_file(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\Miniconda3\envs\stanpy\Lib\site-packages\cmdstanpy\compilation.py", line 475, in compile_stan_file
    raise ValueError(
ValueError: Failed to compile Stan model 'C:\ProgramData\Miniconda3\envs\stanpy\Library\bin\cmdstan\examples\bernoulli\bernoulli.stan'. Console:
g++ -std=c++1y -m64 -D_REENTRANT -Wall -Wno-unused-function -Wno-uninitialized -Wno-unused-but-set-variable -Wno-unused-variable -Wno-sign-compare -Wno-unused-local-typedefs -Wno-int-in-bool-context -Wno-attributes -Wno-ignored-attributes      -I stan/lib/stan_math/lib/tbb_2020.3/include    -O3 -I src -I stan/src -I stan/lib/rapidjson_1.1.0/ -I lib/CLI11-1.9.1/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.4.0 -I stan/lib/stan_math/lib/boost_1.78.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials  -D_USE_MATH_DEFINES  -DBOOST_DISABLE_ASSERTS          -c -MT src/cmdstan/main.o -M -E -MG -MP -MF src/cmdstan/main.d src/cmdstan/main.cpp
mkdir: cannot create directory 'stan': No such file or directory
stan/lib/stan_math/make/libraries:63: recipe for target 'stan/lib/stan_math/lib/sundials_6.1.1/src/sundials/sundials_math.o' failed
mingw32-make: *** [stan/lib/stan_math/lib/sundials_6.1.1/src/sundials/sundials_math.o] Error 1
Command ['mingw32-make', 'STANCFLAGS+=--filename-in-msg=bernoulli.stan', 'C:/ProgramData/Miniconda3/envs/stanpy/Library/bin/cmdstan/examples/bernoulli/bernoulli.exe']
        error during processing No such file or directory

Then checked the path variable using cmdstan_path() and this is set ‘correctly’, in the sense that the path is set to the cmdstan installation selected by Conda as noted above.

This is an absolutely fresh, untouched install into a virtual env without any other packages in it.

Operating System: Windows 11 Pro Build 22631.2861
Interface Version: cmdstanpy 1.2.0 / cmdstan 2.33.1

You are getting that error because the program cannot create a folder in C:\ProgramData. I don’t know why Miniconda selected that folder to be installed, but you have to uninstall it and then reinstall it again in an appropriate folder. Usually the folder is inside $Env:USERPROFILE, you can see here the meaning of that command, open Powershell (or Terminal since you are in Windows 11) and type that command.

But again, Miniconda usually selects that folder during the installation, in my case C:\Users\rosgori\anaconda3.

If you are curious, try to create a folder inside C:\ProgramData using Python, you will get PermissionError.

Thanks, reinstalling to a user location does seem to have got over the hurdle in this case. Conda installed to ProgramData because the option to install system-wide was selected (over user-only) during the initial install. My user profile has admin privileges.

Unless you were running Python in an elevated shell (probably not a good idea for a variety of reasons), your user’s admin permissions wouldn’t have been inhereted. The current CmdStan build system requires being able to write to the folder it is in, which is definitely less than ideal in some circumstances

Thanks @WardBrian, I was indeed running in an elevated shell, so I am surprised it was a permissions issue.