How do I import scikit-learn in a jupyter notebook?

Question:

I created a fresh conda environment for using scikit-learn and used
conda install <package> to install scikit-learn, jupyter, pandas, etc. for compatible dependencies..

I checked if sklearn was working after loading the environment:

$python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sklearn
>>>

Since import command didn’t throw errors, sklearn is ready for use. However, I am getting a ModuleNotFoundError while trying to import it in a jupyter notebook, that I am running from the same environment.

import sklearn
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-b7c74cbf5af0> in <module>()
----> 1 import sklearn

ModuleNotFoundError: No module named 'sklearn'

I was able to import numpy and pandas in the same notebook without any errors.

Please help me understand the problem and how to troubleshoot it.

Asked By: nac001

||

Answers:

Best practice: Install everything via conda or pip3, as mentioned in this answer.

If that didn’t work, check the system paths in jupyter notebook:

import sys
sys.path

and the system executable:

sys.executable

These must correspond to the python in your current loaded environment.

For me, the issue was with the jupyter Notebook’s kernel. See the kernel specifications in kernel.json file in the path. You can find the directory for this file from jupyter kernelspec list. I manually changed the python path to the python in my environment (this is a bad idea, but it worked).

Answered By: nac001

Make sure that your jupyter notebook is finding the same version of python as your terminal, otherwise installing modules with conda install in your terminal won’t show up in your notebook. Do

import sys

print(sys.version)

in your notebook and in your terminal. If they do not match up, then add your terminal’s python version to your notebook:

conda install nb_conda_kernels

conda install ipykernel

and then in the notebook switch to the kernel you just installed (kernel -> change kernel)

Answered By: Christian Seitz

Check your path for Python and Jupyter:

import sys
sys.path

You may find different results from Python and Jupyter. This can be fixed temporarily by appending this line of code if you are using macos:

sys.path.append('/Users/**your_user_name**/anaconda3/lib/python3.7/site-packages')

If you want to solve this permanently, you can create an iPython profile and fix it there.

Answered By: Haiwen20

First activate the finds environment, second launch jupyter notebook, third import sklearn. Inside jupyter notebook.

Answered By: thehand0

I have also found the same issue

ModuleNotFoundError: No module named ‘sklearn’

but after searching I found its best solution.

You should also try the following steps:

Step 1:
open "cmd"

Step 2:
write "pip install notebook"

Step 3:
After installation of notebook, write "jupyter notebook " in cmd.

Tell me if you got your solution
thank you!

Answered By: Aiman Ali

try running pip install sklearn command.

Answered By: Praveer Kumar