Can't compile models with cmdstanpy in ipynb / vscode

I am suddenly unable to compile models using cmdstanpy in ipynb files. I usually use VS Code to work with them.

As a simple example:

import cmdstanpy
model = cmdstanpy.CmdStanModel(stan_file="model.stan")

This script will run fine if I call it from the terminal. However, as soon as I run the same code within a cell of an ipynb in VS Code, I get errors like:

ValueError                                Traceback (most recent call last)
File path/test.py:2
      1 import cmdstanpy
----> 2 model = cmdstanpy.CmdStanModel(stan_file="model.stan")

File ~/miniconda3/envs/cs23v2/lib/python3.10/site-packages/cmdstanpy/model.py:257, in CmdStanModel.__init__(self, model_name, stan_file, exe_file, force_compile, stanc_options, cpp_options, user_header, compile)
    254         get_logger().debug("TBB already found in load path")
    256 if compile and self._exe_file is None:
--> 257     self.compile(force=str(compile).lower() == 'force', _internal=True)

File ~/miniconda3/envs/cs23v2/lib/python3.10/site-packages/cmdstanpy/model.py:465, in CmdStanModel.compile(self, force, stanc_options, cpp_options, user_header, override_options, _internal)
    462         else:
    463             self._compiler_options.add(compiler_options)
--> 465 self._exe_file = compilation.compile_stan_file(
    466     str(self.stan_file),
    467     force=force,
    468     stanc_options=self._compiler_options.stanc_options,
    469     cpp_options=self._compiler_options.cpp_options,
    470     user_header=self._compiler_options.user_header,
    471 )

File ~/miniconda3/envs/cs23v2/lib/python3.10/site-packages/cmdstanpy/compilation.py:481, in compile_stan_file(src, force, stanc_options, cpp_options, user_header)
    474     if 'PCH' in console or 'precompiled header' in console:
    475         get_logger().warning(
    476             "CmdStan's precompiled header (PCH) files "
    477             "may need to be rebuilt."
    478             "Please run cmdstanpy.rebuild_cmdstan().\n"
    479             "If the issue persists please open a bug report"
    480         )
--> 481     raise ValueError(
    482         f"Failed to compile Stan model '{src}'. " f"Console:\n{console}"
    483     )
    484 return str(exe_target)

ValueError: Failed to compile Stan model 'path/model.stan'. Console:
clang++ -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -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.81.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials    -DBOOST_DISABLE_ASSERTS          -c -MT stan/src/stan/model/model_header_14_0.hpp.gch -MT stan/src/stan/model/model_header_14_0.d -MM -E -MG -MP -MF stan/src/stan/model/model_header_14_0.d stan/src/stan/model/model_header.hpp
clang++ -std=c++1y -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT -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.81.0 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials    -DBOOST_DISABLE_ASSERTS          -c -MT src/cmdstan/main.o -MM -E -MG -MP -MF src/cmdstan/main.d src/cmdstan/main.cpp
clang++ -pipe   -Wno-unknown-warning-option -Wno-tautological-compare -Wno-sign-compare -D_REENTRANT  -O3 -I stan/lib/stan_math/lib/sundials_6.1.1/include -I stan/lib/stan_math/lib/sundials_6.1.1/src/sundials -DNO_FPRINTF_OUTPUT     -O3  -c -x c -include stan/lib/stan_math/lib/sundials_6.1.1/include/stan_sundials_printf_override.hpp stan/lib/stan_math/lib/sundials_6.1.1/src/nvector/serial/nvector_serial.c -o stan/lib/stan_math/lib/sundials_6.1.1/src/nvector/serial/nvector_serial.o
stan/lib/stan_math/lib/sundials_6.1.1/src/nvector/serial/nvector_serial.c:19:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~
1 error generated.
make: *** No rule to make target 'stan/lib/stan_math/lib/sundials_6.1.1/src/nvector/serial/nvector_serial.o', needed by 'stan/lib/stan_math/lib/sundials_6.1.1/lib/libsundials_nvecserial.a'.  Stop.

Command ['make', 'STANCFLAGS+=--filename-in-msg=model.stan', 'path/model']
	error during processing No such file or directory

Sometimes there are other errors, though always along these lines. For instance:

fatal error: 'stdlib.h' file not found

These errors are just for compiling models. If I take an already compiled model (compiled outside of the notebook), everything works fine, I’m able to sample from the model, and so on.

I’m not sure if this is an issue with VS Code or with my CmdStanPy installation or what. I have tried rebuilding the environment from scratch, and still get the error. I would really appreciate any help or guidance.

Operating System: macOS Ventura 13.2.1
Interface Version: CmdStanPy 1.2.2, CmdStan 2.34.1
Compiler/Toolkit: Apple clang version 14.0.0 (clang-1400.0.29.202)

Usually this issue arises when you have multiple toolchains installed. I noticed your error message mentions miniconda, so it’s possible it has installed its own copy of clang++. You can try running which clang++ in the terminal and !which clang++ in the ipynb to get started seeing if they match

Thanks for the response. I think this was the issue.

I ended up just deleting miniconda and setting up a totally clean environment. It solved the problem. A kind of drastic solution, but I’m not technologically gifted enough to figure out how to solve it more elegantly.