Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModuleNotFoundError: No module named 'sklearn'

I want to import sklearn but there is no module apparently:

ModuleNotFoundError: No module named 'sklearn'

I am using Anaconda and Python 3.6.1; I have checked everywhere but still can't find answers.

When I use the command: conda install scikit-learn should this not just work?
Where does anaconda install the package?

I was checking the frameworks in my python library and there was nothing about sklearn only numpy and scipy.

Please help, I am new to using python packages especially via anaconda.

like image 401
Hareez Rana Avatar asked Sep 08 '17 09:09

Hareez Rana


People also ask

How do you fix ModuleNotFoundError No module named Sklearn?

The Python "ModuleNotFoundError: No module named 'sklearn'" occurs when we forget to install the scikit-learn module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install scikit-learn command.

Why Sklearn is not working?

Every time you install a package, this installation is associated with just a single version. Therefore, there's a chance that you have installed scikit-learn for one Python version, but you are executing your source code using a different version and this may be the reason why scikit-learn cannot be found.


6 Answers


Introduction


When using Anaconda, one needs to be aware of the environment that one is working.

Then, in Anaconda Prompt one needs to run the following

conda $command -n $ENVIRONMENT_NAME $IDE/package/module

$command - Command that one intends to use (consult documentation for general commands)

$ENVIRONMENT NAME - The name of one's environment (if one is working in the root, conda $command $IDE/package/module is enough)

$IDE/package/module - The name of the IDE or package or module


Solution


Will leave below two options that may help one solve the problem:

Option 1

If one wants to install it in the root and one follows the requirements - (Python (>= 2.7 or >= 3.4), NumPy (>= 1.8.2), SciPy (>= 0.13.3).) - the following will solve the problem:

conda install scikit-learn

Let's say that one is working in the environment with the name ML.

Then the following will solve one's problem:

conda install -n ML scikit-learn

Note: If one needs to install/update packages, the logic is the same as mentioned in the introduction. If you need more information on Anaconda Packages, check the documentation.


Option 2

If the above doesn't work, on Anaconda Prompt one can also use pip (here's how to pip install scikit-learn) so the following may help

pip install scikit-learn

Notes: pip doesn't manage dependencies the same way conda does and can, potentially, damage one's installation.

like image 31
Gonçalo Peres Avatar answered Oct 23 '22 18:10

Gonçalo Peres


You can just use pip for installing packages, even when you are using anaconda:

pip install -U scikit-learn scipy matplotlib

This should work for installing the package.

And for Python 3.x just use pip3:

pip3 install -U scikit-learn scipy matplotlib
like image 149
mrCarnivore Avatar answered Oct 23 '22 18:10

mrCarnivore


If you are using Ubuntu 18.04 or higher with python3.xxx then try this command

$ sudo apt install python3-sklearn 

then try your command. hope it will work

like image 17
yuvraj sune Avatar answered Oct 23 '22 20:10

yuvraj sune


I did the following:

import sys
!{sys.executable} -m pip install sklearn
like image 16
cesareressa Avatar answered Oct 23 '22 18:10

cesareressa


I've tried a lot of things but finally, including uninstall with the automated tools. So, I've uninstalled manually scikit-learn.

sudo rm -R /home/ubuntu/.local/lib/python3.6/site-packages/sklearn
sudo rm -R /home/ubuntu/.local/lib/python3.6/site-packages/scikit_learn-0.20.0-py3.6.egg-info

And re-install using pip

sudo pip3.6 install -U scikit-learn

Hope that can help someone else!

like image 3
Claude COULOMBE Avatar answered Oct 23 '22 20:10

Claude COULOMBE


This happened to me, I tried all the possible solutions with no luck!

Finaly I realized that the problem was with Jupyter notebook environment, not with sklearn!

I solved the problem by re-installing Jupyter at the same environment as sklearn

the command is: conda install -c anaconda ipython. Done...

like image 3
Mohammad ElNesr Avatar answered Oct 23 '22 19:10

Mohammad ElNesr