ImportError: No module named statsmodels
Asked Answered
W

7

45

I downloaded the StatsModels source from this location.

Then untarred to

/usr/local/lib/python2.7/dist-packages

and per this documentation, did this

sudo python setup.py install

It installed but when I try to import

import statsmodels.api as sm

I get the following error

Traceback (most recent call last):
  File "/home/Astrophysics/Histogram_Fast.py", line 6, in <module>
import statsmodels.api as sm
ImportError: No module named statsmodels.api

I read a few post with a similar problem and checked that setuptools was installed and it was also in

/usr/local/lib/python2.7/dist-packages

I'm kinda of lost on this and would appreciate any help...

I am also running

numpy 1.6

so thats not the problem.

Westberg answered 3/8, 2012 at 3:35 Comment(0)
C
61
  • you shouldn't untar it to /usr/local/lib/python2.7/dist-packages (you could use any temporary directory)
  • you might have used by mistake a different python executable e.g., /usr/bin/python instead of the one corresponding to /usr/local/lib/python2.7

You should use pip corresponding to a desired python version (use python -V to check the version) to install it:

$ python -m pip install statsmodels 

It would allow you to upgrade/uninstall it easily.

Don't install as a root to avoid the risk of messing with system python installation by accident. You could use --user option or virtualenv instead.

Caprine answered 9/8, 2012 at 19:34 Comment(3)
The problem was where I un tarred itWestberg
great answer. but why pip3 install statsmodels installs the module but then inside python3 it cannot be imported? I only managed to solve this problem using python 3-m pip install statsmodels .Supreme
@makis pip3 may use a different from python3 executable. There may be several python3 binaries installed on a computer.Caprine
A
3

Install statsmodels:

For Window users:

pip install statsmodels

For Mac users:

pip3 install statsmodels

Alyssa answered 22/3, 2022 at 20:24 Comment(0)
E
2

As per documentation, you can install using the following commands

pip install --upgrade --no-deps statsmodels

using conda

conda install statsmodels

Then restart your jupyter notebook

if you facing an error like no module pasty stop your jupyter notebook and try the below command on terminal

pip install patsy

Don't forget to restart your jupyter notebook Let's hope

Elzaelzevir answered 19/1, 2020 at 23:13 Comment(1)
In some cases, dependencies keep the version outdated. pip install --upgrade --no-deps statsmodels is the way to get the latest version.Bentley
S
0

Install patsy:

pip install --upgrade patsy

Install statsmodels

pip install statsmodels

docs here

Scold answered 20/1, 2018 at 19:17 Comment(2)
pip install statsmodels will automatically install/upgrade patsy.. isn't it?Layby
In my case it didn't and the error output told me to install patsy, which worked.Scold
P
0

You have to upgrade the package pytest. I had the same issue importing in a Jupyter notebook on Ubuntu python 2.7

import statsmodels.api as sm

I also had to restart the Jupyter notebook.

sudo python -m pip install pytest --upgrade

Pyrite answered 27/4, 2019 at 9:34 Comment(0)
D
0

Try installing using the path directly,

For example, pip install --user "downloaded_package_path"

Then try importing the statsmodels, import statsmodels.api as sm

Dogie answered 4/7, 2019 at 17:37 Comment(0)
B
0

Until 2022 many things changed, but if somebody have the same problem, here is what worked for me: on the left side of VS Code, there are many icons (explore, search etc). The last one is Pip manager. Click on it and search for the module to install it :) Much better than 9 years ago haha

Ballroom answered 18/3, 2022 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.