# Compiler Error with PyStan running the Eight School Problem

**URL:** <https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627>\
**Category:** Interfaces\
**Created:** [November 17, 2018, 9:18pm UTC](https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627 "2018-11-17T21:18:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephando](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephando/32/1928_2.png) [@stephando](https://discourse.mc-stan.org/u/stephando)\
**Post date:** [November 17, 2018, 9:18pm UTC](https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627/1 "2018-11-17T21:18:10Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [November 18, 2018, 5:17am UTC](https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627/2 "2018-11-18T05:17:17Z")

</div>

Did you use pip or conda?

Can you compile model manually?

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

---

<div class="post-metadata">

**Author:** ![stephando](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephando/32/1928_2.png) [@stephando](https://discourse.mc-stan.org/u/stephando)\
**Post date:** [November 18, 2018, 6:05pm UTC](https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627/3 "2018-11-18T18:05:27Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [November 18, 2018, 6:22pm UTC](https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627/4 "2018-11-18T18:22:02Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![stephando](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephando/32/1928_2.png) [@stephando](https://discourse.mc-stan.org/u/stephando)\
**Post date:** [November 18, 2018, 7:44pm UTC](https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627/5 "2018-11-18T19:44:29Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [November 18, 2018, 7:45pm UTC](https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627/6 "2018-11-18T19:45:43Z")

</div>

try without `_`

```
pystan.StanModel(...)
```

---

<div class="post-metadata">

**Author:** ![stephando](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephando/32/1928_2.png) [@stephando](https://discourse.mc-stan.org/u/stephando)\
**Post date:** [November 18, 2018, 7:49pm UTC](https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627/7 "2018-11-18T19:49:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [November 18, 2018, 7:59pm UTC](https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627/8 "2018-11-18T19:59:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stephando](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephando/32/1928_2.png) [@stephando](https://discourse.mc-stan.org/u/stephando)\
**Post date:** [November 18, 2018, 8:06pm UTC](https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627/9 "2018-11-18T20:06:38Z")

</div>

> [@ahartikainen](#):
>
> gcc --version

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

---

<div class="post-metadata">

**Author:** ![ahartikainen](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ahartikainen/32/321_2.png) [@ahartikainen](https://discourse.mc-stan.org/u/ahartikainen)\
**Post date:** [November 18, 2018, 8:35pm UTC](https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627/10 "2018-11-18T20:35:52Z")

</div>

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

```
source activate

```

or

```
source activate base
```

---

<div class="post-metadata">

**Author:** ![stephando](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephando/32/1928_2.png) [@stephando](https://discourse.mc-stan.org/u/stephando)\
**Post date:** [November 18, 2018, 9:05pm UTC](https://discourse.mc-stan.org/t/compiler-error-with-pystan-running-the-eight-school-problem/6627/11 "2018-11-18T21:05:32Z")

</div>

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