13

I am trying to run on my local machine. I get an error ImportError: No module named 'sklearn' only in jupyter notebook It works fine when I use python from the command line both with the carnd-term1 env activated and deactivated.

I have installed sklearn with pip, apt-get and conda. Also tried conda upgrade scikit-learn. Both with the env active and deactivated.


(carnd-term1) matt@Malta:~/sdc$ conda upgrade scikit-learn
Fetching package metadata .........
Solving package specifications: .
# All requested packages already installed.
# packages in environment at /home/matt/anaconda3/envs/carnd-term1:
#
scikit-learn 0.18.1 np112py35_1

(carnd-term1) matt@Malta:~/sdc$ python3
Python 3.5.2 | packaged by conda-forge | (default, Jan 19 2017, 15:28:33) 
[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn
>>>

   ...: (carnd-term1) matt@Malta:~/sdc$ ipython
   ...: Python 3.5.2 | packaged by conda-forge | (default, Jan 19 2017, 15:28:33) 
   ...: Type "copyright", "credits" or "license" for more information.
   ...: 
   ...: IPython 5.1.0 -- An enhanced Interactive Python.
   ...: ?         -> Introduction and overview of IPython's features.
   ...: %quickref -> Quick reference.
   ...: help      -> Python's own help system.
   ...: object?   -> Details about 'object', use 'object??' for extra details.
   ...: 
   ...: In [1]: import sklearn
   ...: 
   ...: In [2]: from sklearn.model_selection import train_test_split
   ...: 
   ...: In [3]: (carnd-term1) matt@Malta:~/sdc$ ipython
   ...:    ...: Python 3.5.2 | packaged by conda-forge | (default, Jan 19 2017, 15:28:33) 
   ...:    ...: Type "copyright", "credits" or "license" for more information.
   ...:    ...: 
   ...:    ...: IPython 5.1.0 -- An enhanced Interactive Python.
   ...:    ...: ?         -> Introduction and overview of IPython's features.
   ...:    ...: %quickref -> Quick reference.
   ...:    ...: help      -> Python's own help system.
   ...:    ...: object?   -> Details about 'object', use 'object??' for extra details.
   ...:    ...: 
   ...:    ...: In [1]: import sklearn
   ...:    ...: 
   ...:    ...: In [2]: from sklearn.model_selection import train_test_split
   ...:    ...: 
   ...:    ...: In [3]:

Doesn't work from jupyter notebook.

Any ideas?

Droter
  • 193
  • 1
  • 1
  • 8

6 Answers6

7

This generally means that the two are not the same environment. The best thing to check is sys.executable and make sure that it is what you expect. If it's the notebook that's not using the sys.executable you expect, the first step may be to check your PATHs:

which jupyter
which jupyter-notebook

The most likely issue is that the notebook stack isn't in your conda env, which you can solve with:

conda install notebook

The second most likely is that you have installed a kernelspec (e.g. with ipython kernel install --user) that's overriding your env. You can see where your kernels are with:

jupyter kernelspec list

To make sure you have the IPython kernel installed in the same env, you can do:

conda install ipykernel
ipython kernelspec install --sys-prefix

and check jupyter kernelspec list again after.

minrk
  • 37,545
  • 9
  • 92
  • 87
  • 1
    I tried following your instruction and it didn't work eventhough all conditions where met. Then i realized that i was in the python3 virtual environment of my conda installation. So i exited that notebook. Checked the default python version (without the virtualenv) , it was python 2.7 . Fired up the REPL, followed by the jupyter notebook. BOOM was able to from sklearn. Leaving the comment here incase it helps someone – stormfield Oct 12 '18 at 23:36
  • I think I have the kernel issue but I'm not using conda, just plain old pip. What is the fix for that? – Evan Zamir Aug 28 '19 at 03:22
3

Let's learn a general approach to solve these sort of problems. The solution is quite straightforward. Basically has three steps:

  1. Finding where the pip package is installed.
  2. Adding that directory to path.
  3. Finally, importing the package.

Finding where the pip package is installed:

!pip show PACKAGE_NAME

Don't forget this ! before the command if you are executing it in jupyter-notebook. This will give you the path to that package (with possibly with other information). Get the path given inside Location.

Adding that directory to path: This following code should go before you import that package in jupyter.

import sys
sys.path.append('path/to/the/package')

Now import the package:

import PACKAGE_NAME

So, for sklearn:

Get the sklearn directory:

!pip show scikit-learn

Add directory:

import sys
sys.path.append('/path/to/sklearn')

For example, if you are using anaconda than the site-packages folder will contain all conda installed packages for that environment. Inside this path there is the folder sklearn which we are trying to import:

import sys
sys.path.append('/home/hafiz031/anaconda3/envs/NLP/lib/python3.8/site-packages')

Now import the desired package as usual:

import sklearn

References:

  1. which version and where scikit-learn is installed

  2. Python: Best way to add to sys.path relative to the current running script

hafiz031
  • 2,236
  • 3
  • 26
  • 48
2

If you use virtual environment, then you need to install Notebook into your environment:

pip install notebook
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
0

Updating the package may solve your issue

conda upgrade scikit-learn
Future2020
  • 9,939
  • 1
  • 37
  • 51
0

I tried almost everything and this finally worked:

Change your Jupiter notebook kernel from python3.xx.x (anything other than Ipykernel) to Ipykernel.

Rodalm
  • 5,169
  • 5
  • 21
-1

You can install library on which enviromment you use

pip install sklearn

conda install sklearn