Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

ImportError: DLL load failed: The specified module could not be found. #520

Closed
iMikeT opened this issue Sep 30, 2018 · 41 comments
Closed

ImportError: DLL load failed: The specified module could not be found. #520

iMikeT opened this issue Sep 30, 2018 · 41 comments

Comments

@iMikeT
Copy link

iMikeT commented Sep 30, 2018

Summary:

I have tried to install PyStan using my Anaconda 5.0.0 following the instructions on the PyStan on Windows documentation here: https://pystan.readthedocs.io/en/latest/windows.html#windows

Description:

I did each step starting with installing the C++ compiler using conda install libpython m2w64-toolchain -c msys2 and it made the file distutils.cfg in the correct directory. I then followed the dependencies steps as conda install numpy cython -c conda-forge and then conda install matplotlib scipy pandas -c conda-forge which all seemed fine. Finally, I used the recommended pip install pystan and this was successful.

The problem starts when I try and verify everything was installed successfully. I open python from command prompt and type: import pystan and I straight away get this.

Current Output:

Python 3.6.2 |Anaconda, Inc.| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pystan
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\__init__.py", line 9, in <module>
    from pystan.api import stanc, stan
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\api.py", line 14, in <module>
    import pystan._api  # stanc wrapper
ImportError: DLL load failed: The specified module could not be found.

PyStan Version: 2.18.0.0

Python Version: 3.6

Operating System: Windows 10

I just wanted to install Stan to follow along with a text book that I have.

@ahartikainen
Copy link
Collaborator

Thanks for the info. I might have had the same problem before.

Try to install on a conda env?

@ahartikainen
Copy link
Collaborator

Also are you running with powershell or cmd.exe?

Ps might have some problems with conda.

@iMikeT
Copy link
Author

iMikeT commented Sep 30, 2018

I use an administrator cmd always for installing python packages. I'm still fairly new to all this so I'm not too sure how to use a conda env? is that the step in the documentation where you have activate, follow the install instructions, deactivate?

I have adapted to using Anaconda for Python as it was the only way I could install PyMC without problems.

@curtispuetz
Copy link

curtispuetz commented Oct 2, 2018

I am having the same problem. I have created multiple conda environments trying to install things differently. Some additional information that I can provide:

If I create a conda environment from scratch and run the following lines to install numpy, cython, matplotlib, scipy and pandas (just like they do near the end of the page on the pystan on Windows link you provided):
conda install numpy cython -c conda-forge
conda install matplotlib scipy pandas -c conda-forge

Then I get the following error when I try to import matplotlib:

ImportError: 
Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control).  Otherwise reinstall numpy.
Original error was: DLL load failed: The specified module could not be found.

@ahartikainen
Copy link
Collaborator

Interesting.

If I remember right this is related to this https://github.com/stan-dev/pystan/issues/472

Somehow install order fails. No idea why.

Can you try to reinstall msvc_runtime for your environment?

@curtispuetz
Copy link

In a Command Prompt running python I can import pystan and create a pystan.model.StanModel object with it. However, in Pycharm I still get the "DLL load failed: The specified module could not be found." error when I try to import pystan (using the same conda environment in each of course). For me the problem seems to be related to Pycharm.

@iMikeT
Copy link
Author

iMikeT commented Oct 2, 2018

Should I try to uninstall PyStan with pip and then reinstall? I just don't know if that will make a difference. Also in the document, when I try the following:

>>> import distutils
>>> print(distutils.__file__)

I am given the following return:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'distutils' has no attribute '_file_'

So that part of the documentation seems to not be working as well

@iMikeT
Copy link
Author

iMikeT commented Oct 2, 2018

My Python was working fine before trying to install PyStan and I have just tried a quick test in the Python console and I am still able to import my other modules just not PyStan @curtispuetz

Python 3.6.2 |Anaconda, Inc.| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> import pystan
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\__init__.py", line 9, in <module>
    from pystan.api import stanc, stan
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\api.py", line 14, in <module>
    import pystan._api  # stanc wrapper
ImportError: DLL load failed: The specified module could not be found.

@curtispuetz
Copy link

@iMikeT you are running this in Windows Command Prompt?

@iMikeT
Copy link
Author

iMikeT commented Oct 2, 2018

Yes as I said previously I have always used cmd for my conda install or pip install

@curtispuetz
Copy link

curtispuetz commented Oct 2, 2018

Ok. I know that this process worked for me:

conda create -n st_de python=3.7
activate st_de
conda install numpy
pip install pystan

Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import pystan
>>>

@iMikeT
Copy link
Author

iMikeT commented Oct 2, 2018

I think you need to edit that last comment... @curtispuetz It looks like there might be some typos with `

@curtispuetz
Copy link

@iMikeT I was asking if you are running python in the Command Prompt. Not what you were using to install stuff.

@iMikeT
Copy link
Author

iMikeT commented Oct 2, 2018

To test the install I used the cmd for python but to code properly I use Microsoft Visual Studio 2017. You can use this for free and it installs anaconda3 for you. That's the program I like to use and if I try to import pystan on that I get the same error output.

@curtispuetz
Copy link

Ic. I get the same error on Pycharm, which is like Microsoft Visual Studio but designed mainly for Python. But again, I do not get the error when using python in the cmd. You should try importing pystan in the cmd if you have not already and let me know if that works for you as well.

@iMikeT
Copy link
Author

iMikeT commented Oct 2, 2018

The error above is while importing pystan in the cmd. I can not import pystan at all

@ahartikainen
Copy link
Collaborator

ahartikainen commented Oct 2, 2018

Hi, thanks for trying to debug.

@iMikeT I missed your question about conda env.

Yes with conda you can create an environment that is not sharing its packages with other environments. conda create -n ...

I would not recommend installing your python so you need to use admin rights to update/install anything.

Can you pip uninstall pystan and then recompile it from source?

pip install git+https://github.com/stan-dev/pystan

For this you need git. conda install git -c conda-forge if needed.

What I think is going on is that somehow Python is not seeing some C++ lib or sees a wrong version of it. Something might be wrong with $PATH

In python

import os
os.environ['PATH'] # or just print os.environ

@curtispuetz
Copy link

curtispuetz commented Oct 2, 2018

Edit: please take note of a vital edit in the edited version of this comment
Thanks @ahartikainen,

As a workaround I added these 4 to $PATH:

<my_path_to_anaconda>\anaconda3
<my_path_to_anaconda>\anaconda3\Scripts
<my_path_to_anaconda>\anaconda3\Library\bin
<my_path_to_anaconda>\anaconda3\Library\mingw-w64\bin

This can be done in "edit system environment variables" in the control panel in windows.

For me I think Pycharm was not seeing the last one (mingw-w64) for some reason.

@ahartikainen
Copy link
Collaborator

Ok, thanks for that info. Now I have idea what is going wrong.

PyCharm / Basic Anaconda / Visual Studio is missing path locations to Anaconda/bin folder. These are usually added while activating the conda environment (it should be base for base).

conda activate base

and to see all environments

conda env list

Maybe there is a way to add some start-up script that calls activate when starting?

@curtispuetz
Copy link

I am fairly new to this so I'm not 100% sure I know what your talking about with the path stuff. But if it is helpful I know that in the Anaconda Prompt these paths seem to be there. i.e. if I run (in the base conda environment):

import os
ar = os.environ['PATH'].split(';')
for i in ar:
     print(i)

then I get:

C:\Users\cmp310\AppData\Local\Continuum\anaconda3
C:\Users\cmp310\AppData\Local\Continuum\anaconda3\Library\mingw-w64\bin
C:\Users\cmp310\AppData\Local\Continuum\anaconda3\Library\usr\bin
C:\Users\cmp310\AppData\Local\Continuum\anaconda3\Library\bin
C:\Users\cmp310\AppData\Local\Continuum\anaconda3\Scripts
C:\Users\cmp310\AppData\Local\Continuum\anaconda3\bin
C:\Program Files\USask-ARG\MartRetrievals\
C:\Program Files\USask-ARG\commonbin\
C:\Program Files (x86)\Intel\iCLS Client\
C:\Program Files\Intel\iCLS Client\
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL
C:\Program Files\Intel\Intel(R) Management Engine Components\DAL
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT
C:\Program Files\Intel\Intel(R) Management Engine Components\IPT
C:\Program Files\Git\cmd
C:\Program Files\Git LFS
C:\Users\cmp310\AppData\Local\Microsoft\WindowsApps
C:\Users\cmp310\AppData\Local\Continuum\anaconda3
C:\Users\cmp310\AppData\Local\Continuum\anaconda3\Scripts
C:\Users\cmp310\AppData\Local\Continuum\anaconda3\Library\bin
C:\Users\cmp310\AppData\Local\Continuum\anaconda3\Library\mingw-w64\bin

You can see the anaconda paths at the top, and then there are are again at the end because I put those ones in manually (because I need it for Pycharm).

@ahartikainen
Copy link
Collaborator

ahartikainen commented Oct 2, 2018

Yes,

Anaconda prompt is basically running conda activate base under the hood and adding those paths.

Let's see if we can find instructions to enable this for PyCharm, Visual Studio and Spyder and add them to the docs.

Good work!

@ahartikainen
Copy link
Collaborator

So based on quick googling

Can you try to start PyCharm from command line?

conda activate base
pycharm

Also for Visual Studio, is there a terminal or something similar? Activate the environment from there

conda activate base

If nothing else works, adding correct folders to %PATH% will help.

@curtispuetz
Copy link

@ahartikainen. Yes,

I just tried what you suggested. If I activate the conda environment I want to use in the command line and then open pycharm in the command line then those folders are set. No need to set $PATH$ manually if you open pycharm this way.

@iMikeT
Copy link
Author

iMikeT commented Oct 4, 2018

I added the following to path:

<my_path_to_anaconda>\anaconda3
<my_path_to_anaconda>\anaconda3\Scripts
<my_path_to_anaconda>\anaconda3\Library\bin
<my_path_to_anaconda>\anaconda3\Library\mingw-w64\bin

as @curtispuetz mentioned and then went to visual studio and tried running some code that I have:

import pystan as ps

schools_code = """
data {
    int<lower=0> J; // number of schools
    vector[J] y; // estimated treatment effects
    vector<lower=0>[J] sigma; // s.e. of effect estimates
}
parameters {
    real mu;
    real<lower=0> tau;
    vector[J] eta;
}
transformed parameters {
    vector[J] theta;
    theta = mu + tau * eta;
}
model {
    eta ~ normal(0, 1);
    y ~ normal(theta, sigma);
}
"""

schools_dat = {'J': 8,
               'y': [28,  8, -3,  7, -1,  1, 18, 12],
               'sigma': [15, 10, 16, 11,  9, 11, 10, 18]}

sm = ps.StanModel(model_code=schools_code)
fit = sm.sampling(data=schools_dat, iter=1000, chains=4)

This is from a lesson so I don't yet know what the output should be but when I ran, my console output gives:

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW.

which felt promising and this would remain for about 5-10 seconds and then it would output the same 4 more times like this:

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW.
INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW.
INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW.
INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW.
INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW.

and my cpu usage goes up to 100% which makes me nervous so I abort program with CTRL+C.

So what might this mean? It must now know where the compiler is but it feels like it's in a mad loop and I don't want my overclocked cpu burning up.
.
.
.
.
.
UPDATE: I tried my original test of just import pystan into python command prompt and that gives no error

Python 3.6.2 |Anaconda, Inc.| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pystan
>>>

So I continue with the rest of the example on the document as so:

>>> model_code = 'parameters {real y;} model {y ~ normal(0,1);}'
>>> model = pystan.StanModel(model_code=model_code)
INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_5944b02c79788fa0db5b3a93728ca2bf NOW.

When I saw this I tried to hold my nerve and there was no sudden spike! So I continue further down the rabbit hole:

>>> y = model.sampling().extract()['y']


G
radient evaluation took 0 secondsG
radient evaluation took 0 seconds1
G000 transitions using 10 leapfrog steps per transition would take 0 seconds.radient evaluation took 0 seconds1

A000 transitions using 10 leapfrog steps per transition would take 0 seconds.1djust your expectations accordingly!
000 transitions using 10 leapfrog steps per transition would take 0 seconds.
A

djust your expectations accordingly!
A
djust your expectations accordingly!
I

teration:    1 / 2000 [  0%]  (Warmup)

I
teration:    1 / 2000 [  0%]  (Warmup)Iteration:    1 / 2000 [  0%]  (Warmup)

Iteration:  200 / 2000 [ 10%]  (Warmup)
IIteration:  200 / 2000 [ 10%]  (Warmup)teration:  200 / 2000 [ 10%]  (Warmup)

Iteration:  400 / 2000 [ 20%]  (Warmup)
Iteration:  400 / 2000 [ 20%]  (Warmup)
Iteration:  400 / 2000 [ 20%]  (Warmup)I
teration:  600 / 2000 [ 30%]  (Warmup)
IIteration:  600 / 2000 [ 30%]  (Warmup)teration:  600 / 2000 [ 30%]  (Warmup)

Iteration:  800 / 2000 [ 40%]  (Warmup)
IIteration:  800 / 2000 [ 40%]  (Warmup)teration: 1000 / 2000 [ 50%]  (Warmup)

IIteration: 1001 / 2000 [ 50%]  (Sampling)
teration:  800 / 2000 [ 40%]  (Warmup)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
IIteration: 1001 / 2000 [ 50%]  (Sampling)teration: 1200 / 2000 [ 60%]  (Sampling)

Iteration: 1000 / 2000 [ 50%]  (Warmup)
Iteration: 1001 / 2000 [ 50%]  (Sampling)
IIteration: 1200 / 2000 [ 60%]  (Sampling)teration: 1400 / 2000 [ 70%]  (Sampling)

Iteration: 1200 / 2000 [ 60%]  (Sampling)

IIteration: 1600 / 2000 [ 80%]  (Sampling)teration: 1400 / 2000 [ 70%]  (Sampling)G

radient evaluation took 0 seconds
1000 transitions using 10 leapfrog steps per transition would take 0 seconds.I
teration: 1400 / 2000 [ 70%]  (Sampling)IA
teration: 1800 / 2000 [ 90%]  (Sampling)djust your expectations accordingly!I

teration: 1600 / 2000 [ 80%]  (Sampling)I
I
teration: 2000 / 2000 [100%]  (Sampling)
teration: 1600 / 2000 [ 80%]  (Sampling)
I

teration:    1 / 2000 [  0%]  (Warmup)I
 Elapsed Time: 0.01 seconds (Warm-up)teration: 1800 / 2000 [ 90%]  (Sampling)

I teration: 1800 / 2000 [ 90%]  (Sampling)              0.01 seconds (Sampling)

I Iteration:  200 / 2000 [ 10%]  (Warmup)              0.02 seconds (Total)teration: 2000 / 2000 [100%]  (Sampling)




I Elapsed Time: 0.012 seconds (Warm-up)teration: 2000 / 2000 [100%]  (Sampling)I

teration:  400 / 2000 [ 20%]  (Warmup)

               0.012 seconds (Sampling)Elapsed Time: 0.013 seconds (Warm-up)

                0.024 seconds (Total)              0.011 seconds (Sampling)


 I              0.024 seconds (Total)teration:  600 / 2000 [ 30%]  (Warmup)


Iteration:  800 / 2000 [ 40%]  (Warmup)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
Iteration: 1001 / 2000 [ 50%]  (Sampling)
Iteration: 1200 / 2000 [ 60%]  (Sampling)
Iteration: 1400 / 2000 [ 70%]  (Sampling)
Iteration: 1600 / 2000 [ 80%]  (Sampling)
Iteration: 1800 / 2000 [ 90%]  (Sampling)
Iteration: 2000 / 2000 [100%]  (Sampling)

 Elapsed Time: 0.011 seconds (Warm-up)
               0.006 seconds (Sampling)
               0.017 seconds (Total)

>>> y.mean()  # with luck the result will be near 0
-0.0054015061332986711
>>>

Is this the right result!?!? If so than it must just be my previous code is much more demanding but can you tell me why @ahartikainen ?

@ahartikainen
Copy link
Collaborator

Yes, looks good.

Is visual studio somehow running your code multiple times?

@iMikeT
Copy link
Author

iMikeT commented Oct 4, 2018

@ahartikainen I don't see how that would be possible. Clicking run only runs it once. What else could be happening? what happens to you when you run the code?

@ahartikainen
Copy link
Collaborator

ahartikainen commented Oct 4, 2018

It should show this line once

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW.

After that do sampling. It will do that by default 4 chains using min(nchain, cpu_count).

It will go to 100% cpu usage if it maxes out your cpu count. And can takes longer than simple models.

@iMikeT
Copy link
Author

iMikeT commented Oct 4, 2018

After trying to wait it out I got back this massive traceback:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\model.py", line 85, in _map_parallel
    pool = multiprocessing.Pool(processes=n_jobs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\context.py", line 119, in Pool
    context=self.get_context())
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\pool.py", line 174, in __init__
    self._repopulate_pool()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\pool.py", line 239, in _repopulate_pool
    w.start()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "F:\Documents\University\Math Project\Project Year 4\Python Code\Bayesian Video Examples\Bayesian Video Example 2\Bayesian Video Example 2\Bayesian_Video_Example_2.py", line 29, in <module>
    fit = sm.sampling(data=schools_dat, iter=1000, chains=4)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\model.py", line 776, in sampling
    ret_and_samples = _map_parallel(call_sampler_star, call_sampler_args, n_jobs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\model.py", line 88, in _map_parallel
    pool.close()
UnboundLocalError: local variable 'pool' referenced before assignment
Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\model.py", line 85, in _map_parallel
    pool = multiprocessing.Pool(processes=n_jobs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\context.py", line 119, in Pool
    context=self.get_context())
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\pool.py", line 174, in __init__
    self._repopulate_pool()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\pool.py", line 239, in _repopulate_pool
    w.start()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "F:\Documents\University\Math Project\Project Year 4\Python Code\Bayesian Video Examples\Bayesian Video Example 2\Bayesian Video Example 2\Bayesian_Video_Example_2.py", line 29, in <module>
    fit = sm.sampling(data=schools_dat, iter=1000, chains=4)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\model.py", line 776, in sampling
    ret_and_samples = _map_parallel(call_sampler_star, call_sampler_args, n_jobs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\model.py", line 88, in _map_parallel
    pool.close()
UnboundLocalError: local variable 'pool' referenced before assignment
Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\model.py", line 85, in _map_parallel
    pool = multiprocessing.Pool(processes=n_jobs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\context.py", line 119, in Pool
    context=self.get_context())
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\pool.py", line 174, in __init__
    self._repopulate_pool()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\pool.py", line 239, in _repopulate_pool
    w.start()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "F:\Documents\University\Math Project\Project Year 4\Python Code\Bayesian Video Examples\Bayesian Video Example 2\Bayesian Video Example 2\Bayesian_Video_Example_2.py", line 29, in <module>
    fit = sm.sampling(data=schools_dat, iter=1000, chains=4)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\model.py", line 776, in sampling
    ret_and_samples = _map_parallel(call_sampler_star, call_sampler_args, n_jobs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\model.py", line 88, in _map_parallel
    pool.close()
UnboundLocalError: local variable 'pool' referenced before assignment
Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\model.py", line 85, in _map_parallel
    pool = multiprocessing.Pool(processes=n_jobs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\context.py", line 119, in Pool
    context=self.get_context())
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\pool.py", line 174, in __init__
    self._repopulate_pool()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\pool.py", line 239, in _repopulate_pool
    w.start()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError:
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "F:\Documents\University\Math Project\Project Year 4\Python Code\Bayesian Video Examples\Bayesian Video Example 2\Bayesian Video Example 2\Bayesian_Video_Example_2.py", line 29, in <module>
    fit = sm.sampling(data=schools_dat, iter=1000, chains=4)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\model.py", line 776, in sampling
    ret_and_samples = _map_parallel(call_sampler_star, call_sampler_args, n_jobs)
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\pystan\model.py", line 88, in _map_parallel
    pool.close()
UnboundLocalError: local variable 'pool' referenced before assignment

Do you have any idea what this could mean? @ahartikainen

@ahartikainen
Copy link
Collaborator

Hmm, on windows run your sampling under

if __name__ == "__main__":
    sm = ...
    fit = ...

I kind of forget this, because I use jupyter lab/notebook, where that is the default env.

@iMikeT
Copy link
Author

iMikeT commented Oct 21, 2018

@ahartikainen Okay, I know it's been some time now. But Python is going good and now I wanted to move on to R. I'm just curious about the install process. I have installed R and RStudio and now I'm needing to do the RTools but I have already installed the compiler in the python folders. Should I still do the RTools process or try and direct R into the compiler directory within anaconda?

@ahartikainen
Copy link
Collaborator

If you use R and Rstudio from conda channels, I would use conda installed toolchain.

For external R / Rstudio, I would follow the common recommendation and use Rtools.

Basically they are the same compiler, but I'm not sure if Rtools have some R specific settings (probably not).

@Eh2406
Copy link

Eh2406 commented Feb 5, 2019

I am getting the same error. I have checked and the conda environments folders are in the path.

@ahartikainen
Copy link
Collaborator

Do you use pycharm?

@Eh2406
Copy link

Eh2406 commented Feb 6, 2019

I do use pycharm, but was not in this case. I was just using a plane cmd. I built a conda environment. installed dependencies as described on the install page, and got the errore from the OP. I am running win 10 and using 64 bit python.

@johntiger1
Copy link

I eventually fixed this by downgrading all the way to python 3.5

@Ritz111
Copy link

Ritz111 commented Mar 10, 2019

Should I try to uninstall PyStan with pip and then reinstall? I just don't know if that will make a difference. Also in the document, when I try the following:

>>> import distutils
>>> print(distutils.__file__)

I am given the following return:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'distutils' has no attribute '_file_'

So that part of the documentation seems to not be working as well

you should use double '_'

@S-Black
Copy link
Contributor

S-Black commented Apr 18, 2019

Hmm, on windows run your sampling under

if __name__ == "__main__":
    sm = ...
    fit = ...

I kind of forget this, because I use jupyter lab/notebook, where that is the default env.

Thanks @ahartikainen, this has solved my issue with being able to run PyStan in the Atom editor on a Windows machine.

I open a comand prompt window, type activate stan_env and then atom to launch the editor in my Stan environment. I can then run the below code my pressing F5 as usual and it works.

import pystan
model_code = 'parameters {real y;} model {y ~ normal(0,1);}'
if __name__ == "__main__":
    model = pystan.StanModel(model_code=model_code)
    y = model.sampling().extract()['y']
    print(y.mean()) # with luck the result will be near 0

My output is the same as it is when I run it directly in the live Python shell.

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_5944b02c79788fa0db5b3a93728ca2bf NOW.

GGGGradient evaluation took 0 secondsradient evaluation took 0 secondsradient evaluation took 0 secondsradient evaluation took 0 seconds

1111000 transitions using 10 leapfrog steps per transition would take 0 seconds.000 transitions using 10 leapfrog steps per transition would take 0 seconds.000 transitions using 10 leapfrog steps per transition would take 0 seconds.000 transitions using 10 leapfrog steps per transition would take 0 seconds.

AAAAdjust your expectations accordingly!djust your expectations accordingly!djust your expectations accordingly!djust your expectations accordingly!

IIIIteration:    1 / 2000 [  0%]  (Warmup)teration:    1 / 2000 [  0%]  (Warmup)teration:    1 / 2000 [  0%]  (Warmup)teration:    1 / 2000 [  0%]  (Warmup)

Iteration:  200 / 2000 [ 10%]  (Warmup)
Iteration:  200 / 2000 [ 10%]  (Warmup)
Iteration:  200 / 2000 [ 10%]  (Warmup)
Iteration:  200 / 2000 [ 10%]  (Warmup)
Iteration:  400 / 2000 [ 20%]  (Warmup)
Iteration:  400 / 2000 [ 20%]  (Warmup)I
teration:  400 / 2000 [ 20%]  (Warmup)
IIIteration:  400 / 2000 [ 20%]  (Warmup)teration:  600 / 2000 [ 30%]  (Warmup)teration:  600 / 2000 [ 30%]  (Warmup)

Iteration:  600 / 2000 [ 30%]  (Warmup)
Iteration:  600 / 2000 [ 30%]  (Warmup)
Iteration:  800 / 2000 [ 40%]  (Warmup)
Iteration:  800 / 2000 [ 40%]  (Warmup)
Iteration:  800 / 2000 [ 40%]  (Warmup)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
IIteration:  800 / 2000 [ 40%]  (Warmup)teration: 1001 / 2000 [ 50%]  (Sampling)

Iteration: 1000 / 2000 [ 50%]  (Warmup)
IIteration: 1001 / 2000 [ 50%]  (Sampling)teration: 1000 / 2000 [ 50%]  (Warmup)

Iteration: 1001 / 2000 [ 50%]  (Sampling)
Iteration: 1000 / 2000 [ 50%]  (Warmup)
IIteration: 1001 / 2000 [ 50%]  (Sampling)teration: 1200 / 2000 [ 60%]  (Sampling)

Iteration: 1200 / 2000 [ 60%]  (Sampling)
IIIIteration: 1200 / 2000 [ 60%]  (Sampling)teration: 1200 / 2000 [ 60%]  (Sampling)teration: 1400 / 2000 [ 70%]  (Sampling)teration: 1400 / 2000 [ 70%]  (Sampling)

Iteration: 1400 / 2000 [ 70%]  (Sampling)
Iteration: 1400 / 2000 [ 70%]  (Sampling)
IIIteration: 1600 / 2000 [ 80%]  (Sampling)teration: 1600 / 2000 [ 80%]  (Sampling)teration: 1600 / 2000 [ 80%]  (Sampling)


Iteration: 1600 / 2000 [ 80%]  (Sampling)
Iteration: 1800 / 2000 [ 90%]  (Sampling)
IIteration: 1800 / 2000 [ 90%]  (Sampling)teration: 1800 / 2000 [ 90%]  (Sampling)

Iteration: 2000 / 2000 [100%]  (Sampling)

III teration: 1800 / 2000 [ 90%]  (Sampling)teration: 2000 / 2000 [100%]  (Sampling)teration: 2000 / 2000 [100%]  (Sampling)Elapsed Time: 0.016 seconds (Warm-up)

   I              0.014 seconds (Sampling)Elapsed Time: 0.018 seconds (Warm-up)Elapsed Time: 0.018 seconds (Warm-up)teration: 2000 / 2000 [100%]  (Sampling)

              0.03 seconds (Total)              0.017 seconds (Sampling)              0.016 seconds (Sampling)

Elapsed Time: 0.019 seconds (Warm-up)

              0.035 seconds (Total)              0.034 seconds (Total)

              0.018 seconds (Sampling)


               0.037 seconds (Total)

-0.06891431773359

Process returned 0 (0x0)        execution time : 38.581 s```

@ilovejs
Copy link

ilovejs commented Jun 25, 2019

D:\Anaconda3\Library\bin

pypa/virtualenv#1139

@daewonyoon
Copy link

daewonyoon commented Jul 10, 2019

having the same issue on

  • windows 10 64bit
  • vanilla (no anaconda) python3.6 64bit
  • into a venv virtualenvironment
  • pystan installed via pip, installed version pystan-2.19.0.0-cp36-cp36m-win_amd64.whl

  • can reproduce the error by typing
import pystan
  • the error message is
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\PyEnvs\anomaly3664\lib\site-packages\pystan\__init__.py", line 9, in <module>
    from pystan.api import stanc, stan
  File "D:\PyEnvs\anomaly3664\lib\site-packages\pystan\api.py", line 13, in <module>
    import pystan._api  # stanc wrapper
ImportError: DLL load failed:  The specified module could not be found.

checked the error using procmon, and the log seems to indicate the failure is caused by missing

  • libgcc_s_seh-1.dll
  • libsstdc++-6.dll
    which are dependencies of _api.cp36-win_amd64.pyd on site-packages/pystan.

pystan 2.18.0 also has this issue.
pystan 2.17.1 doesn't have this issue.

@ahartikainen
Copy link
Collaborator

On Windows you need conda version of python (you need mingw-w64 + libpython from conda)

See https://pystan.readthedocs.io/en/latest/windows.html

@jorgecastillo2
Copy link

For me, the best answer in this thread: https://stackoverflow.com/questions/14372706/visual-studio-cant-build-due-to-rc-exe worked: you must copy and paste rc.exe and rcdll.dll

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants