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.

Gonçalo Peres's user avatar

asked Sep 8, 2017 at 9:56

Hareez Rana's user avatar

2

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

MBT's user avatar

MBT

21.2k19 gold badges82 silver badges102 bronze badges

answered Sep 8, 2017 at 10:00

mrCarnivore's user avatar

mrCarnivoremrCarnivore

4,4672 gold badges11 silver badges29 bronze badges

4

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

  1. Using conda

  2. Using pip

One might want to consider the notes at the end, specially before resorting to the 2nd option.


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 should solve the problem

conda install scikit-learn

Alternatively, as mentioned here, one can specify the channel as follows

conda install -c anaconda scikit-learn

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

Then the following should solve one’s problem:

conda install -n ML scikit-learn

# or

conda install -n ML -c anaconda scikit-learn

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

However, consider the last note below before proceeding.


Notes:

  • 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

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

  • What is the flag -c.

  • pip doesn’t manage dependencies the same way conda does and can, potentially, damage one’s installation.

answered Sep 26, 2018 at 15:21

Gonçalo Peres's user avatar

Gonçalo PeresGonçalo Peres

11.3k3 gold badges48 silver badges82 bronze badges

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

Hristo Eftimov's user avatar

answered Sep 20, 2019 at 13:29

yuvraj sune's user avatar

I did the following:

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

answered Jun 30, 2020 at 21:35

cesareressa's user avatar

cesareressacesareressa

3262 silver badges9 bronze badges

1

On Windows, I had python 3+ version. pip version — 22.3.1

I had installed:

pip install sklearn

But, it seems it is deprecated with scikit-learn.

So, I did:

pip install scikit-learn

And, it worked!!!

answered Dec 8, 2022 at 7:58

KnockingHeads's user avatar

KnockingHeadsKnockingHeads

1,4391 gold badge19 silver badges42 bronze badges

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!

answered Oct 21, 2018 at 9:01

Claude COULOMBE's user avatar

Claude COULOMBEClaude COULOMBE

3,3582 gold badges36 silver badges38 bronze badges

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…

answered Dec 25, 2018 at 11:52

Mohammad ElNesr's user avatar

Mohammad ElNesrMohammad ElNesr

2,4674 gold badges27 silver badges44 bronze badges

1

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.

answered Jul 19, 2020 at 18:01

Sasan's user avatar

SasanSasan

1312 bronze badges

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

answered Feb 2, 2021 at 21:08

Zvi's user avatar

ZviZvi

2,3242 gold badges24 silver badges37 bronze badges

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’

answered May 8, 2019 at 7:02

mon's user avatar

monmon

17.9k19 gold badges106 silver badges191 bronze badges

1

I had the same problem.
The issue is when we work on multiple anaconda environments, not all packages are installed in all environments.
you can check your conda environment by writing the following code in anaconda prompt:

conda env list

then you can check the packages installed in each environment :

conda list -n NAME_OF_THE_ENVIRONMENT

for me, the environment that I was working with , was missing sklearn, although the package was installed in the other environments.

therefore, I just simply installed sklearn package in that particular environment

conda install -n NAME_OF_THE_ENVIRONMENT scikit-learn

and the issue was resolved

answered Jan 12, 2022 at 2:57

Mustafa's user avatar

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

answered Jul 2, 2020 at 18:10

Akash Desai's user avatar

Akash DesaiAkash Desai

4925 silver badges11 bronze badges

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 Libsite-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:UsersYourUserNameAppDataLocalTemp). 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.

answered Nov 1, 2020 at 15:54

allan200's user avatar

Using Anaconda-navigator UI environment

When running Anaconda-navigator:

  • Choose the ‘Environments’ tab on the left and create a new environment (e.g. ML — see Gonçalo Peres answer above, I made one called ‘CourseraML’).

  • Set Python version 3.7 (for Coursera course Applied Machine Learning in Python). Also include R.

  • Then find modules to install using the ‘not installed’ drop-down menu item. Search for each module needed in the search bar and select. sklearn is part of scikit-learn. Select it and install (it should find all relevant dependencies). Modules needed for Applied ML course: seaborn, numpy, scikit-learn, pandas, matplotlib

  • You’ll need to restart Jupyter Notebook and reopen your file.

Command line version of above:

conda install -n CourseraML seaborn scikit-learn pandas numpy matplotlib graphviz

answered May 4, 2021 at 7:37

AntC's user avatar

AntCAntC

766 bronze badges

Causes
-your jupyter notebook might be importing the sklearn and other libraries from the
another the location(path) than the libraries from conda or pip.

MY Problem
In my case, My jupyter notebook was importing the libraries for snap manager. Since, I install jupyter using the snap instead of other ways.

You can check where other libraries are being imported in jupyter using code:

import cv2 as cv
print(cv.__file__)

Solution
So , I uninstall jupyter notebook and then install notebook using conda.

 sudo snap remove jupyter

conda install -c conda-forge notebook

answered May 28, 2021 at 15:28

Sagar Shrestha's user avatar

The error “ModuleNotFoundError: No module named sklearn» is a common error experienced by data scientists when developing in Python. The error is likely an environment issue whereby the scikit-learn package has not been installed correctly on your machine, thankfully there are a few simple steps to go through to troubleshoot the problem and find a solution.

Your error, whether in a Jupyter Notebook or in the terminal, probably looks like one of the following:

No module named 'sklearn'
ModuleNotFoundError: No module named 'sklearn'

In order to find the root cause of the problem we will go through the following potential fixes:

  1. Upgrade pip version
  2. Upgrade or install scikit-learn package
  3. Check if you are activating the environment before running
  4. Create a fresh environment
  5. Upgrade or install Jupyer Notebook package

Are you installing packages using Conda or Pip package manager?

It is common for developers to use either Pip or Conda for their Python package management. It’s important to know what you are using before we continue with the fix.

If you have not explicitly installed and activated Conda, then you are almost definitely going to be using Pip. One sanity check is to run conda info in your terminal, which if it returns anything likely means you are using Conda.

Upgrade or install pip for Python

First things first, let’s check to see if we have the up to date version of pip installed. We can do this by running:

pip install --upgrade pip

Upgrade or install scikit-learn package via Conda or Pip

The most common reason for this error is that the scikit-learn package is not installed in your environment or an outdated version is installed. So let’s update the package or install it if it’s missing.

For Conda:

# To install in the root environment 
conda install -c anaconda scikit-learn 

# To install in a specific environment 
conda install -n MY_ENV scikit-learn

For Pip:‌

# To install in the root environment
python3 -m pip install -U scikit-learn

# To install in a specific environment
source MY_ENV/bin/activate
python3 -m pip install -U scikit-learn

Activate Conda or venv Python environment

It is highly recommended that you use isolated environments when developing in Python. Because of this, one common mistake developers make is that they don’t activate the correct environment before they run the Python script or Jupyter Notebook. So, let’s make sure you have your correct environment running.

For Conda:

conda activate MY_ENV

For virtual environments:

source MY_ENV/bin/activate

Create a new Conda or venv Python environment with scikit-learn installed

During the development process, a developer will likely install and update many different packages in their Python environment, which can over time cause conflicts and errors.

Therefore, one way to solve the module error for sklearn is to simply create a new environment with only the packages that you require, removing all of the bloatware that has built up over time. This will provide you with a fresh start and should get rid of problems that installing other packages may have caused.

For Conda:

# Create the new environment with the desired packages
conda create -n MY_ENV python=3.9 scikit-learn 

# Activate the new environment 
conda activate MY_ENV 

# Check to see if the packages you require are installed 
conda list

For virtual environments:

# Navigate to your project directory 
cd MY_PROJECT 

# Create the new environment in this directory 
python3 -m venv MY_ENV 

# Activate the environment 
source MY_ENV/bin/activate 

# Install scikit-learn 
python3 -m pip install scikit-learn

Upgrade Jupyter Notebook package in Conda or Pip

If you are working within a Jupyter Notebook and none of the above has worked for you, then it could be that your installation of Jupyter Notebooks is faulty in some way, so a reinstallation may be in order.

For Conda:

conda update jupyter

For Pip:

pip install -U jupyter

Best practices for managing Python packages and environments

Managing packages and environments in Python is notoriously problematic, but there are some best practices which should help you to avoid package the majority of problems in the future:

  1. Always use separate environments for your projects and avoid installing packages to your root environment
  2. Only install the packages you need for your project
  3. Pin your package versions in your project’s requirements file
  4. Make sure your package manager is kept up to date

References

Conda managing environments documentation
Python venv documentation

Sklearn or scikit learn is the most popular, free machine learning library for python. It includes widely used machine learning algorithms such as regression models, k nearest neighbors, k means clustering, support vector machine, dbscan, random forests, etc. Hence, there’s no surprise that data scientists or anyone with a passion for machine learning and Artificial Intelligence would want to install scikit learn in their systems. But a lot of developers have to face ModuleNotFoundError: No module named ‘sklearn’ once they try to execute their code. There can be a couple of reasons why they are seeing this error.

But first, What is a ModuleNotFoundError?

Simply A ModuleNotFoundError (In this particular case no module named ‘sklearn’ error) is raised at runtime when the python is unable/fails to import a module into your program. There are a few possible reasons behind this error.

  1. Python cannot locate the module in your import command
  2. You have misspelled your module name in your import command
  3. You simply haven’t installed that module

Common Cause of ModuleNotFoundError: No module named ‘sklearn’

In most cases of no module named ‘sklearn’, we can rule out the number two and number three causes that I have just mentioned in the above section.

Because most of the time we know that we have installed scikit learn beforehand. And we also know that we have typed the name correctly.

Then it must be that python cannot locate sklearn module, right? Yes, Of Course!

Reasons why Python cannot locate the sklearn/scikit learn module

The reasons can be very complicated because every case is different. So one’s cause for no module named ‘sklearn’ error might not be the same as someone else’s. There are many possible scenarios why python cannot locate sklearn module. Starting from different python virtual environments and different python versions to things like whether you are running your algorithm in jupyter notebooks or in an IDE like PyCharm, can all be a possible cause. So we have to investigate every single scenario and apply possible fixes. Otherwise, we aren’t addressing no module named ‘sklearn’ in everyone’s favor!

Are you a complete beginner to Scikit-Learn? Or do you want more hands-on tutorials? Then check out the following Scikit Learn Books for beginners.

Scikit Learn has been installed in a different virtual environment than your program being executed

The first thing you should do when you see this error is to check your python virtual environment. Is it the same environment that you installed scikit learn into? If not, then python can’t locate the sklearn module and hence raises the error.

I have installed all my machine learning libraries and packages into a default environment called “tensorflow“. Why named TensorFlow? It’s just because I am working heavily with TensorFlow projects currently. The name of the environment can be anything that you wish. It’s just a method for isolating and organizing your different python projects so everything is clean and separated. You don’t have to worry about version incompatibilities, etc. if you have different environments.

But now, let’s import sklearn into a different python environment in PyCharm and check whether python can locate it or not.

ModuleNotFoundError No module named 'sklearn'

Yes, you guessed right! Python cannot locate sklearn module! We get the ModuleNotFoundError: No module named ‘sklearn’ error.

Let me edit my project environment and set it to tensorflow. And try again. Sown below.

choosing the environments in PyCharm edit configurations window

Running the code again, with the correct environment (tensorflow) where sklearn has been installed.

sklearn module successfully imported into pycharm

Now as you can see the problem has been solved. Hence, we can continue with our machine learning code, there are numerous things to be done using scikit learn after all! 🙂

ModuleNotFoundError: No module named ‘sklearn’ error in Jupyter Notebooks

Not everyone’s running their ML programs in a real IDE. Often developers use jupyter as an easy python development environment that actually lives on web. When trying to run sklearn within jupyter notebooks people seem to face no module named sklearn error often.

Let’s now look for the possible causes for this no module named ‘sklearn’ errors raised in jupyter notebooks.

Again, we know that we have installed scikit learn and we have spelled the thing correctly. So the obvious cause behind the error is that python cannot find the sklearn module to import.

Best practice of installing Jupyter notebooks with scikit learn in your computer to avoid ModuleNotFoundError: No module named ‘sklearn’

The best way to install jupyter and to work with essential python packages and modules like sklearn by default is by installing the Anaconda distribution. I highly recommend installing Anaconda. Every machine learning enthusiast should have this amazing distribution for python installed. 🙂

Do you already have Anaconda installed? Then what version it is? Read this guide to find that out. Check Anaconda Version in Windows?

If you have installed the latest Anaconda version, you must already have jupyter notebooks and scikit learn pre-installed. Then you should be able to run jupyter notebook and import sklearn with no issue.

Simply go to your CMD and type and hit enter the following command.

jupyter notebook

This will automatically open up jupyter notebook for you.

Let’s start a new notebook and checking whether we can import sklearn with no errors

import sklearn

sklearn. __version__

getting sklearn version in jupyter notebook

Success! There is no ModuleNotFoundError: No module named ‘sklearn’. I have the latest scikit- library which is 0.24.1.

No module named ‘sklearn’ error when jupyter notebooks and skleran installed separately

If you have installed jupyter notebooks and scikit-learn separately the chances are that jupyter notebooks cannot locate sklearn module to import.

Scenario 01:

For an example, you might have installed jupyter notebooks using another package manager like snap. Then Jupyter notebook will look for sklearn module inside the snap manager, which will raise an error ModuleNotFoundError: No module named ‘sklearn’.

Scenario 02:

Both Conda and pip install scikit-learn under the following path when using virtual environments.

~/anaconda3/envs/$ENV/lib/python"version"/site-packages

But Jupyter notebook searches for the installed packages under,

~/anaconda3/lib/python"version"/site-packages

Hence, it can’t locate the installed sklearn module. Then you get the infamous ModuleNotFoundError: No module named ‘sklearn’, yet again!

You can check the path of sklearn by running the following code in jupyter notebook.

import sklearn
sklearn.__file__

getting sklearn module path in jupyter notebooks

To avoid path mismatch, we can install scikit learn always under,

~/anaconda3/lib/python"version"/site-packages

Do you have several python versions installed in your computer?

When you have several python versions installed you are asking for trouble. Each python version must have its own path for Libsite-packages and its own set of packages and modules installed. So when your python machine learning program is looking for the sklearn module it literally doesn’t have any idea which python version’s path to look for. Hence raises ModuleNotFoundError: No module named ‘sklearn’ error.

As a solution, you can have one python version installed on your computer. If you do need several versions to be installed then you must install sklearn module into where jupyter notebook is pointing when it imports external modules.

Conclusion

ModuleNotFoundError: No module named ‘sklearn’ isn’t a complicated error. It’s a simple runtime error when python cannot import the specified module into your code. Most of the time this is because python cannot locate the path where sklearn module has been installed into. On other occasions, it’s we being so busy and misspelling the word “sklearn” in to something else. Lastly, in rare situations, it can be that we are trying to import the sklearn module when it’s hasn’t actually been installed! The bottom line is that this error isn’t something serious, you can always figure out what has gone wrong, and then fix it.

Want to become an ML and Data Science Expert? And get hired by world class companies? Enroll with Eduraka Today!

When running Python code, sometimes you will see an error saying ModuleNotFoundError: No module named 'sklearn'.

This error means that Python can’t find the sklearn module. You may not have the sklearn module installed, or the module is installed in a different environment.

The example error log is as follows:

Traceback (most recent call last):
  File ...
    import sklearn
ModuleNotFoundError: No module named 'sklearn'

To fix this error, you need to install the sklearn package using pip or conda.

First, let’s see how to install sklearn with pip. You need to run one of the following commands:

# Use pip to install scikit-learn
pip install scikit-learn

# Or pip3
pip3 install scikit-learn

# If pip isn't available in PATH
python -m pip install scikit-learn

# Or with python3
python3 -m pip install scikit-learn

# For Windows without pip in PATH
py -m pip install scikit-learn

Once the installation has finished, you should be able to import the sklearn module in your code successfully.

When you are running Python from Jupyter Notebook, add the ! operator before pip like this:

!pip install -U scikit-learn
# or
!conda install -c anaconda scikit-learn

If you installed Python from Anaconda distribution, then you might want to install the sklearn package from conda with the following command:

conda install scikit-learn

If you still see the error, it means the Python interpreter still can’t find the sklearn module.

One of the following scenarios may happen in your case:

  1. You have multiple versions of Python installed on your system, and you are using a different version of Python than the one where scikit-learn is installed.
  2. You might have scikit-learn installed in a virtual environment, and you are not activating the virtual environment before running your code.

Let’s see how to fix these errors.

Case#1 — You have multiple versions of Python

If you have multiple versions of Python installed on your system, you need to make sure that you are using the specific version where the scikit-learn package is installed.

You can test this by running the which -a python or which -a python3 command from the terminal:

$ which -a python
/opt/homebrew/bin/python
/usr/bin/python

In the example above, there are two versions of Python installed on /opt/homebrew/bin/python and /usr/bin/python.

Suppose you run the following steps in your project:

  1. Install scikit-learn with pip using /usr/bin/ Python version
  2. Install Python using Homebrew, now you have Python in /opt/homebrew/
  3. Then add import scikit-learn in your code

The steps above will cause the error because scikit-learn is installed in /usr/bin/, and your code is probably executed using Python from /opt/homebrew/ path.

To solve this error, you need to run pip install scikit-learn command again so that the package is installed and accessible by the new Python version.

Finally, keep in mind that you can also have pip and pip3 available on your computer.

The pip command usually installs module for Python 2, while pip3 installs for Python 3. Make sure that you are using the right command for your situation.

Next, you can also have the package installed in a virtual environment.

Case#2 — You are using Python virtual environment

Python venv package allows you to create a virtual environment where you can install different versions of packages required by your project.

If you are installing scikit-learn inside a virtual environment, then the module won’t be accessible outside of that environment.

Even if you never run the venv package, Python Anaconda distribution usually creates a virtual enviroment automatically when you install it on your computer.

You can see if a virtual environment is activated or not by looking at your command prompt.

When a virtual environment is activated, the name of that environment will be shown inside parentheses as shown below:

In the picture above, the name of the virtual environment (base) appears when the Conda virtual environment is activated.

Usually, a Conda virtual environment already has the scikit-learn module active. You can check this in the Anaconda Environments tab as shown below:

If you see scikit-learn installed but the environment is not activated, then run the command below to activate it:

conda activate <env>

# For base env:
conda activate base

Once the base environment is activated, you should be able to import sklearn module.

Conclusion

To conclude, the ModuleNotFoundError: No module named 'scikit-learn' error occurs when the scikit-learn package is not available in your Python environment.

To fix this error, you need to install scikit-learn using pip or conda.

If you already have the module installed, make sure you are using the correct version of Python, or activate the virtual environment if you have one.

By following these steps, you should be able to import scikit-learn modules in your code successfully.

A common error you may encounter when using Python is modulenotfounderror: no module named ‘sklearn’. This error occurs when Python cannot detect the Scikit-learn library in your current environment, and Scikit-learn does not come with the default Python installation. This tutorial goes through the exact steps to troubleshoot this error for the Windows, Mac and Linux operating systems.


Table of contents

  • ModuleNotFoundError: no module named ‘sklearn’
    • What is ModuleNotFoundError?
  • What is Scikit-learn?
    • How to install Scikit-learn on Windows Operating System
    • How to install Scikit-learn on Mac Operating System
    • How to install Scikit-learn on Linux Operating System
      • Installing pip for Ubuntu, Debian, and Linux Mint
      • Installing pip for CentOS 8 (and newer), Fedora, and Red Hat
      • Installing pip for CentOS 6 and 7, and older versions of Red Hat
      • Installing pip for Arch Linux and Manjaro
      • Installing pip for OpenSUSE
    • Check Scikit-Learn Version
  • Installing Scikit-Learn Using Anaconda
  • Prerequisites Before Using Scikit-Learn
  • Summary

ModuleNotFoundError: no module named ‘sklearn’

What is ModuleNotFoundError?

The ModuleNotFoundError occurs when the module you want to use is not present in your Python environment. There are several causes of the modulenotfounderror:

The module’s name is incorrect, in which case you have to check the name of the module you tried to import. Let’s try to import the re module with a double e to see what happens:

import ree
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
1 import ree

ModuleNotFoundError: No module named 'ree'

To solve this error, ensure the module name is correct. Let’s look at the revised code:

import re

print(re.__version__)
2.2.1

You may want to import a local module file, but the module is not in the same directory. Let’s look at an example package with a script and a local module to import. Let’s look at the following steps to perform from your terminal:

mkdir example_package

cd example_package

mkdir folder_1

cd folder_1

vi module.py

Note that we use Vim to create the module.py file in this example. You can use your preferred file editor, such as Emacs or Atom. In module.py, we will import the re module and define a simple function that prints the re version:

import re

def print_re_version():

    print(re.__version__)

Close the module.py, then complete the following commands from your terminal:

cd ../

vi script.py

Inside script.py, we will try to import the module we created.

import module

if __name__ == '__main__':

    mod.print_re_version()

Let’s run python script.py from the terminal to see what happens:

ModuleNotFoundError: No module named 'module'

To solve this error, we need to point to the correct path to module.py, which is inside folder_1. Let’s look at the revised code:

import folder_1.module as mod

if __name__ == '__main__':

    mod.print_re_version()

When we run python script.py, we will get the following result:

2.2.1

Lastly, you can encounter the modulenotfounderror when you import a module that is not installed in your Python environment.

What is Scikit-learn?

Scikit-learn is a Python module for machine learning. The library is mainly written in Python and is built on NumPy, SciPy, and Matplotlib. The simplest way to install Scikit-learn is to use the package manager for Python called pip. The following instructions to install Scikit-learn are for the major Python version 3.

How to install Scikit-learn on Windows Operating System

You need to download and install Python on your PC. Ensure you select the install launcher for all users and Add Python to PATH checkboxes. The latter ensures the interpreter is in the execution path. Pip is automatically on Windows for Python versions 2.7.9+ and 3.4+.

You can install pip on Windows by downloading the installation package, opening the command line and launching the installer. You can install pip via the CMD prompt by running the following command.

python get-pip.py

You may need to run the command prompt as administrator. Check whether the installation has been successful by typing.

pip --version

To install Scikit-learn with pip, run the following command from the command prompt.

pip install -U scikit-learn

How to install Scikit-learn on Mac Operating System

Open a terminal by pressing command (⌘) + Space Bar to open the Spotlight search. Type in terminal and press enter. To get pip, first ensure you have installed Python3.

You can install Python3 by using the Homebrew package manager:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

# if you are on macOS 10.12 (Sierra) use `export PATH="/usr/local/bin:/usr/local/sbin:$PATH"`

brew update

brew install python  # Python 3
python3 --version
Python 3.8.8

Download pip by running the following curl command:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

The curl command allows you to specify a direct download link, and using the -o option sets the name of the downloaded file.

Install pip by running:

python3 get-pip.py

From the terminal, use pip3 to install Scikit-learn:

pip install -U scikit-learn

How to install Scikit-learn on Linux Operating System

All major Linux distributions have Python installed by default. However, you will need to install pip. You can install pip from the terminal, but the installation instructions depend on the Linux distribution you are using. You will need root privileges to install pip. Open a terminal and use the commands relevant to your Linux distribution to install pip.

Installing pip for Ubuntu, Debian, and Linux Mint

sudo apt install python-pip3

Installing pip for CentOS 8 (and newer), Fedora, and Red Hat

sudo dnf install python-pip3

Installing pip for CentOS 6 and 7, and older versions of Red Hat

sudo yum install epel-release

sudo yum install python-pip3

Installing pip for Arch Linux and Manjaro

sudo pacman -S python-pip

Installing pip for OpenSUSE

sudo zypper python3-pip

Once you have installed pip, you can install Scikit-learn using:

pip install -U scikit-learn

Check Scikit-Learn Version

Once you have successfully installed Scikit-learn, you can use two methods to check the version of Scikit-learn. First, you can use pip show from your terminal.

pip show scikit-learn
Name: scikit-learn
Version: 0.24.1
Summary: A set of python modules for machine learning and data mining
Home-page: http://scikit-learn.org
Author: None
Author-email: None
License: new BSD
Location: /Users/Yusufu.Shehu/opt/anaconda3/lib/python3.8/site-packages
Requires: threadpoolctl, numpy, scipy, joblib
Required-by: mlxtend, imbalanced-learn 

Second, within your python program, you can import Scikit-Learn and then reference the __version__ attribute:

import sklearn

print(sklearn.__version__)
0.24.1

Installing Scikit-Learn Using Anaconda

Anaconda is a distribution of Python and R for scientific computing and data science. You can install Anaconda by going to the installation instructions. Once you have installed Anaconda, you can install Scikit-learn using the following command:

conda install -c conda-forge scikit-learn

Prerequisites Before Using Scikit-Learn

Before you can start using the latest release of scikit-learn, you must have the following installed:

  • Python (>= 3.5)
  • NumPy (>= 1.11.0)
  • SciPy (>= 0.17.0)
  • Joblib (>= 0.11)
  • Matplotlib (>= 1.5.1) required for Scikit-Learn plotting capabilities
  • Pandas (>= 0.18.0) is required for Scikit-learn data structure and analysis

Summary

Congratulations on reading to the end of this tutorial. The modulenotfounderror occurs if you misspell the module name, incorrectly point to the module path or do not have the module installed in your Python environment. If you do not have the module installed in your Python environment, you can use pip to install the package. However, you must ensure you have pip installed on your system. You can also install Anaconda on your system and use the conda install command to install the Scikit-learn library.

You may encounter a ModuleNotFoundError when trying to use a class or function from a module in Scikit-Learn. To solve this error, go to the article: How to Solve ModuleNotFoundError: No module named ‘sklearn.cross_validation’.

For further reading on installing data science and machine learning libraries, you can go to the articles:

  • OpenCV: How to Solve Python ModuleNotFoundError: no module named ‘cv2’
  • Requests: How to Solve Python ModuleNotFoundError: no module named ‘requests’
  • Pandas: How to Solve Python ModuleNotFoundError: no module named ‘pandas’
  • Matplotlib: How to Solve Python ModuleNotFoundError: no module named ‘matplotlib’
  • Numpy: How to Solve Python ModuleNotFoundError: no module named ‘numpy’
  • Imbalanced-learn: How to Solve Python ModuleNotFoundError: no module named ‘imblearn’

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!

Понравилась статья? Поделить с друзьями:
  • No module named pyside2 ошибка
  • No module named colorama ошибка
  • Nissan tiida ошибка подушки безопасности
  • Nissan tiida ошибка p1611
  • Nissan tiida ошибка p1126