stan-dev / cmdstanpy

CmdStanPy is a lightweight interface to Stan for Python users which provides the necessary objects and functions to compile a Stan program and fit the model to data using CmdStan.
BSD 3-Clause "New" or "Revised" License
149 stars 67 forks source link

Conda install: "No CmdStan installation found" Win 11 WSL ARM64 #765

Closed atownse2 closed 2 weeks ago

atownse2 commented 2 weeks ago

Summary:

I am trying to setup my Conda environment (that contains cmdstanpy) on my Windows 11 machine (ARM64) in WSL (Ubuntu 22.04.4). Not sure if the architecture is the issue but this setup works on my Windows 10 machine (x86).

Description:

I install cmdstanpy with Conda, specifying the conda-forge channel. I can import cmdstanpy however whenever I try to initialize a CMDStanModel I get:

ValueError: No CmdStan installation found, run command "install_cmdstan"or (re)activate your conda environment!

I have tried reactivating my environment and shell, restarting the python kernel and several permutations of these things. When I try to run install_cmdstan I get:

Traceback (most recent call last):
  File "/home/atownse2/micromamba/envs/triphoton-env/bin/install_cmdstan", line 6, in <module>
    from cmdstanpy.install_cmdstan import main
ImportError: cannot import name 'main' from 'cmdstanpy.install_cmdstan' (/home/atownse2/micromamba/envs/triphoton-env/lib/python3.12/site-packages/cmdstanpy/install_cmdstan.py)

Additional Information:

Conda env file looks something like:

name: my-env
channels:
  - conda-forge
dependencies:
  ...
  - cmdstanpy
  ...

Current Version:

Please include the output of import cmdstanpy; cmdstanpy.show_versions(), or at least the cmdstan and cmdstanpy versions used.

>>> import cmdstanpy; cmdstanpy.show_versions()
INSTALLED VERSIONS
---------------------
python: 3.12.4 | packaged by conda-forge | (main, Jun 17 2024, 10:02:13) [GCC 12.3.0]
python-bits: 64
OS: Linux
OS-release: 5.15.153.1-microsoft-standard-WSL2
machine: aarch64
processor: aarch64
byteorder: little
LC_ALL: None
LANG: C.UTF-8
LOCALE: ('C', 'UTF-8')
cmdstan_folder: /home/atownse2/micromamba/envs/triphoton-env/bin/cmdstan
cmdstan: (2, 35)
cmdstanpy: 1.2.4
pandas: 2.2.2
xarray: None
tqdm: 4.66.4
numpy: 2.0.0
WardBrian commented 2 weeks ago

Can you share the result of printenv | grep CMDSTAN from inside your conda environment?

Oddly, the print_versions script seems to locate your installation just fine. I'm not sure how many people have tried the exact combination of WSL/conda, let alone WSL/aarch64/conda

atownse2 commented 2 weeks ago

Sure!

(triphoton-env) atownse2@Surface:~/Code/RSTriPhoton$ printenv | grep CMDSTAN
CMDSTAN_OLD=
CMDSTAN=/home/atownse2/micromamba/envs/triphoton-env/bin/cmdstan
WardBrian commented 2 weeks ago

Yeah, sure looks correct to me.

In Python, can you run the following?

import os
print('CMDSTAN' in os.environ)
print(os.environ.get('CMDSTAN'))
atownse2 commented 2 weeks ago
>>> import os
>>> print('CMDSTAN' in os.environ)
True
>>> print(os.environ.get('CMDSTAN'))
/home/atownse2/micromamba/envs/triphoton-env/bin/cmdstan
>>> 
WardBrian commented 2 weeks ago

Hm, can you try restarting your shell, re-activating the environment, and trying again? I can't spot anything wrong with your config

atownse2 commented 2 weeks ago

This is weird, the CmdStanModel initialization works in the Python shell but not in the Jupyter notebook. The error in the Jupyter notebook also gives this message:

06:18:41 - cmdstanpy - INFO - No CmdStan installation found.
06:18:41 - cmdstanpy - INFO - Cannot determine whether version is before 2.27.
06:18:41 - cmdstanpy - INFO - No CmdStan installation found.
06:18:41 - cmdstanpy - INFO - Cannot determine whether version is before 2.27.
WardBrian commented 2 weeks ago

Hm - are you sure you're using the kernel you think you are inside of Jupyter? Especially when mixing with conda environments this can be tricky

atownse2 commented 2 weeks ago

Not sure - if I compare

import sys
print(sys.executable)

In the notebook I get:

/home/atownse2/micromamba/envs/triphoton-env/bin/python

and in the shell I get:

/home/atownse2/micromamba/envs/triphoton-env/bin/python

Also all the other Conda packages I installed are available in both the shell and the notebook.

WardBrian commented 2 weeks ago

Did you launch the jupyter instance from an activated environment? I think it inherits the environment variables of the shell

atownse2 commented 2 weeks ago

So I'm using VSCode's Jupyter extension to run my notebooks. If I instead launch a notebook from the command line (inside the conda environment) everything works. Not sure why this particular package is having issues while the others are working. We can close this issue if you want as it's looking like this is a VSCode/Jupter issue and not a cmdstanpy issue. That being said, if you have any suggestions for fixes that would be very helpful. Thanks for your help so far!

WardBrian commented 2 weeks ago

Oh hmm, i assume you’re using the VSCode: Remote WSL extension to do so? I have no idea how that interacts with environment variables and Jupyter.

One thing you might want to try is - from a WSL terminal with the environment active - running code /path/to/your/workspace and see if launching it in that manner makes a difference?

atownse2 commented 2 weeks ago

That gives the same result.

I use this same setup (Conda/WSL/VSCode) on my other Win 10 (x86) machine and it works fine.

This might be related to: https://discourse.mc-stan.org/t/cmdstan-not-being-set-when-running-cmdstanpy-on-jupyter-notebook-in-vs-code/34740/2 the workaround of manually setting the CMDSTAN path mentioned by OP fixes the issue, however their solution of updating VSCode's Jupyter extension does not (neither does rolling back the extension version).

Either way, I can move forward with this workaround so I will close the issue, thanks again for your help.

atownse2 commented 2 weeks ago

Okay nevermind, there still seems to be issues which I assume are related to the C++ toolchain paths not being set. Either way this doesn't seem to be a cmdstanpy issue so I will leave the issue closed.