CompileError: even after successfully installing pystan

Hello, I installed pystan on Windows using Anaconda, i.e., pip install pystan.

The installation process finished successfully. But running the following code will generate the error message as follows

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,
            }

fit = pystan.stan(model_code=coin_code, data=coin_dat, iter=1000, chains=1)
WARNING:pystan:DeprecationWarning: pystan.stan was deprecated in version 2.17 and will be removed in version 3.0. Compile and use a Stan program in separate steps.
INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_7f1947cd2d39ae427cd7b6bb6e6ffd77 NOW.
WARNING:pystan:MSVC compiler is not supported
---------------------------------------------------------------------------
DistutilsExecError                        Traceback (most recent call last)
~\Anaconda3\envs\tensorflow\lib\distutils\_msvccompiler.py in compile(self, sources, output_dir, macros, include_dirs, debug, extra_preargs, extra_postargs, depends)
    395             try:
--> 396                 self.spawn(args)
    397             except DistutilsExecError as msg:

~\Anaconda3\envs\tensorflow\lib\distutils\_msvccompiler.py in spawn(self, cmd)
    501             os.environ['path'] = self._paths
--> 502             return super().spawn(cmd)
    503         finally:

~\Anaconda3\envs\tensorflow\lib\distutils\ccompiler.py in spawn(self, cmd)
    909     def spawn(self, cmd):
--> 910         spawn(cmd, dry_run=self.dry_run)
    911 

~\Anaconda3\envs\tensorflow\lib\distutils\spawn.py in spawn(cmd, search_path, verbose, dry_run)
     37     elif os.name == 'nt':
---> 38         _spawn_nt(cmd, search_path, dry_run=dry_run)
     39     else:

~\Anaconda3\envs\tensorflow\lib\distutils\spawn.py in _spawn_nt(cmd, search_path, verbose, dry_run)
     80             raise DistutilsExecError(
---> 81                   "command %r failed with exit status %d" % (cmd, rc))
     82 

DistutilsExecError: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.10.25017\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2

During handling of the above exception, another exception occurred:

CompileError                              Traceback (most recent call last)
<ipython-input-3-7570001413d3> in <module>
     21             }
     22 
---> 23 fit = pystan.stan(model_code=coin_code, data=coin_dat, iter=1000, chains=1)

~\Anaconda3\envs\tensorflow\lib\site-packages\pystan\api.py in stan(file, model_name, model_code, fit, data, pars, chains, iter, warmup, thin, init, seed, algorithm, control, sample_file, diagnostic_file, verbose, boost_lib, eigen_lib, include_paths, n_jobs, allow_undefined, **kwargs)
    433                       include_paths=include_paths,
    434                       obfuscate_model_name=obfuscate_model_name, verbose=verbose,
--> 435                       allow_undefined=allow_undefined)
    436     # check that arguments in kwargs are valid
    437     valid_args = {"chain_id", "init_r", "test_grad", "append_samples", "enable_random_init",

~\Anaconda3\envs\tensorflow\lib\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, allow_undefined, include_dirs, includes)
    376 
    377         try:
--> 378             build_extension.run()
    379         finally:
    380             if redirect_stderr:

~\Anaconda3\envs\tensorflow\lib\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):

~\Anaconda3\envs\tensorflow\lib\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):

~\Anaconda3\envs\tensorflow\lib\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

~\Anaconda3\envs\tensorflow\lib\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

~\Anaconda3\envs\tensorflow\lib\distutils\_msvccompiler.py in compile(self, sources, output_dir, macros, include_dirs, debug, extra_preargs, extra_postargs, depends)
    396                 self.spawn(args)
    397             except DistutilsExecError as msg:
--> 398                 raise CompileError(msg)
    399 
    400         return objects

CompileError: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.10.25017\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2

Hi, did you follow Windows instructions in pystan2 docs?

https://pystan2.readthedocs.io/en/latest/windows.html

I also has similar problem but on mac m1. It displays “raise CompileError(msg)
distutils.errors.CompileError: command ‘/Users/miniforge3/envs/miniforge/bin/clang’ failed with exit code 1”, how can I solve this?

you could try CmdStanPy. CmdStanPy 1.0

Please create a new discussion for macOS (you could check what problems other have had with M1 → search discourse)

Using
conda install pystan -c conda-forge
instead of
pip install pystan
has fixed the same issue for me.

I followed the rest of the instructions from PyStan on Windows — PyStan 2.19.1.1 documentation