0

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.

user4157124
  • 2,809
  • 13
  • 27
  • 42
nac001
  • 693
  • 2
  • 9
  • 18
  • 1
    Are you sure that `sklearn` is installed in the same environment as you are working in? Check with `!pip freeze` from your jupyter notebook and see if you have `scikit-learn` there. – CypherX Sep 17 '19 at 03:55
  • Did you `pip install` for `sklearn` in a `conda` environment by any chance? This may cause an improper installation. https://scikit-learn.org/stable/install.html#canopy-and-anaconda-for-all-supported-platforms – CypherX Sep 17 '19 at 03:59
  • He's using conda and specifically states, the package was installed with conda. [Managing environments: Conda](https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html). `conda env list` will give you a list of your conda environments. Make sure you activate the correct one with `conda activate myenv` and deactive with `conda deactivate` – Trenton McKinney Sep 17 '19 at 03:59
  • @Trenton_M is right. I have not used `pip install` yet. Also, I have the right environment loaded. I can see `scikit-learn 0.21.2 py37h27c97d8_0` with `conda list`. – nac001 Sep 17 '19 at 04:07
  • 2
    Possible duplicate of [ModuleNotFoundError: No module named 'sklearn'](https://stackoverflow.com/questions/46113732/modulenotfounderror-no-module-named-sklearn) – Trenton McKinney Sep 17 '19 at 04:12
  • 1
    I checked the kernel specifications for jupyter notebook and it was using Python3 installed via brew (I am on MacOS). Fixed after I changed it to the conda version. – nac001 Sep 17 '19 at 15:35

5 Answers5

3

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).

nac001
  • 693
  • 2
  • 9
  • 18
0

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)

Christian Seitz
  • 728
  • 6
  • 15
0

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.

0

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

thehand0
  • 1,123
  • 4
  • 14
0

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!

Mina Abd El-Massih
  • 636
  • 1
  • 8
  • 13
  • The OP was on a Mac. For something not platform specific when installing packages, in a cell in your notebook you can use `%pip install ` or `%conda install `. For example, here it'd be `%pip install scikit-learn`. That allows the newer magics commands that insure installation goes to the environment backing the current notebook, see [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez) for more about that. Usually you can leave off the `%` & let automagics handle it; avoid use of `!` from now on for pip/conda. – Wayne Mar 14 '22 at 16:11