Complier error even if installation on MAC M1

I have installed pystan but it still displays complier error.

import pystan
coin_code = """
data {
    int<lower=0> n; // number of tosses
    int<lower=0> y; // number of heads
}
transformed data {}
parameters {
    real<lower=0, upper=1> p;
}
transformed parameters {}
model {
    p ~ beta(2, 2);
    y ~ binomial(n, p);
}
generated quantities {}
"""
coin_dat = {
    'n': 100,
    'y': 61,
}
model = pystan.StanModel(model_code=coin_code)






INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_7f1947cd2d39ae427cd7b6bb6e6ffd77 NOW.
Traceback (most recent call last):
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/unixccompiler.py", line 117, in _compile
    self.spawn(compiler_so + cc_args + [src, '-o', obj] +
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/ccompiler.py", line 910, in spawn
    spawn(cmd, dry_run=self.dry_run)
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/spawn.py", line 91, in spawn
    raise DistutilsExecError(
distutils.errors.DistutilsExecError: command '/Users/miniforge3/envs/miniforge/bin/clang' failed with exit code 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 39, in <module>
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/site-packages/pystan/model.py", line 378, in __init__
    build_extension.run()
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/command/build_ext.py", line 340, in run
    self.build_extensions()
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/command/build_ext.py", line 449, in build_extensions
    self._build_extensions_serial()
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/command/build_ext.py", line 474, in _build_extensions_serial
    self.build_extension(ext)
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/command/build_ext.py", line 529, in build_extension
    objects = self.compiler.compile(sources,
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/ccompiler.py", line 574, in compile
    self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/unixccompiler.py", line 120, in _compile
    raise CompileError(msg)
distutils.errors.CompileError: command '/Users/miniforge3/envs/miniforge/bin/clang' failed with exit code 1
  • Operating System:Mac M1
  • Python Version:3.9.7
  • PyStan Version:2.19.1.1

Can I have some suggestions?

1 Like

How did you install pystan?

I recommend that you install pystan from source, because I think we don’t have suitable 2.19 binaries for M1

From the error logs, it looks like there are stack traces from python but there are no error logs from the actual compiler that might give a precise clue about what has gone wrong.

Aha – it looks like pystan2 hides all the compiler error messages by default, which is helpful in the case where compilation succeeds, and less helpful in cases like this where it doesn’t.

Try setting verbose=True when you create the StanModel instance, i.e.:

model = pystan.StanModel(model_code=coin_code, verbose=True)

it still won’t work, but the compiler error messages may give you more direct clues as to why it isn’t working.

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_7f1947cd2d39ae427cd7b6bb6e6ffd77 NOW.
INFO:pystan:OS: darwin, Python: 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:22:19) 
[Clang 11.1.0 ], Cython 0.29.24
Compiling /var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy/stanfit4anon_model_7f1947cd2d39ae427cd7b6bb6e6ffd77_7624627078521180498.pyx because it changed.
[1/1] Cythonizing /var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy/stanfit4anon_model_7f1947cd2d39ae427cd7b6bb6e6ffd77_7624627078521180498.pyx
building 'stanfit4anon_model_7f1947cd2d39ae427cd7b6bb6e6ffd77_7624627078521180498' extension
creating /var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy/var
creating /var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy/var/folders
creating /var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy/var/folders/ft
creating /var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy/var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn
creating /var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy/var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T
creating /var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy/var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /Users/miniforge3/envs/miniforge/include -arch arm64 -fPIC -O2 -isystem /Users/miniforge3/envs/miniforge/include -arch arm64 -DBOOST_RESULT_OF_USE_TR1 -DBOOST_NO_DECLTYPE -DBOOST_DISABLE_ASSERTS -I/var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy -I/Users/miniforge3/envs/miniforge/lib/python3.9/site-packages/pystan -I/Users/miniforge3/envs/miniforge/lib/python3.9/site-packages/pystan/stan/src -I/Users/miniforge3/envs/miniforge/lib/python3.9/site-packages/pystan/stan/lib/stan_math -I/Users/miniforge3/envs/miniforge/lib/python3.9/site-packages/pystan/stan/lib/stan_math/lib/eigen_3.3.3 -I/Users/miniforge3/envs/miniforge/lib/python3.9/site-packages/pystan/stan/lib/stan_math/lib/boost_1.69.0 -I/Users/miniforge3/envs/miniforge/lib/python3.9/site-packages/pystan/stan/lib/stan_math/lib/sundials_4.1.0/include -I/Users/miniforge3/envs/miniforge/lib/python3.9/site-packages/numpy/core/include -I/Users/miniforge3/envs/miniforge/include/python3.9 -c /var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy/stanfit4anon_model_7f1947cd2d39ae427cd7b6bb6e6ffd77_7624627078521180498.cpp -o /var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy/var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy/stanfit4anon_model_7f1947cd2d39ae427cd7b6bb6e6ffd77_7624627078521180498.o -O2 -ftemplate-depth-256 -Wno-unused-function -Wno-uninitialized -std=c++1y
In file included from /var/folders/ft/vhc7p9z96g9991n6nmwv56yc0000gn/T/pystan_swcurfcy/stanfit4anon_model_7f1947cd2d39ae427cd7b6bb6e6ffd77_7624627078521180498.cpp:62:
In file included from /Users/miniforge3/envs/miniforge/include/python3.9/Python.h:25:
/Users/miniforge3/envs/miniforge/bin/../include/c++/v1/stdio.h:107:15: fatal error: 'stdio.h' file not found
#include_next <stdio.h>
              ^~~~~~~~~
1 error generated.
Traceback (most recent call last):
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/unixccompiler.py", line 117, in _compile
    self.spawn(compiler_so + cc_args + [src, '-o', obj] +
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/ccompiler.py", line 910, in spawn
    spawn(cmd, dry_run=self.dry_run)
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/spawn.py", line 91, in spawn
    raise DistutilsExecError(
distutils.errors.DistutilsExecError: command '/Users/miniforge3/envs/miniforge/bin/clang' failed with exit code 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/site-packages/pystan/model.py", line 378, in __init__
    build_extension.run()
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/command/build_ext.py", line 340, in run
    self.build_extensions()
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/command/build_ext.py", line 449, in build_extensions
    self._build_extensions_serial()
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/command/build_ext.py", line 474, in _build_extensions_serial
    self.build_extension(ext)
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/command/build_ext.py", line 529, in build_extension
    objects = self.compiler.compile(sources,
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/ccompiler.py", line 574, in compile
    self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
  File "/Users/miniforge3/envs/miniforge/lib/python3.9/distutils/unixccompiler.py", line 120, in _compile
    raise CompileError(msg)
distutils.errors.CompileError: command '/Users/miniforge3/envs/miniforge/bin/clang' failed with exit code 1```

After adding verbose =True, it turns out 'stdio.h' file not found.

Try to install or run pystan from a python session that has sudo privileges. I don’t know how to explain but MacOS Xcode stuff for C++ headers works only with sudo privileges. I’ve tried to find the answer for years (I’ve been a long time Mac and Stan user), but to no avail.

See if that helps.

EDIT: Please be advised that running stuff with sudo can be dangerous. See the comment below from @ahartikainen.

1 Like

I’m not an expert on macs, but I will never recommend that people touch their system python or run python with sudo.

I had mac 8 years and setting up compiler was pain, sudo was needed for xcode, but python never needed sudo.

1 Like

How would someone go about building it from source? Is there a script we can follow? Thanks very much!

I have been trying to install pystan==2.19 on MacM1. Here is my environment:

conda create -n stan2 python=3.7 --yes
conda activate stan2
conda install -c anaconda clang_osx-64 clangxx_osx-64 --yes
conda install -c conda-forge numpy pandas scipy cython ipython jupyterlab matplotlib freetype --yes
python -m pip install --upgrade pip
pip install pystan==2.19.0.0
conda install -c conda-forge arviz --yes

python -c "import pystan; import arviz"

But I get the following error when running the 8 schools code here

When running this line sm = pystan.StanModel(model_code=schools_code) on M1 Mac:

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW.
---------------------------------------------------------------------------
DistutilsExecError                        Traceback (most recent call last)
~/miniconda3/envs/stan2/lib/python3.7/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:

~/miniconda3/envs/stan2/lib/python3.7/distutils/ccompiler.py in spawn(self, cmd)
    909     def spawn(self, cmd):
--> 910         spawn(cmd, dry_run=self.dry_run)
    911

~/miniconda3/envs/stan2/lib/python3.7/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':

~/miniconda3/envs/stan2/lib/python3.7/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 'x86_64-apple-darwin13.4.0-clang' failed with exit status 254

During handling of the above exception, another exception occurred:

CompileError                              Traceback (most recent call last)
<ipython-input-2-25aa85272b50> in <module>
     24                'sigma': [15, 10, 16, 11,  9, 11, 10, 18]}
     25
---> 26 sm = pystan.StanModel(model_code=schools_code)
     27 fit = sm.sampling(data=schools_dat, iter=1000, chains=4)

~/miniconda3/envs/stan2/lib/python3.7/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)
    347
    348         try:
--> 349             build_extension.run()
    350         finally:
    351             if redirect_stderr:

~/miniconda3/envs/stan2/lib/python3.7/distutils/command/build_ext.py in run(self)
    338
    339         # Now actually compile and link everything.
--> 340         self.build_extensions()
    341
    342     def check_extensions_list(self, extensions):

~/miniconda3/envs/stan2/lib/python3.7/distutils/command/build_ext.py in build_extensions(self)
    447             self._build_extensions_parallel()
    448         else:
--> 449             self._build_extensions_serial()
    450
    451     def _build_extensions_parallel(self):

~/miniconda3/envs/stan2/lib/python3.7/distutils/command/build_ext.py in _build_extensions_serial(self)
    472         for ext in self.extensions:
    473             with self._filter_build_errors(ext):
--> 474                 self.build_extension(ext)
    475
    476     @contextlib.contextmanager

~/miniconda3/envs/stan2/lib/python3.7/distutils/command/build_ext.py in build_extension(self, ext)
    532                                          debug=self.debug,
    533                                          extra_postargs=extra_args,
--> 534                                          depends=ext.depends)
    535
    536         # XXX outdated variable, kept here in case third-part code

~/miniconda3/envs/stan2/lib/python3.7/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.

~/miniconda3/envs/stan2/lib/python3.7/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 'x86_64-apple-darwin13.4.0-clang' failed with exit status 254

Which makes sense to me: the C++ bits are made for an Intel x86 architecture, but the Mac has an M1 chip (very imprecise language because that is all I understand).

Is there a solution? Not to worry if not, I can get Pystan3 working with this environment in my gist here

The reason I was hoping to also install Pystan2 was that I am attending a course with the materials in Pystan2. Worst case scenario I can
(i) do the translation exercise into Pystan3
(ii) run a Linux Virtual Machine somewhere and install the environment from here

this may have something to do with it. The following message after running conda install -c anaconda clang_osx-64 clangxx_osx-64 --yes

objc[16431]: Class AMSupportURLConnectionDelegate is implemented in both /usr/lib/libamsupport.dylib (0x1e99eb678) and /Library/Apple/System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/MobileDevice (0x1042202c8). One of the two will be used. Which one is undefined.
objc[16431]: Class AMSupportURLSession is implemented in both /usr/lib/libamsupport.dylib (0x1e99eb6c8) and /Library/Apple/System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/MobileDevice (0x104220318). One of the two will be used. Which one is undefined.
objc[16437]: Class AMSupportURLConnectionDelegate is implemented in both /usr/lib/libamsupport.dylib (0x1e99eb678) and /Library/Apple/System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/MobileDevice (0x10442c2c8). One of the two will be used. Which one is undefined.
objc[16437]: Class AMSupportURLSession is implemented in both /usr/lib/libamsupport.dylib (0x1e99eb6c8) and /Library/Apple/System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/MobileDevice (0x10442c318). One of the two will be used. Which one is undefined.