ImportError in PyStan with Eight Schools (macOS)

Dear Stan users,

I would like to use Stan from Python on a Mac, but I keep failing at the model compilation step. I have fixed 3 different error messages (by installing the gcc headers to /usr/include, upgrading gcc, and something else), but this one persists.

Steps I did so far, in order:

$ open /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg  # I guess it's not needed anymore
$ brew install gcc49
$ sudo ln -s `which gcc-4.9` /usr/local/bin/gcc
$ conda create -n pystan python=3.7
$ conda activate pystan
(pystan) $ pip install pystan

Then in Python:

>>> import pystan
>>> sm = pystan.StanModel(file='8schools.stan')
INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_4337cb01f5c9774a0dd265dcc01c105e NOW.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/L/miniconda3/envs/pystan/lib/python3.7/site-packages/pystan/", line 355, in __init__
    self.module = load_module(self.module_name, lib_dir)
  File "/Users/L/miniconda3/envs/pystan/lib/python3.7/site-packages/pystan/", line 50, in load_module
    return __import__(module_name)
ImportError: dlopen(/var/folders/fj/w45tzg1d3v91l288cdvrr5z40000gn/T/tmp59jlnbn7/, 2): Symbol not found: __ZNKSt5ctypeIcE13_M_widen_initEv
  Referenced from: /var/folders/fj/w45tzg1d3v91l288cdvrr5z40000gn/T/tmp59jlnbn7/
  Expected in: flat namespace
 in /var/folders/fj/w45tzg1d3v91l288cdvrr5z40000gn/T/tmp59jlnbn7/

If your question relates to installation please also provide the following information:

  • macOS Mojave (10.14.5)
  • Python 3.7.3 (default, Mar 27 2019, 16:54:48) [Clang 4.0.1]
  • PyStan
  • GCC 4.9.4
$ gcc -v
Using built-in specs.
Target: x86_64-apple-darwin17.3.0
Configured with: ../configure --build=x86_64-apple-darwin17.3.0 --prefix=/usr/local/Cellar/gcc@4.9/4.9.4_1 --libdir=/usr/local/Cellar/gcc@4.9/4.9.4_1/lib/gcc/4.9 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-4.9 --with-system-zlib --enable-libstdcxx-time=yes --enable-stage1-checking --enable-checking=release --enable-lto --enable-plugin --with-build-config=bootstrap-debug --disable-werror --with-pkgversion='Homebrew GCC 4.9.4_1' --with-bugurl= MAKEINFO=missing --disable-nls --enable-multilib
Thread model: posix
gcc version 4.9.4 (Homebrew GCC 4.9.4_1)

I get the same error with the bernoulli.stan model from the CmdStan examples, but compilation+sampling with CmdStan works fine.

Any help would be much appreciated.

Try to define CC and CXX

1 Like

Thanks for the hint!

I found two solutions:

A. Link g++ too:

$ ln -s `which g++-4.9` /usr/local/bin/g++

B. Set the CC and CXX environmental variables:

$ export CC=`which gcc-4.9`
$ export CXX=`which g++-4.9`
$ python
1 Like