Scikit-learn: after update : ImportError: No module named model_selection on windows computer

Created on 13 Jan 2016  ·  17Comments  ·  Source: scikit-learn/scikit-learn

after successful updating with
conda update scikit-learn
on Windows computer
from sklearn.model_selection import GridSearchCV
or
from sklearn.model_selection import train_test_split
gives
ImportError: No module named model_selection
BUT, scikit-learn started work after update only after
conda install -f scipy
per
http://stackoverflow.com/questions/31012216/updated-anaconda-why-arent-my-ipython-notebooks-working-anymore
by the way this example is running ok
http://scikit-learn.org/stable/auto_examples/text/document_classification_20newsgroups.html#example-text-document-classification-20newsgroups-py

Most helpful comment

The model_selection module will be available from the next release (0.18) only. You will have to use the old cross_validation module!

All 17 comments

The model_selection module will be available from the next release (0.18) only. You will have to use the old cross_validation module!

Closing as this is not a bug.

ok, but may you pls help get working code for file exercise_02_sentiment.py
from http://scikit-learn.org/stable/tutorial/text_analytics/working_with_text_data.html
as written
The source of this tutorial can be found within your scikit-learn folder:
scikit-learn/doc/tutorial/text_analytics/

you can download the code from the stable doc website or checkout the 0.17.X branch in your repo.

This issue was closed in Jan 2016, but it is still relevant (Oct 2016). Solution:

## [2016-Oct-19] Python 3.5 virtual environment
(py35) [victoria@victoria ~]$ conda install scikit-learn
    ...
    The following NEW packages will be INSTALLED:
    scikit-learn: 0.18-np111py35_0
    ...
(py35) [victoria@victoria ~]$ conda list | grep scikit
    scikit-learn    0.18    np111py35_0 
## Python 3.5:
>>> from sklearn.model_selection import train_test_split
    Traceback (most recent call last):
      File "", line 1, in 
    ImportError: No module named 'sklearn.model_selection'
## shell:
$ conda install -f scipy
    ...
    The following NEW packages will be INSTALLED:
    scipy: 0.18.1-np111py35_0
    ...
## Python: same import error!! Grrrrrrr.......
## http://scikit-learn.org/stable/install.html
    "If you already have a working installation of numpy and scipy, the easiest way to install scikit-learn is using pip

        pip install -U scikit-learn

    or conda:

        conda install scikit-learn"
$ pip install -U scikit-learn
    Collecting scikit-learn
      Downloading scikit_learn-0.18-cp35-cp35m-manylinux1_x86_64.whl (11.3MB)
        100% |████████████████████████████████| 11.3MB 162kB/s 
    Installing collected packages: scikit-learn
      Found existing installation: scikit-learn 0.17.1
        DEPRECATION: Uninstalling a distutils installed project (scikit-learn) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
        Uninstalling scikit-learn-0.17.1:
          Successfully uninstalled scikit-learn-0.17.1
    Successfully installed scikit-learn-0.18
## Can NOW successfully import:
(py35) [victoria@victoria ~]$ P
      [P: python]
    Python 3.5.2 (default, Jun 28 2016, 08:46:01) 
    [GCC 6.1.1 20160602] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from sklearn.model_selection import train_test_split
    >>> 
    >>>  [Ctrl-D / exit]
(py35) [victoria@victoria ~]$  ## :-D

Q.E.D.

hmm, I don't seem to have the problem you have @victoriastuart . Are you sure that you are activating the environment / have python in the environment that you conda install scikit-learn in?

nfliu at kilauea in ~
$ conda create -n test python=3
Fetching package metadata .......
Solving package specifications: ..........

Package plan for installation in environment /Users/nfliu/miniconda2/envs/test:

The following NEW packages will be INSTALLED:

    openssl:    1.0.2j-0
    pip:        8.1.2-py35_0
    python:     3.5.2-0
    readline:   6.2-2
    setuptools: 27.2.0-py35_0
    sqlite:     3.13.0-0
    tk:         8.5.18-0
    wheel:      0.29.0-py35_0
    xz:         5.2.2-0
    zlib:       1.2.8-3

Proceed ([y]/n)? y

Linking packages ...
[      COMPLETE      ]|###################################################| 100%
#
# To activate this environment, use:
# > source activate test
#
# To deactivate this environment, use:
# > source deactivate test
#

nfliu at kilauea in ~
$ source activate test
(test) nfliu at kilauea in ~
$ conda install scikit-learn
Fetching package metadata .......
Solving package specifications: ..........

Package plan for installation in environment /Users/nfliu/miniconda2/envs/test:

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    numpy-1.11.2               |           py35_0         2.7 MB
    scipy-0.18.1               |      np111py35_0        12.3 MB
    scikit-learn-0.18          |      np111py35_0         4.6 MB
    ------------------------------------------------------------
                                           Total:        19.6 MB

The following NEW packages will be INSTALLED:

    mkl:          11.3.3-0
    numpy:        1.11.2-py35_0
    scikit-learn: 0.18-np111py35_0
    scipy:        0.18.1-np111py35_0

Proceed ([y]/n)? y

Fetching packages ...
numpy-1.11.2-p 100% |#############################################################################################################################################################################################| Time: 0:00:01   1.57 MB/s
scipy-0.18.1-n 100% |#############################################################################################################################################################################################| Time: 0:00:02   6.30 MB/s
scikit-learn-0 100% |#############################################################################################################################################################################################| Time: 0:00:01   2.66 MB/s
Extracting packages ...
[      COMPLETE      ]|################################################################################################################################################################################################################| 100%
Linking packages ...
[      COMPLETE      ]|################################################################################################################################################################################################################| 100%
(test) nfliu at kilauea in ~
$ python
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul  2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from sklearn.model_selection import train_test_split
>>>

@nelson-liu: thank you for your suggestion ... Yes: I was working in my Python 3.5 virtual environment the whole time (terminal; Python). Working now, per my answer. :-)

@victoriastuart Pretty sure this happened to you (see big red box):
http://scikit-learn.org/dev/install.html#canopy-and-anaconda-for-all-supported-platforms

Don't try to upgrade a package with conda that you installed with pip. it will not work. It's a setuptools bug.

@amueller -- Here's the thing: I'm in a CONDA installed venv: at the outset I 'm pretty sure sklearn was not present (I haven't used it previously in this venv, so it wouldn't have previously been installed, e.g. via pip; regardless, I always prefer conda over pip -- first, in my venv -- using pip as a last resort):

(py35) [victoria@victoria ~]$ conda install scikit-learn
Fetching package metadata .......
Solving package specifications: ..........
Package plan for installation in environment /home/victoria/anaconda3/envs/py35:
The following packages will be downloaded:
    package                    |            build
    ---------------------------|-----------------
    numpy-1.11.2               |           py35_0         6.1 MB
    scipy-0.18.1               |      np111py35_0        30.4 MB
    scikit-learn-0.18          |      np111py35_0        10.8 MB
    theano-0.8.2               |           py35_0         3.7 MB
    ------------------------------------------------------------
                                           Total:        51.0 MB
The following NEW packages will be INSTALLED:
    scikit-learn: 0.18-np111py35_0  
The following packages will be UPDATED:
    astropy:      1.1.2-np110py35_0  --> 1.2.1-np111py35_0 
    bottleneck:   1.0.0-np110py35_1  --> 1.1.0-np111py35_0 
    matplotlib:   1.5.1-np110py35_0  --> 1.5.1-np111py35_0 
    numpy:        1.10.4-py35_2      --> 1.11.2-py35_0     
    scipy:        0.17.1-np110py35_1 --> 0.18.1-np111py35_0
    theano:       0.7.0-np110py35_0  --> 0.8.2-py35_0      
Proceed ([y]/n)? 
[... snip ...]

Note, above:

The following NEW packages will be INSTALLED: scikit-learn: 0.18-np111py35_0

So, in summary: per standard and sklearn-recommended (http://scikit-learn.org/stable/install.html) procedures, I install sklearn -- apparently installs OK (no warnings), then try to use it: fails to import with esoteric error message >> Google search >> this page. I appreciate all the minutiae about this or that (sklearn viz-a-viz NumPy or SciPy, ...) , but the main point is that as a user, I needed to work to resolve this issue, that has apparently (top of thread) been ongoing for some users.

the issue was actually that a stackoverflow answer referenced code that was not published to conda/pip yet (we just did so 2 weeks ago, so naturally the code didn't work on conda/pip-installed versions until then). Your issue seems a bit unrelated, but I'm glad you got it working regardless.

@nelson-liu: lol thank you -- and all the people involved in this project/development. I know it's insanely complicated, and that complications such as those reported here are an inevitable fact-of-life. Anyway my sklearn install appears to be working now, as summarized above. [I'll begin using it in a few hours.] Hopefully the discussion above will inform/help others. Again, much appreciation! :-)

@victoriastuart im presuming you don't have the conda environment around anymore/ can't reproduce it again, but I'd be curious to see what:

>>> import sklearn
>>> sklearn.__version__

would yield (i'm expecting that it's not what we think it is, namely not 0.18).
I suspect it's an issue with the $PYTHONPATH and perhaps drawing scikit-learn from some other location; fixing such an error would hopefully avoid such errors down the line with other packages.

@nelson-liu : sure, no problem! ;-)

----------------------------
PYTHON 3.5 VENV (py35 | p3):
----------------------------
    [victoria@victoria Vancouver]$ p3
      [Anaconda Python 3.5 venv (source activate py35)]
    (py35) [victoria@victoria Vancouver]$ which python
        /home/victoria/anaconda3/envs/py35/bin/python
    (py35) [victoria@victoria Vancouver]$ conda list | egrep -wi 'scikit|numpy|scipy'
        numpy                     1.11.2                   py35_0
        scikit-learn              0.18                np111py35_0
        scipy                     0.18.1              np111py35_0
    (py35) [victoria@victoria Vancouver]$ pip list | egrep -wi 'scikit|numpy|scipy'
        numpy (1.11.1)
        scikit-image (0.12.3)
        scikit-learn (0.18)
        scipy (0.17.0)
    (py35) [victoria@victoria Vancouver]$ P
          [P: python]
        Python 3.5.2 (default, Jun 28 2016, 08:46:01)
        [GCC 6.1.1 20160602] on linux
        Type "help", "copyright", "credits" or "license" for more information.
        >>> from sklearn.model_selection import train_test_split
        >>> import sklearn
        >>> sklearn.__version__
            '0.18'
        >>> quit()
    (py35) [victoria@victoria Vancouver]$ sd
      [Anaconda venv deactivate (source deactivate)]
    [victoria@victoria Vancouver]$
---------------------------------------------
HOST (NON-VIRTUAL) ENVIRONMENT (PYTHON 3.5):
---------------------------------------------
    [victoria@victoria Vancouver]$ which python
        /usr/bin/python
    [victoria@victoria Vancouver]$ conda list | egrep -wi 'scikit|numpy|scipy'
        numpy                     1.11.1                    
        numpy                     1.10.4                   py35_1
        scikit-image              0.12.3              np110py35_0
        scikit-learn              0.18                      
        scikit-learn              0.17.1              np110py35_0
        scipy                     0.17.0              np110py35_2
    [victoria@victoria Vancouver]$ pip list | egrep -wi 'scikit|numpy|scipy'
    [victoria@victoria Vancouver]$ P
          [P: python]
        Python 3.5.2 (default, Jun 28 2016, 08:46:01)
        [GCC 6.1.1 20160602] on linux
        Type "help", "copyright", "credits" or "license" for more information.
        >>> sklearn.__version__
        Traceback (most recent call last):
          File "", line 1, in 
        NameError: name 'sklearn' is not defined
        >>> import sklearn
        Traceback (most recent call last):
          File "", line 1, in 
        ImportError: No module named 'sklearn'
        >>>
----------------------------
Python 2.7 VENV (py27 | p2):
----------------------------
    [victoria@victoria Vancouver]$ p2
      [Anaconda Python 2.7 venv (source activate py27)]
    (py27) [victoria@victoria Vancouver]$ conda list | egrep -wi 'scikit|numpy|scipy'
        numpy                     1.11.1                    
        numpy                     1.10.4                   py27_1
        scikit-image              0.12.3              np110py27_0
        scikit-learn              0.17.1              np110py27_0
        scipy                     0.17.1                    
        scipy                     0.17.0              np110py27_0
    (py27) [victoria@victoria Vancouver]$ pip list | egrep -wi 'scikit|numpy|scipy'
        numpy (1.11.1)
        scikit-image (0.12.3)
        scikit-learn (0.17.1)
        scipy (0.17.1)
    (py27) [victoria@victoria Vancouver]$ P
          [P: python]
        Python 2.7.12 |Anaconda custom (64-bit)| (default, Jul  2 2016, 17:42:40)
        [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2
        Type "help", "copyright", "credits" or "license" for more information.
        Anaconda is brought to you by Continuum Analytics.
        Please check out: http://continuum.io/thanks and https://anaconda.org
        >>> import sklearn
        >>> sklearn.__version__
            '0.17.1'
        >>>
    (py27) [victoria@victoria Vancouver]$

AFAIK, my Python 2.7 venv would have been "similar" to my Python 3.5 venv, yesterday, prior to my install in that (py35) venv.

sorry, I meant the conda-installed scikit-learn that you were previously having problems with. This one is the pip installed one that you put over it to solve your problems. Since it seems like you can import train_test_split here, python is probably drawing from pip. I'm curious to see what it would be like if you uninstalled the things you installed via pip (thus reverting back to the state where you had the ImportError).

@nelson-liu : yes / oops! I realized that, immediately after I posted that output. Updated, now, above. ;-)

The python3.5 env you were drawing on still has scikit-learn from pip on it, which muddies the waters considerably. Not too sure what's happening on your machine, especially because you can't even import scikit-learn in your root env; I think that scikit-learn in the py35 env is being drawn from someplace else that you do not want it to be. I'd check your $PATH and $PYTHONPATH if you want to avoid future problems like this with other packages, but good to see that it's "working" anyway.

Ahhh .. good points! ;-)

Update: Hey -- good catch (clever, cever people out there; ;-) !! I've had problems with my $PYTHONPATH, arising from my use of PyCharm earlier this year as my Python IDE. To resolve those, I created a my_script.pth in my PyCharm root directory, that listed my PYTHONPATHS, with symlinks to that *.pth file in my conda-installed Pyhton package folders; e.g.

/home/victoria/.local/lib/python3.5/site-packages/my_scripts.pth
# symlink to: /home/victoria/PycharmProjects/scripts/my_scripts.pth

[I know: it's complicated, but I needed to do it.] Long story short I actually prefer Geany as my (multi-platform) IDE / editor, so I later replicated /updated those paths, e.g.

/home/victoria/.local/lib/python3.5/site-packages/my_scripts.pth
# symlink (now!) to: /home/victoria/GeanyProjects/Victoria/scripts/my_scripts.pth

My (Geany) my_scripts.pth ($PYTHONPATH) file was pointing to

/home/victoria/.local/lib/python2.7

... which, I believe, may have been the problem?

Was this page helpful?
0 / 5 - 0 ratings