python 3.5 in statsmodels ImportError: cannot import name '_representation'
Asked Answered
U

3

18

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 =)

Unmoved answered 17/5, 2018 at 12:47 Comment(6)
check this link github.com/statsmodels/statsmodels/issues/3527 . You may be running statsmodel with wrong version of numpy/scipy/python. Install it in a new virtualenvironment and see if it works.Sufflate
I suffer from the same error and have asked for help at github.com/statsmodels/statsmodels/issues/4654 if you'd like to follow it. If we resolve it, I'll post an answer here.Gilemette
Since '_representation' is a cython-compiled file, it could be relevant and helpful to edit your question and describe your machine, OS, and method of installation (pip, easy_install, anaconda, etc). You could also look in your /home/mlv/.local/lib/python3.5/site-packages/statsmodels/tsa/statespace directory for other '_*' files to see if any are being built.Gilemette
my OS is ubuntu debian9 i install statsmodels with pip3 install statsmodels -- userSmoke
I'm trying to understand the python path your error reports. On my ubuntu 16 laptop, they are all '/usr/lib/...' and '/usr/local/lib/...'. But on my debian server, they have a mix of '/usr/lib/...' and '/usr/local/lib/...' and '/home/mike/.local/lib/...' like yours. I'll edit one more thing into the answer for you to try.Gilemette
I came misguided here because my import was in uppercase instead of lowercase: #63378245Sharolynsharon
G
18

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
Gilemette answered 18/5, 2018 at 3:22 Comment(5)
Thanks a lot I will try that as soon as a can and keep you post !=)Smoke
sorry but even after that résult is all the same... :(Smoke
I've tested this several times in a singularity container, and in virtual environments, and it works in every case I've tested. But I'm happy to edit the answer, dependent on your configuration, if you can add more details.Gilemette
I use Ubutu debian 9 and i use pip3, i'm in 3.5.3 version. Do need other informations?Smoke
thanks i have an additinal problème ^^ my pip3 doesn't work since i update it 10.1? seems to be a ptobleme with my debian 9Smoke
B
5

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

pip3 install git+https://github.com/statsmodels/statsmodels.git
Behlke answered 7/3, 2019 at 9:46 Comment(3)
Thanks. Worked for me. It is also suggested as a solution here: github.com/statsmodels/statsmodels/issues/5572Deel
I had to re-start the Jupyter kernel. Then this solution worked.Deel
The only solution that worked for me. Updated to latest at the time which was version 14.0.Mellicent
E
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.

Updating Package

Eley answered 30/4, 2019 at 0:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.