No module named sklearn ошибка

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.

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

  1. Causes of ImportError: No module named sklearn in Python
  2. Fix ImportError: No module named sklearn in Python
  3. Installation of sklearn Module Using PIP in Python
  4. Installation of sklearn Module Using Conda
  5. Import sklearn and Check Its Version in Python
  6. Conclusion

ImportError: No Module Named Sklearn in Python

In Python, sklearn is used as a machine learning tool for creating programs on regression, cluster, etc. A lot of times, importing it throws an error — No module named sklearn.

This means the system cannot find it due to a bad installation, invalid Python or pip version, or other problems.

Causes of ImportError: No module named sklearn in Python

Suppose we install sklearn or any Python library into the system. The system says that the library has been installed successfully.

But when we import the library, we see the error — No module named 'sklearn'.

no module named sklearn

This can happen for mainly two reasons.

  1. The library did not get installed successfully.
  2. It was installed into an unknown directory that the system cannot find.

This article explains how to install sklearn into the system correctly.

Fix ImportError: No module named sklearn in Python

Installing sklearn into the Windows system is one of the methods through which we can solve the ImportError: No module named sklearn error in Python. There are two prerequisites for it.

  1. Python
  2. pip

Knowing the pip and Python versions beforehand is not only helpful but advantageous. Bad installation happens when the Python version does not match the pip version inside the system.

There are three different ways to install Python on Windows, whichever seems suitable.

  1. Installing from an executable (.exe) installer
  2. Getting it from the Microsoft store
  3. Getting a subsystem Linux distro inside Windows

Only the first two alternatives, the most common installation techniques in a Windows system, will be the subject of this section. There are differences between the two official Python installers for Windows and some significant restrictions on the Microsoft Store bundle.

Installation From the Microsoft Store

The Microsoft Store bundle is the greatest option for a hassle-free setup. This has two main steps.

  1. Search for Python

    Search for Python through the Microsoft Store application. Several versions will likely be encountered that can be installed.

    To access the installation screen, choose Python 3.8 or the highest version number found in the installer.

    Another way is to launch PowerShell and enter the following command.

    Pressing Enter will launch the Microsoft Store and direct the browser to the most recent version of Python available if it is not installed on the computer.

  2. Install the Python app.

    Once the version is chosen, the steps to finish the installation are as follows.

  • Select Get.
  • Await the download of the application. The Install on my devices button will appear in place of the Get button once the download is complete.
  • Select the devices you want to finish installing, then click Install on my devices.
  • To begin the installation, click Install Now and then OK.
  • If the installation was successful, the Microsoft Store page will display "This product is installed" at the top.

    Installation From the Microsoft Store

Installation Using an Executable Installer

  • Download the Full Installer

To download the whole installer, adhere to the following steps.

  1. Go to the Python.org Downloads page for Windows by opening a browser window.
  2. Click the link for the Most Recent Python 3 Release — Python 3.x.x under the “Python Releases for Windows” category. Python 3.10.5 was the most recent version as of this writing.
  3. Either choose Windows x86-64 executable installer for 64-bit or Windows x86 executable installer for 32-bit by scrolling to the bottom.

Go to the next step after the installer has finished downloading.

  • Run the installer

After selecting and downloading an installer, double-click the downloaded file to launch it. You’ll see a dialogue box similar to the one below.

Installation Using an Executable Installer

There are four things to notice about this dialog box.

  1. The standard Windows user’s AppData/ directory contains the default install path.

  2. The pip and IDLE installation locations and extra features can be altered using the Customize installation button.

  3. The default checkbox next to Install launcher for all users (recommended) is selected. This implies that the py.exe launcher will be accessible to all users on the system.

    To make the installation available just for the current user, uncheck the option.

  4. The Add Python 3.10 to PATH checkbox is not selected. Make sure you understand the ramifications before checking this box because there are several reasons why you would not want Python on PATH.

The installation process using the full installer gives complete control over it. Following these steps will install Python on the system.

Now the installation of sklearn can be initiated to solve the No module named 'sklearn' error.

Installation of sklearn Module Using PIP in Python

Launch a command-line runner like PowerShell or the command prompt to install sklearn this way.

How to start PowerShell is as follows:

  1. Go to start by hitting the Windows button.
  2. Type PowerShell (or Command prompt/cmd, if PowerShell is not installed).
  3. Click on it or press the Enter key.

PowerShell can be accessed as an alternative by right-clicking and selecting Open here as an administrator.

Inside the command line runner, the first step is to install pip.

Pip is a Python package manager that downloads and installs Python libraries inside the system. To install pip, type the command:

Once Python and pip are set up, sklearn can be installed. Type the command:

pip3 install scikit-learn

This will install the scikit-learn directory from the pip repositories.

If the system already has an older version of Python, or sklearn is required for a previous version like Python2, use the command:

pip2 install scikit-learn

If all the steps are executed correctly, the No module named 'sklearn' error would be resolved.

Once it is installed successfully, we can check its version by using:

pip show

It can be seen in the above image that the system can detect sklearn, which means our error — No module named 'sklearn' now stands resolved, and now it can be imported.

Installation of sklearn Module Using Conda

Anaconda is a Python distribution that executes Python scripts inside a virtual environment. It can also install and manage Python library packages using conda, an alternative to pip.

This is an alternative method to install sklearn into the system in a targeted environment to resolve the No module named 'sklearn' issue.

To install sklearn in Anaconda, open Anaconda3 in an administrative mode. After Anaconda is launched, open a command line prompt like cmd or PowerShell from inside Anaconda.

Opening a command line runner will load it inside a virtual environment of Anaconda, and all the packages installed will be made exclusively available for Anaconda only.

There are a few checkpoints that need to be ensured to install sklearn, which are:

  1. Checking conda’s version
  2. Updating conda
  3. Installing sklearn
  4. Creating and activating a sklearn virtual environment.

Inside the prompt, write the command:

conda -V

This will display the current version of conda. Then it needs to be updated using the command:

conda update conda

It asks for a y/n after displaying the list of packages that need to be installed. To proceed, type y and press Enter.

Conda will update itself with all the necessary packages.

Now, to install sklearn, use the command:

conda create -n sklearn-env -c conda-forge scikit-learn

The above command directs the prompt to create a virtual environment named sklearn-env and install scikit learn and all the dependencies within its virtual environment.

In Python, a virtual environment is simply an isolated directory that stores specific libraries and a version of Python so that it does not interfere with other libraries and other Python versions.

Entering the above command will display a list of available packages with sklearn that must be downloaded and installed. It asks for a y/n prompt, and entering y will install all the displayed packages.

After completing it, scikit learn gets downloaded and installed into the Anaconda.

Note: A conda HTTP error might occur while installing one of the above packages if the download timeouts from the server. This specifically happens while Python gets downloaded.

The prompt displays the file path of the bad installation; go to the file path, delete the directory with the faulty download, and retry the above command.

This time it will get completed successfully.

Though sklearn is installed, it cannot be directly used. The reason behind it is that it was installed inside a virtual environment that needs to be activated before.

Type the command:

conda activate sklearn-env

This will activate the sklearn virtual environment, and now sklearn can be imported and used. If all the steps are correctly followed, the No module named 'sklearn' error would be resolved by now.

In the next section, we will check sklearn’s version and import it.

Import sklearn and Check Its Version in Python

To check the version of sklearn, type the command:

conda list scikit-learn

conda list

To check all the installed packages, use the command:

conda list

To check the version of sklearn along with its dependencies, type:

python -c "import sklearn; sklearn.show_versions()"

Now we know that sklearn is installed and working successfully, we can import it by:

import sklearn

import sklearn

If it shows no errors, that means sklearn is working correctly.

If you again get the error — No module named 'sklearn', remove the virtual environment and packages, restart your system and repeat the above steps once again to get it installed correctly.

Conclusion

This article explains various methods to install sklearn into the system and resolve the No module named 'sklearn' error in Python. After reading this article, the reader will be able to install sklearn easily in different kinds of systems and setups.

It is hoped that this article helped in your learning journey.

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!

Quick Fix: Python raises the ImportError: No module named 'sklearn' when it cannot find the library sklearn. The most frequent source of this error is that you haven’t installed sklearn explicitly with pip install sklearn. Alternatively, you may have different Python versions on your computer, and sklearn is not installed for the particular version you’re using.

Problem Formulation

You’ve just learned about the awesome capabilities of the sklearn library and you want to try it out, so you start your code with the following statement:

import sklearn

This is supposed to import the Pandas library into your (virtual) environment. However, it only throws the following ImportError: No module named sklearn:

>>> import sklearn
Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    import sklearn
ModuleNotFoundError: No module named 'sklearn'

Solution Idea 1: Install Library sklearn

The most likely reason is that Python doesn’t provide sklearn in its standard library. You need to install it first!

Before being able to import the Pandas module, you need to install it using Python’s package manager pip. Make sure pip is installed on your machine.

To fix this error, you can run the following command in your Windows shell:

$ pip install sklearn

This simple command installs sklearn in your virtual environment on Windows, Linux, and MacOS. It assumes that your pip version is updated. If it isn’t, use the following two commands in your terminal, command line, or shell (there’s no harm in doing it anyways):

$ python -m pip install --upgrade pip
$ pip install pandas

💡 Note: Don’t copy and paste the $ symbol. This is just to illustrate that you run it in your shell/terminal/command line.

Solution Idea 2: Fix the Path

The error might persist even after you have installed the sklearn library. This likely happens because pip is installed but doesn’t reside in the path you can use. Although pip may be installed on your system the script is unable to locate it. Therefore, it is unable to install the library using pip in the correct path.

To fix the problem with the path in Windows follow the steps given next.

Step 1: Open the folder where you installed Python by opening the command prompt and typing where python

Step 2: Once you have opened the Python folder, browse and open the Scripts folder and copy its location. Also verify that the folder contains the pip file.

Step 3: Now open the Scripts directory in the command prompt using the cd command and the location that you copied previously.

Step 4: Now install the library using pip install sklearn command. Here’s an analogous example:

After having followed the above steps, execute our script once again. And you should get the desired output.

Other Solution Ideas

  • The ModuleNotFoundError may appear due to relative imports. You can learn everything about relative imports and how to create your own module in this article.
  • You may have mixed up Python and pip versions on your machine. In this case, to install sklearn for Python 3, you may want to try python3 -m pip install sklearn or even pip3 install sklearn instead of pip install sklearn
  • If you face this issue server-side, you may want to try the command pip install --user sklearn
  • If you’re using Ubuntu, you may want to try this command: sudo apt install sklearn
  • You can check out our in-depth guide on installing sklearn here.
  • You can also check out this article to learn more about possible problems that may lead to an error when importing a library.

Understanding the “import” Statement

import sklearn

In Python, the import statement serves two main purposes:

  • Search the module by its name, load it, and initialize it.
  • Define a name in the local namespace within the scope of the import statement. This local name is then used to reference the accessed module throughout the code.

What’s the Difference Between ImportError and ModuleNotFoundError?

What’s the difference between ImportError and ModuleNotFoundError?

Python defines an error hierarchy, so some error classes inherit from other error classes. In our case, the ModuleNotFoundError is a subclass of the ImportError class.

You can see this in this screenshot from the docs:

You can also check this relationship using the issubclass() built-in function:

>>> issubclass(ModuleNotFoundError, ImportError)
True

Specifically, Python raises the ModuleNotFoundError if the module (e.g., sklearn) cannot be found. If it can be found, there may be a problem loading the module or some specific files within the module. In those cases, Python would raise an ImportError.

If an import statement cannot import a module, it raises an ImportError. This may occur because of a faulty installation or an invalid path. In Python 3.6 or newer, this will usually raise a ModuleNotFoundError.

Related Videos

The following video shows you how to resolve the ImportError:

How to Fix : “ImportError: Cannot import name X” in Python?

The following video shows you how to import a function from another folder—doing it the wrong way often results in the ModuleNotFoundError:

How to Call a Function from Another File in Python?

How to Fix “ModuleNotFoundError: No module named ‘sklearn’” in PyCharm

If you create a new Python project in PyCharm and try to import the sklearn library, it’ll raise the following error message:

Traceback (most recent call last):
  File "C:/Users/.../main.py", line 1, in <module>
    import sklearn
ModuleNotFoundError: No module named 'sklearn'

Process finished with exit code 1

The reason is that each PyCharm project, per default, creates a virtual environment in which you can install custom Python modules. But the virtual environment is initially empty—even if you’ve already installed sklearn on your computer!

Here’s a screenshot exemplifying this for the pandas library. It’ll look similar for sklearn.

The fix is simple: Use the PyCharm installation tooltips to install Pandas in your virtual environment—two clicks and you’re good to go!

First, right-click on the pandas text in your editor:

Second, click “Show Context Actions” in your context menu. In the new menu that arises, click “Install Pandas” and wait for PyCharm to finish the installation.

The code will run after your installation completes successfully.

As an alternative, you can also open the Terminal tool at the bottom and type:

$ pip install sklearn

If this doesn’t work, you may want to set the Python interpreter to another version using the following tutorial: https://www.jetbrains.com/help/pycharm/2016.1/configuring-python-interpreter-for-a-project.html

You can also manually install a new library such as sklearn in PyCharm using the following procedure:

  • Open File > Settings > Project from the PyCharm menu.
  • Select your current project.
  • Click the Python Interpreter tab within your project tab.
  • Click the small + symbol to add a new library to the project.
  • Now type in the library to be installed, in your example Pandas, and click Install Package.
  • Wait for the installation to terminate and close all popup windows.

Here’s an analogous example:

Here’s a full guide on how to install a library on PyCharm.

  • How to Install a Library on PyCharm

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

In this article, I will show you how to solve ModuleNotFoundError: No module named 'sklearn' error in Python3. It is not very uncommon to get this error in a freshly built system which has been given to Python programmers or developers to run their programs. Something similar happened to me as well, for the first time I tried to use scikit-learn module in my program but when i tried to run it, I ended up with ModuleNotFoundError: No module named 'sklearn' error. I am sure lot of folks out there might be facing the same issue so before solving this error I thought to write an article about this so that it will help you guys also.

Solved: ModuleNotFoundError: No module named 'sklearn' in Python3

Also Read: Python: Introduction to Pipenv and Poetry with Best Examples

So as I said when I was trying to run my program, I noticed below ModuleNotFoundError: No module named 'sklearn' error on the output.

NOTE:

Please note that in my case I am using Ubuntu 20.04 LTS System with Python3 installed on the System. You could be using some different system but the nature of problem most probably remains the same.

cyberithub@ubuntu:~$ python3 example.py
Traceback (most recent call last):
 File "example.py", line 1, in <module>
  import sklearn as sk
ModuleNotFoundError: No module named 'sklearn'

While the above error could occur due to multiple reasons but most of the time it occurs due to missing sklearn module in your System. So to solve this error you just need to install sklearn module in your System. If you have pip3 utility available in your system then you can easily install the module by using pip3 install -U scikit-learn command as shown below.

cyberithub@ubuntu:~$ pip3 install -U scikit-learn
Collecting scikit-learn
Downloading scikit_learn-1.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.2 MB)
|████████████████████████████████| 31.2 MB 111 kB/s
Collecting joblib>=1.0.0
Downloading joblib-1.1.0-py2.py3-none-any.whl (306 kB)
|████████████████████████████████| 306 kB 4.3 MB/s
Requirement already satisfied, skipping upgrade: numpy>=1.17.3 in /usr/lib/python3/dist-packages (from scikit-learn) (1.17.4)
Collecting threadpoolctl>=2.0.0
Downloading threadpoolctl-3.1.0-py3-none-any.whl (14 kB)
Collecting scipy>=1.3.2
Downloading scipy-1.9.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (43.4 MB)
|████████████████████████████████| 43.4 MB 94 kB/s
ERROR: scipy 1.9.1 has requirement numpy<1.25.0,>=1.18.5, but you'll have numpy 1.17.4 which is incompatible.
Installing collected packages: joblib, threadpoolctl, scipy, scikit-learn
Successfully installed joblib-1.1.0 scikit-learn-1.1.2 scipy-1.9.1 threadpoolctl-3.1.0

Alternatively, on a Debian/Ubuntu based system, you can install the module by using sudo apt install python3-sklearn command as shown below.

cyberithub@ubuntu:~$ sudo apt install python3-sklearn
[sudo] password for cyberithub:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
blt fonts-lyx javascript-common libjs-jquery libjs-jquery-ui liblbfgsb0 libtcl8.6 libtk8.6 python-matplotlib-data python3-atomicwrites python3-cycler
python3-decorator python3-importlib-metadata python3-joblib python3-kiwisolver python3-matplotlib python3-more-itertools python3-nose python3-packaging
python3-pluggy python3-py python3-pyparsing python3-pytest python3-scipy python3-sklearn-lib python3-tk python3-wcwidth python3-zipp tk8.6-blt2.5
ttf-bitstream-vera
Suggested packages:
blt-demo apache2 | lighttpd | httpd libjs-jquery-ui-docs tcl8.6 tk8.6 python-cycler-doc dvipng ffmpeg inkscape ipython3 python-matplotlib-doc
python3-gobject python3-pyqt5 python3-tornado texlive-extra-utils texlive-latex-extra ttf-staypuft python-nose-doc subversion python-pyparsing-doc
python-scipy-doc python3-dap python-sklearn-doc tix python3-tk-dbg
The following NEW packages will be installed:
blt fonts-lyx javascript-common libjs-jquery libjs-jquery-ui liblbfgsb0 libtcl8.6 libtk8.6 python-matplotlib-data python3-atomicwrites python3-cycler
python3-decorator python3-importlib-metadata python3-joblib python3-kiwisolver python3-matplotlib python3-more-itertools python3-nose python3-packaging
python3-pluggy python3-py python3-pyparsing python3-pytest python3-scipy python3-sklearn python3-sklearn-lib python3-tk python3-wcwidth python3-zipp
tk8.6-blt2.5 ttf-bitstream-vera
0 upgraded, 31 newly installed, 0 to remove and 51 not upgraded.
Need to get 27.7 MB of archives.
After this operation, 110 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
.........................................

After installing the scikit-learn module, I again tried to run the program and guess what. This time it ran successfully. This confirms that my issue is resolved now. But there could be a chance that you might be still facing the issue and the same solution does not work for you. So in that case you need to check if the python libraries path are visible to System or not. If you have installed the libraries in different path then you need to specify the location in PATH environment variable to make it visible to the System.

Hopefully, above given solution would be helpful to you as well. Please let me know your feedback on the comment box.

Понравилась статья? Поделить с друзьями:
  • No module named psycopg2 как исправить
  • No module named pipe как исправить
  • No module named pandas pycharm как исправить
  • No module named numpy python как исправить
  • No module named matplotlib как исправить ошибку