17

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

sshashank124
  • 31,495
  • 9
  • 67
  • 76
Jérémy
  • 340
  • 1
  • 3
  • 13
  • check this link https://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. – vishal May 17 '18 at 13:20
  • I suffer from the same error and have asked for help at https://github.com/statsmodels/statsmodels/issues/4654 if you'd like to follow it. If we resolve it, I'll post an answer here. – mightypile May 17 '18 at 22:13
  • 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. – mightypile May 18 '18 at 11:42
  • my OS is ubuntu debian9 i install statsmodels with pip3 install statsmodels -- user – Jérémy May 18 '18 at 12:01
  • 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. – mightypile May 18 '18 at 12:19

3 Answers3

17

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
mightypile
  • 7,589
  • 3
  • 37
  • 42
  • Thanks a lot I will try that as soon as a can and keep you post !=) – Jérémy May 18 '18 at 09:08
  • 3
    sorry but even after that résult is all the same... :( – Jérémy May 18 '18 at 09:35
  • 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. – mightypile May 18 '18 at 11:54
  • I use Ubutu debian 9 and i use pip3, i'm in 3.5.3 version. Do need other informations? – Jérémy May 18 '18 at 12:09
  • 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 9 – Jérémy May 21 '18 at 07:52
5

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

pip3 install git+https://github.com/statsmodels/statsmodels.git
Shinto Joseph
  • 2,809
  • 27
  • 25
2

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

Nikhil Gupta
  • 1,436
  • 12
  • 15