[python] 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.

This question is related to python anaconda package conda

The answer is


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

install these ==>> pip install -U scikit-learn scipy matplotlib if still getting the same error then , make sure that your imoprted statment should be correct. i made the mistike while writing ensemble so ,(check spelling) its should be >>> from sklearn.ensemble import RandomForestClassifier


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


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


I did the following:

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


Brief Introduction


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

Then, in Anaconda Prompt (base) one needs to use the following code:

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

$command - Command that I intend to use (consult documentation for general commands)

$ENVIRONMENT NAME - The name of your 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


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.


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

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!


The other name of sklearn in anaconda is scikit-learn. simply open your anaconda navigator, go to the environments, select your environment, for example tensorflow or whatever you want to work with, search for scikit_learn in the list of uninstalled packages, apply it and then you can import sklearn in your jupyter.


I had the same issue as the author, and ran into the issue with and without Anaconda and regardless of Python version. Everyone's environment is different, but after resolving it for myself I think that in some cases it may be due to having multiple version of Python installed. Each installed Python version has its own \Lib\site-packages\ folder which can contain a unique set of modules for that Python version, and where the IDE looks into folder path that doesn't have scikit-learn in it.

One way to try solve the issue: you might clear your system of all other Python versions and their cached/temp files/system variables, and then only have one version of Python installed anywhere. Then install the dependencies Numpy and Scipy, and finally Scikit-learn.

More detailed steps:

  1. Uninstall all Python versions and their launchers (e.g. from Control Panel in Windows) except the one version you want to keep. Delete any old Python version folders in the Python directory --uninstalling doesn't remove all files.
  2. Remove other Python versions from your OS' Environment Variables (both under the system and user variables sections)
  3. Clear temporary files. For example, for Windows, delete all AppData Temp cache files (in C:\Users\YourUserName\AppData\Local\Temp). In addition, you could also do a Windows disk cleanup for other temporary files, and then reboot.
  4. If your IDE supports it, create a new virtual environment in Settings, then set your only installed Python version as the interpreter.
  5. In your IDE, install the dependencies Scipy and Numpy from the module list first, then install Scikit-Learn.

As some others have suggested, the key is making sure your environment is set up correctly where everything points to the correct library folder on your computer where the Sklearn package is located. There are a few ways this can be resolved. My approach was more drastic, but it turns out that I had a very messy Python setup on my system so I had to start fresh.


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


Cause

Conda and pip install scikit-learn under ~/anaconda3/envs/$ENV/lib/python3.7/site-packages, however Jupyter notebook looks for the package under ~/anaconda3/lib/python3.7/site-packages.

Therefore, even when the environment is specified to conda, it does not work.

conda install -n $ENV scikit-learn # Does not work

Solution

pip 3 install the package under ~/anaconda3/lib/python3.7/site-packages.

Verify

After pip3, in a Jupyter notebook.

import sklearn
sklearn.__file__

~/anaconda3/lib/python3.7/site-packages/sklearn/init.py'


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to anaconda

Upgrade to python 3.8 using conda Updating Anaconda fails: Environment Not Writable Error Jupyter Notebook not saving: '_xsrf' argument missing from post Conda version pip install -r requirements.txt --target ./lib How to check python anaconda version installed on Windows 10 PC? Anaconda / Python: Change Anaconda Prompt User Path How to access Anaconda command prompt in Windows 10 (64-bit) Conda activate not working? update to python 3.7 using anaconda Tensorflow import error: No module named 'tensorflow'

Examples related to package

ModuleNotFoundError: No module named 'sklearn' Python: How to pip install opencv2 with specific version 2.4.9? Relative imports - ModuleNotFoundError: No module named x Is __init__.py not required for packages in Python 3.3+ "pip install unroll": "python setup.py egg_info" failed with error code 1 Unable to Install Any Package in Visual Studio 2015 beyond top level package error in relative import How can I specify the required Node.js version in package.json? "installation of package 'FILE_PATH' had non-zero exit status" in R Error in installation a R package

Examples related to conda

Upgrade to python 3.8 using conda How do I prevent Conda from activating the base environment by default? Conda version pip install -r requirements.txt --target ./lib Removing Conda environment PackagesNotFoundError: The following packages are not available from current channels: Conda activate not working? ModuleNotFoundError: No module named 'sklearn' How do I update Anaconda? Conda command is not recognized on Windows 10 'Conda' is not recognized as internal or external command