python 3.5 in statsmodels ImportError: cannot impo

2020-07-03 04:48发布

问题:

I cannot manage to import statsmodels.api correctly when i do that I have this error:

File "/home/mlv/.local/lib/python3.5/site-packages/statsmodels/tsa/statespace/tools.py", line 59, in set_mode from . import (_representation, _kalman_filter, _kalman_smoother, ImportError: cannot import name '_representation'

I already try to re-install or update it, that does not change. plese i need help =)

回答1:

Please see the github report for more detail.

It turns out that statsmodels is dependent upon several packages being installed before it so that it can key on them to compile its own modules. I don't completely understand the dependencies, or why they aren't specified in the package's setup, but this solves the problem for me.

If you need to clean out what you already have, you can uninstall with the following:

pip3 uninstall statsmodels

then make sure your dependencies are there

pip3 install numpy scipy patsy pandas

then, only after these four are installed first:

pip3 install statsmodels

Then move on with your imports and code.

==== additionally / alternately =====

It is recommended to use virtualenv in most cases. It also would allow you to create your own environments where you can control your own libraries. You can create all you want, and name them whatever you like for each project. It is likely that you are now using a mix of python modules installed at the system level and the user level, and they could change out from under you when the system packages are updated. It's possible you have a system version of scipy that conflicts with a newer user version of statsmodels. For python 3.5, you have to install venv; but with 3.6 it becomes part of the distribution.

First, look at your system paths from when you just run python3.

python3
>>> import sys
>>> print(sys.path)
>>> quit()

And then create a clean, independent environment and do the same.

sudo apt install python3-venv
python3 -m venv ~/name_me
source ~/name_me/bin/activate
python3
>>> import sys
>>> print(sys.path)
>>> quit()

It should have paths to base libaries, but avoid paths to the installed additional packages. You have a clean environment to install them into. Then, from within this virtualenv, which you should be able to detect by your changed shell prompt, you can do the pip installs from before and see if they work.

pip install numpy scipy patsy pandas
pip install statsmodels
python
>>> import statsmodels.api as sm

And when you are done, you can exit the virtualenv

deactivate


回答2:

The issue was solved for me by installing the gihub repository version of statsmodels,

pip3 install git+https://github.com/statsmodels/statsmodels.git


回答3:

You can simply install the package again using Anaconda

conda install statsmodels

If there are packages that need to be adjusted, they you will be prompted automatically (see below). I was able to resolve the issue this way.