Cannot import scikits-learn even though it seems to be installed
Asked Answered
C

5

29

Per the scikit-learn user guide, I installed scikit-learn using pip install -U scikit-learn.

So using pip search scikit-learn, I get this search result:

scikit-learn - A set of python modules for machine learning and data mining
INSTALLED: 0.12.1 (latest)

But when I go into Python and try to import sklearn, I get an ImportError: No module named sklearn. This really should have just worked.

I am using Enthought's free distribution of Python (2.7.3) on a Mac OS 10.6.8 with NumPy 1.6.1 and SciPy 0.10.1. Yes, I'm aware that EPD Free comes with scikit-learn but pip should have upgraded my version so that I can actually use scikit-learn.

Corkage answered 3/11, 2012 at 20:3 Comment(4)
Do you have more than one Python installed? The python you call pip with is the python that gets access to the module.Pappose
python --version from the command line and import sys; sys.version in the interactive enviroment. The command line version should give you the version pip used.Rejuvenate
@tcaswell Definitely using Python 2.7.3 which is provided with EPD Free. When I do a printenv, there does seem to be more than one Python in PATH, but the EPD Free version is earlier in PATH and should trump the other version.Corkage
find where ever pip installed the module, and make sure that path is in sys.pathRejuvenate
C
14

Thanks folks (see comment thread under the question)! It turns out that I have two versions of Python under my Mac's /Library/Frameworks/Python.framework directory tree: 2.7 (came with OSX) and 7.3 (installed by EPD Free).

It turns out pip put scikit-learn under 2.7 when I really wanted it under 7.3.

Changing directories to site-packages under the desired version of Python, and invoking pip install -t . {package-name} suffices to install package-name under the desired version of Python.

POSTLUDE (Feb 2021): Looking back on this question from almost 9 years ago, I think I was running into a version of this XKCD comic.

dependency management hell in python

Nowadays, for dependency management of Python data engineering libraries like scikit, I'm happy with just using conda and pyenv.

Corkage answered 3/11, 2012 at 22:34 Comment(1)
could you elaborate on the command to be executed? It doesnt seem to work for me,and I think I have the same problem.Tricky
B
30

Got same problem, @Alan gave correct solution but hard way. Here are easy steps to resolve issue, as i am on mac osx, giving steps for same.

Ameys-Mac-mini:~ amey$ python --version
Python 2.7.2
Ameys-Mac-mini:~ amey$ cd /Library/Python/2.7/site-packages/
Ameys-Mac-mini:site-packages amey$ brew install gcc
Ameys-Mac-mini:site-packages amey$ sudo pip install -t . numpy scipy scikit-learn 
Bartizan answered 21/1, 2015 at 8:46 Comment(4)
I get must supply either home or prefix/exec-prefix -- not bothVries
@javadba, please follow this question you will get your solution . #41641435Bartizan
If you want to install it for a specific python version, do sudo python2.7 -m pip install -t . scikit-learn or use environments.Opposition
I really wouldn't advise sudo pip installing anything. A year from now you'll kick yourself. Install without sudo to a virtualenv or a conda environment instead.Gospodin
C
14

Thanks folks (see comment thread under the question)! It turns out that I have two versions of Python under my Mac's /Library/Frameworks/Python.framework directory tree: 2.7 (came with OSX) and 7.3 (installed by EPD Free).

It turns out pip put scikit-learn under 2.7 when I really wanted it under 7.3.

Changing directories to site-packages under the desired version of Python, and invoking pip install -t . {package-name} suffices to install package-name under the desired version of Python.

POSTLUDE (Feb 2021): Looking back on this question from almost 9 years ago, I think I was running into a version of this XKCD comic.

dependency management hell in python

Nowadays, for dependency management of Python data engineering libraries like scikit, I'm happy with just using conda and pyenv.

Corkage answered 3/11, 2012 at 22:34 Comment(1)
could you elaborate on the command to be executed? It doesnt seem to work for me,and I think I have the same problem.Tricky
G
7

In my case, my python extension has installed all in

/Library/Python/2.7/site-packages/ 

After I use the command given by the official site

pip install -U numpy scipy scikit-learn

the scikit-learn package was actually installed in my

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

So I copied all the packages in second to the first directory, then I can import the sklearn package.

Grier answered 28/4, 2015 at 3:44 Comment(0)
P
0

For future reference:

easy_install -U statsmodels

Palomo answered 14/11, 2015 at 1:15 Comment(0)
A
0

I had the same issue and I could figure out that python versions were different. I followed this answer.

On Zeppelin page, go to the settings, Intepreter settings and change your python to python3.

Avant answered 25/1, 2021 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.