Compiling error in MacOS Monterey (clang)

Hello

I am new to PyStan. I am trying to run this code. I created a conda vitural environment but running train.py gives me the following error

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_33b001bedecf1e0fef804f7b6da8af31 NOW.
Traceback (most recent call last):
  File "/usr/local/anaconda3/envs/bayesian_mmm/lib/python3.9/site-packages/setuptools/_distutils/unixccompiler.py", line 173, in _compile
    self.spawn(compiler_so + cc_args + [src, '-o', obj] +
  File "/usr/local/anaconda3/envs/bayesian_mmm/lib/python3.9/site-packages/setuptools/_distutils/ccompiler.py", line 917, in spawn
    spawn(cmd, dry_run=self.dry_run, **kwargs)
  File "/usr/local/anaconda3/envs/bayesian_mmm/lib/python3.9/site-packages/setuptools/_distutils/spawn.py", line 68, in spawn
    raise DistutilsExecError(
distutils.errors.DistutilsExecError: command '/usr/local/anaconda3/envs/bayesian_mmm/bin/clang' failed with exit code 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/BayesianMMM-main/bayesian_mmm/train.py", line 147, in <module>
    run()
  File "/Users/BayesianMMM-main/bayesian_mmm/train.py", line 66, in run
    stan_model_generator.create_model()
  File "/Users/BayesianMMM-main/bayesian_mmm/sampling/stan_model_generator.py", line 40, in create_model
    self.__compile_code()
  File “`BayesianMMM-main/bayesian_mmm/sampling/stan_model_generator.py", line 313, in __compile_code
    self.__model.compile()
  File "/Users/BayesianMMM-main/bayesian_mmm/sampling/stan_model_wrapper.py", line 77, in compile
    self.__model = pystan.StanModel(model_code=self.__code, verbose=False)
  File "/usr/local/anaconda3/envs/bayesian_mmm/lib/python3.9/site-packages/pystan/model.py", line 378, in __init__
    build_extension.run()
  File "/usr/local/anaconda3/envs/bayesian_mmm/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py", line 339, in run
    self.build_extensions()
  File "/usr/local/anaconda3/envs/bayesian_mmm/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py", line 448, in build_extensions
    self._build_extensions_serial()
  File "/usr/local/anaconda3/envs/bayesian_mmm/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py", line 473, in _build_extensions_serial
    self.build_extension(ext)
  File "/usr/local/anaconda3/envs/bayesian_mmm/lib/python3.9/site-packages/setuptools/_distutils/command/build_ext.py", line 528, in build_extension
    objects = self.compiler.compile(sources,
  File "/usr/local/anaconda3/envs/bayesian_mmm/lib/python3.9/site-packages/setuptools/_distutils/ccompiler.py", line 574, in compile
    self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
  File "/usr/local/anaconda3/envs/bayesian_mmm/lib/python3.9/site-packages/setuptools/_distutils/unixccompiler.py", line 176, in _compile
    raise CompileError(msg)
distutils.errors.CompileError: command '/usr/local/anaconda3/envs/bayesian_mmm/bin/clang' failed with exit code 1

Process finished with exit code 1

I have tried a variety of suggestions from stackexchange without resolving the problem.

 export CC=x86_64-apple-darwin13.4.0-clang   
 export CXX=x86_64-apple-darwin13.4.0-clang++
export CC=x86_64-apple-darwin21.4.0-clang 
export CXX=x86_64-apple-darwin21.4.0-clang++
export CC=clang_osx  
 export CXX=clangxx_osx-64

I get a different error, if I insert the following in train.py

import os
os.environ['CC'] = 'gcc-11'
os.environ['CXX'] = 'g++-11'

Traceback (most recent call last):
  File "/Users/lois/Library/CloudStorage/OneDrive-IQUANTIIndiaPvt.Ltd/Analytics/MMM/PyMMM/code/BayesianMMM-main/bayesian_mmm/train.py", line 147, in <module>
    run()
  File "/Users/lois/Library/CloudStorage/OneDrive-IQUANTIIndiaPvt.Ltd/Analytics/MMM/PyMMM/code/BayesianMMM-main/bayesian_mmm/train.py", line 66, in run
    stan_model_generator.create_model()
  File "/Users/lois/Library/CloudStorage/OneDrive-IQUANTIIndiaPvt.Ltd/Analytics/MMM/PyMMM/code/BayesianMMM-main/bayesian_mmm/sampling/stan_model_generator.py", line 40, in create_model
    self.__compile_code()
  File "/Users/lois/Library/CloudStorage/OneDrive-IQUANTIIndiaPvt.Ltd/Analytics/MMM/PyMMM/code/BayesianMMM-main/bayesian_mmm/sampling/stan_model_generator.py", line 313, in __compile_code
    self.__model.compile()
  File "/Users/lois/Library/CloudStorage/OneDrive-IQUANTIIndiaPvt.Ltd/Analytics/MMM/PyMMM/code/BayesianMMM-main/bayesian_mmm/sampling/stan_model_wrapper.py", line 77, in compile
    self.__model = pystan.StanModel(model_code=self.__code, verbose=False)
  File "/usr/local/anaconda3/envs/bayesian_mmm/lib/python3.9/site-packages/pystan/model.py", line 384, in __init__
    self.module = load_module(self.module_name, lib_dir)
  File "/usr/local/anaconda3/envs/bayesian_mmm/lib/python3.9/site-packages/pystan/model.py", line 50, in load_module
    return __import__(module_name)
ImportError: dlopen(/var/folders/f1/ftrbt5jx2x99rt4lj7c3rcb40000gn/T/pystan_bhiuqydh/stanfit4anon_model_33b001bedecf1e0fef804f7b6da8af31_3749951838933030311.cpython-39-darwin.so, 0x0002): symbol not found in flat namespace '__ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEED1Ev'

Process finished with exit code 1

Note:

  • Operating System: MacOS 12.4
  • Python Version: Python 3.9.12
  • PyStan Version: 2.19.1.1
which clang
/usr/local/anaconda3/envs/bayesian_mmm/bin/clang
 which gcc
/usr/local/bin/gcc
 ls /usr/local/anaconda3/envs/bayesian_mmm/bin/ | grep clang   
-clang++
clang
clang++
clang-11
clang-cl
clang-cpp
x86_64-apple-darwin13.4.0-clang
x86_64-apple-darwin13.4.0-clang++

Please help me resolve this. Thank you!

I wonder if you rename anaconda folder temporarily and compile again - would it be successful? Sometimes Anaconda is in conflict with some other installations of the compiler, which causes error (at least, it was like that in my case)