Compiler Error with PyStan running the Eight School Problem

Operating System:Mac OS 10.14 Mojave
Interface Version:2.18.0.0
Compiler/Toolkit:

Hi
Having installed Python 3.6.6 over Anaconda and PyStan 2.18, I was trying to run the Eight School Problem in Jupyter:
http://mc-stan.org/users/documentation/case-studies/pystan_workflow.html

On the line
model = stan_utility.compile_model(‘eight_schools_cp.stan’)

I get the following error (copied the first 13 lines):

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_71b609c34d59a40b345b5328a36dbb39 NOW.
/anaconda3/lib/python3.6/site-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive ‘language_level’ not set, using 2 for now (Py2). This will change in a later release! File: /var/folders/r9/xxppbx0s5q190x6xqpsjcx6m0000gp/T/tmpbd0odkm6/stanfit4anon_model_71b609c34d59a40b345b5328a36dbb39_3878708735013528215.pyx
tree = Parsing.p_module(s, pxd, full_module_name)

FileNotFoundError Traceback (most recent call last)
~/Dropbox/02 economics/00 my projects/10 Bayesian Anaysis/09 PyStan/pystan_workflow/stan_utility.py in compile_model(filename, model_name, **kwargs)
134 try:
–> 135 sm = pickle.load(open(cache_fn, ‘rb’))
136 except:

FileNotFoundError: [Errno 2] No such file or directory: ‘cached-model-71b609c34d59a40b345b5328a36dbb39.pkl’

I don’t know how to fix the error. Any help is very much appreciated. Best -Stephan

Did you use pip or conda?

Can you compile model manually?

import pystan
model_code = """..."""
stan_model = pystan.StanModel(model_code=model_code)

Thanks for looking at that.
I installed Pystan over Anaconda, so conda install -c conda-forge pystan

when trying:

import pystan as pys
schools_code = “”"data {
int<lower=0> J;
real y[J];
real<lower=0> sigma[J];
}

parameters {
real mu;
real<lower=0> tau;
real theta[J];
}

model {
mu ~ normal(0, 5);
tau ~ cauchy(0, 5);
theta ~ normal(mu, tau);
y ~ normal(theta, sigma);
}"""
pys.stan_model(model_code = schools_code)

I get the error:


AttributeError Traceback (most recent call last)
in
----> 1 pys.stan_model(model_code = schools_code)

AttributeError: module ‘pystan’ has no attribute ‘stan_model’

Heh, sorry, I was too fast to write that, it was missing pystan function

stan_model = pystan.StanModel(model_code=model_code)

But there is apparently something wrong going with pystan 2.18 on anaconda channel. Conda forge should work. (I don’t remember if 2.18 is there yet)

Install with pip, we have wheels so it should be quick

pip install pystan

I have uninstalled pystan in conda and then installed via pip as you suggested. A check:
(base) ikonmacpro:~ stephan$ conda list | grep pystan
pystan 2.18.0.0

But running the code reveals the same problem… (maybe I screwed sth up with the environments…?)

import pystan as pys

eightschool_stan = pys.Stan_Model(model_code = schools_code)


AttributeError Traceback (most recent call last)
in
----> 1 eightschool_stan = pys.Stan_Model(model_code = schools_code)

AttributeError: module ‘pystan’ has no attribute ‘Stan_Model’

try without _

pystan.StanModel(...)

the command is getting recognized but I again get the compiler problem:

eightschool_stan = pys.StanModel(model_code = schools_code)

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_b061d14b15cea7d7e6cf0b7700a8b143 NOW.
/anaconda3/lib/python3.6/site-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive ‘language_level’ not set, using 2 for now (Py2). This will change in a later release! File: /var/folders/r9/xxppbx0s5q190x6xqpsjcx6m0000gp/T/tmp5h3kq2c2/stanfit4anon_model_b061d14b15cea7d7e6cf0b7700a8b143_8651377948048783664.pyx
tree = Parsing.p_module(s, pxd, full_module_name)

DistutilsExecError Traceback (most recent call last)
/anaconda3/lib/python3.6/distutils/unixccompiler.py in _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)
117 self.spawn(compiler_so + cc_args + [src, ‘-o’, obj] +
–> 118 extra_postargs)
119 except DistutilsExecError as msg:

/anaconda3/lib/python3.6/distutils/ccompiler.py in spawn(self, cmd)
908 def spawn(self, cmd):
–> 909 spawn(cmd, dry_run=self.dry_run)
910

/anaconda3/lib/python3.6/distutils/spawn.py in spawn(cmd, search_path, verbose, dry_run)
35 if os.name == ‘posix’:
—> 36 _spawn_posix(cmd, search_path, dry_run=dry_run)
37 elif os.name == ‘nt’:

/anaconda3/lib/python3.6/distutils/spawn.py in _spawn_posix(cmd, search_path, verbose, dry_run)
158 “command %r failed with exit status %d”
–> 159 % (cmd, exit_status))
160 elif os.WIFSTOPPED(status):

DistutilsExecError: command ‘gcc’ failed with exit status 1

During handling of the above exception, another exception occurred:

CompileError Traceback (most recent call last)
in
----> 1 eightschool_stan = pys.StanModel(model_code = schools_code)

/anaconda3/lib/python3.6/site-packages/pystan/model.py in init(self, file, charset, model_name, model_code, stanc_ret, include_paths, boost_lib, eigen_lib, verbose, obfuscate_model_name, extra_compile_args)
348
349 try:
–> 350 build_extension.run()
351 finally:
352 if redirect_stderr:

/anaconda3/lib/python3.6/distutils/command/build_ext.py in run(self)
337
338 # Now actually compile and link everything.
–> 339 self.build_extensions()
340
341 def check_extensions_list(self, extensions):

/anaconda3/lib/python3.6/distutils/command/build_ext.py in build_extensions(self)
446 self._build_extensions_parallel()
447 else:
–> 448 self._build_extensions_serial()
449
450 def _build_extensions_parallel(self):

/anaconda3/lib/python3.6/distutils/command/build_ext.py in _build_extensions_serial(self)
471 for ext in self.extensions:
472 with self._filter_build_errors(ext):
–> 473 self.build_extension(ext)
474
475 @contextlib.contextmanager

/anaconda3/lib/python3.6/distutils/command/build_ext.py in build_extension(self, ext)
531 debug=self.debug,
532 extra_postargs=extra_args,
–> 533 depends=ext.depends)
534
535 # XXX outdated variable, kept here in case third-part code

/anaconda3/lib/python3.6/distutils/ccompiler.py in compile(self, sources, output_dir, macros, include_dirs, debug, extra_preargs, extra_postargs, depends)
572 except KeyError:
573 continue
–> 574 self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
575
576 # Return all object filenames, not just the ones we just built.

/anaconda3/lib/python3.6/distutils/unixccompiler.py in _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)
118 extra_postargs)
119 except DistutilsExecError as msg:
–> 120 raise CompileError(msg)
121
122 def create_static_lib(self, objects, output_libname,

CompileError: command ‘gcc’ failed with exit status 1

Thanks for the info.

I start to wonder if default osx compiler is C++11 compatible.

What does gcc --version give you?

Can you try conda install -c conda-forge gcc and then try to compile again.

Thanks very much for looking into this Ari.
I tried it now with launching the Jupyter notebook over the Anaconda-Navigator and now its compiling the model. I don’t know… can’t make much sense out of it.

The compiler is

(base) ikonmacpro:~ stephan$ gcc --version

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1

Apple LLVM version 10.0.0 (clang-1000.11.45.2)

Target: x86_64-apple-darwin18.0.0

Thread model: posix

InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Great it works. What happens if you activate conda env and then try normal way?

source activate

or

source activate base

Thanks again Ari. I ran that but got a bit confusing results. Let me do that at another time, with a fresh start.

1 Like