ModuleNotFoundError: No module named 'sklearn'

392,272

Solution 1

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

Solution 2


Brief 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

Solution 3

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

Solution 4

I did the following:

import sys
!{sys.executable} -m pip install sklearn

Solution 5

SOLVED:

The above did not help. Then I simply installed sklearn from within Jypyter-lab, even though sklearn 0.0 shows in 'pip list':

!pip install sklearn
import sklearn

What I learned later is that pip installs, in my case, packages in a different folder than Jupyter. This can be seen by executing:

import sys
print(sys.path)

Once from within Jupyter_lab notebook, and once from the command line using 'py notebook.py'.

In my case Jupyter list of paths where subfolders of 'anaconda' whereas Python list where subfolders of c:\users[username]...

Share:
392,272
Admin
Author by

Admin

Updated on January 12, 2022

Comments

  • Admin
    Admin over 2 years

    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.

  • Sterls
    Sterls almost 5 years
    Clear your cache
  • San
    San over 4 years
    try appending sudo like below command. It worked for me sudo pip3 install -U scikit-learn
  • keramat
    keramat almost 4 years
    What is the meaning of -U?
  • Quicksilver
    Quicksilver almost 4 years
    @keramat -U means upgrade, so it will install the newest version if you have an older version (see stackoverflow.com/q/12435209/11063448)
  • EgoPingvina
    EgoPingvina about 3 years
    It did not help, any other ideas?
  • Upulie Han
    Upulie Han almost 3 years
    to see which version of python you have, python --version
  • JoErNanO
    JoErNanO over 2 years
    This. This worked like a charm. I had a fresh conda installation on W10. Installed tensorflow in a dedicated environment. Installed scikit-learn in the same environment. Could not import it. Ran conda install -c anaconda ipython in the environment and fixed the problem.
  • Mohammad ElNesr
    Mohammad ElNesr over 2 years
    Thank you @JoErNanO for your feedback.
  • Moureen Caroline
    Moureen Caroline about 2 years
    This worked for me