Droter
Droter

Reputation: 313

Jupyter Notebook ImportError: No module named 'sklearn'

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?

Upvotes: 15

Views: 49407

Answers (8)

Mariya Ivanova
Mariya Ivanova

Reputation: 15

This was enough for me:

pip install scikit-learn

Upvotes: 0

ANAND YADAV
ANAND YADAV

Reputation: 11

As you are Anaconda/Miniconda, while installing the package you should use conda install command instead of pip install. More precisely you need to use the below command (for jupyter notebook):

!conda install scikit-learn -y

After that you can use sklearn in you upcoming commands.

If you still face the issue of 'sklearn' not found. You can try closing the environment you are using (like, jupyter) and restart it. This can help your environment access the package if it wasn't able to before.

Upvotes: 0

Gnostist
Gnostist

Reputation: 1

I tried almost everything and this finally worked:

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

Upvotes: 0

hafiz031
hafiz031

Reputation: 2720

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

Upvotes: 5

dogukanakgol
dogukanakgol

Reputation: 19

You can install library on which enviromment you use

pip install sklearn

conda install sklearn

Upvotes: -1

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

pip install notebook

Upvotes: 2

Bashar Altakrouri
Bashar Altakrouri

Reputation: 10595

Updating the package may solve your issue

conda upgrade scikit-learn

Upvotes: 1

minrk
minrk

Reputation: 38638

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.

Upvotes: 7

Related Questions