CMDSTAN not being set when running cmdstanpy on jupyter notebook in VS code

Hi all. I am experiencing some issues with using notebook to compile my stan models. It worked fine up until a minute ago and suddenly just broke (I did not install nor update any the packages.extensions). When running the code in the terminal and manually activating my conda env the model compiles just fine.

The error message I get is:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 19
     16 with open('m.stan', 'w') as f:
     17     f.write(model_code)
---> 19 model = cmdstanpy.CmdStanModel(stan_file='m.stan')

File ~/anaconda3/envs/cmdstan_condaforge/lib/python3.12/site-packages/cmdstanpy/model.py:215, in CmdStanModel.__init__(self, model_name, stan_file, exe_file, force_compile, stanc_options, cpp_options, user_header, compile)
    211 if not cmdstan_version_before(
    212     2, 27
    213 ):  # unknown end of version range
    214     try:
--> 215         model_info = self.src_info()
    216         if 'parameters' in model_info:
    217             self._fixed_param |= len(model_info['parameters']) == 0

File ~/anaconda3/envs/cmdstan_condaforge/lib/python3.12/site-packages/cmdstanpy/model.py:321, in CmdStanModel.src_info(self)
    319 if self.stan_file is None or cmdstan_version_before(2, 27):
    320     return {}
--> 321 return compilation.src_info(str(self.stan_file), self._compiler_options)

File ~/anaconda3/envs/cmdstan_condaforge/lib/python3.12/site-packages/cmdstanpy/compilation.py:348, in src_info(stan_file, compiler_options)
    338 def src_info(
    339     stan_file: str, compiler_options: CompilerOptions
    340 ) -> Dict[str, Any]:
    341     """
    342     Get source info for Stan program file.
    343 
    344     This function is used in the implementation of
    345     :meth:`CmdStanModel.src_info`, and should not be called directly.
    346     """
    347     cmd = (
--> 348         [os.path.join(cmdstan_path(), 'bin', 'stanc' + EXTENSION)]
    349         # handle include-paths, allow-undefined etc
    350         + compiler_options.compose_stanc(None)
    351         + ['--info', str(stan_file)]
    352     )
    353     proc = subprocess.run(cmd, capture_output=True, text=True, check=False)
    354     if proc.returncode:

File ~/anaconda3/envs/cmdstan_condaforge/lib/python3.12/site-packages/cmdstanpy/utils/cmdstan.py:170, in cmdstan_path()
    168 cmdstan_dir = os.path.expanduser(os.path.join('~', _DOT_CMDSTAN))
    169 if not os.path.exists(cmdstan_dir):
--> 170     raise ValueError(
    171         'No CmdStan installation found, run command "install_cmdstan"'
    172         'or (re)activate your conda environment!'
    173     )
    174 latest_cmdstan = get_latest_cmdstan(cmdstan_dir)
    175 if latest_cmdstan is None:

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

OS: Ubuntu 22.04.4 LTS
Jupyter Extension: v2024.3.0
Cmdstan: 2.34.0
Cmdstanpy: 1.2.1

I installed cmdstan through the condaforge channel, thus when activating the conda env, the CMDSTAN variable should be set. Running os.environ['CMDSTAN'] in the terminal gives me the installed cmdstan directory '/home/garren/anaconda3/envs/cmdstan_condaforge/bin/cmdstan' but when running it in the jupyter kernel gives a key error, indicating that this is not being set upon activating the conda env. It seems like manually adding os.environ['CMDSTAN']='/home/garren/anaconda3/envs/cmdstan_condaforge/bin/cmdstan' in my jupyter kernel seems to be a workaround for now.

I did not change anything manually and also tried uninstalling and reinstalling jupyter notebook and vs code, but to no avail. Any help on this would be much appreciated.

Are you launching a jupyter lab instance from the activated environment or just selecting the “kernel” for that environment? The latter does not actually activate the environment and therefore does not set environment variables: Proper activation of conda env when working with kernels · Issue #12544 · jupyterlab/jupyterlab · GitHub

I just select the kernel for that environment and this has worked perfectly fine up until today, but I will try this. Thanks

Update:

After updating my jupyter extension in vs code, the old behavior has return where the conda variables are set upon selecting the kernel for that environment. I can only assume that there may have been a bug with a previous update the jupyter extension?

1 Like