ModuleNotFoundError: No module named ‘sklearn’ in Python

In Python, the “sklearn” library provides several machine learning algorithms such as SVM, regression, clustering, etc. This library must be installed in Python before it can be accessed within the script. Otherwise, an error message “ModuleNotFoundError” will appear.

This Python guide will illustrate various causes and solutions of “No module named sklearn” in Python.

So, let’s get started!

Reason: sklearn Module Not Installed in Python

The main reason which causes this “No module named “sklearn” module error is when a user tries to import the “sklearn” library without installing it in Python. The error also appears when the package is installed in an “incorrect version” of Python. The other reason which causes this error is installing the package “globally” through “cmd” but not in a virtual environment.

The above snippet shows the module not found error when the “sklearn” library is imported in the program.

Solution 1: Install sklearn Module in Python (Windows)

To install “sklearn” library in Python, follow the given below steps one by one:

Step 1: Open Command Prompt Terminal

Open the “cmd” through the start menu’s search bar:

Step 2: Install sklearn Library Using pip

After, opening the “cmd”,  install the “sklearn” library using the given below command:

> pip3 install scikit-learn scipy matplotlib numpy

After typing the command “pip3 install scikit-learn scipy matplotlib numpy” in cmd, the installation process started, and within a few minutes, the “sklearn” was successfully installed in our system.

If the above command shows any permission error, then the given below command is used for installing the “sklearn” in Python:

> pip install scikit-learn scipy matplotlib numpy --user

To install this library in Anaconda and Jupyter Notebook, following commands are used:

# for Anaconda
conda install -c anaconda scikit-learn

# for Jupyter Notebook
!pip install scikit-learn scipy matplotlib numpy

Step 3: Verification of sklearn Library in Python

To verify the installation, the following pip command is used in the command prompt terminal:

> pip show scikit-learn

This command shows the name, version, and location of the installed library.

How to Uninstall the “sklearn” Library From Python?

To uninstall the “sklearn” library from Python, type the following command in the terminal:

> pip uninstall scikit-learn

The above snippet verified that the “sklearn” library was successfully uninstalled from Python.

Solution 2: Install the sklearn Module in Python (For Linux)

To install the “sklearn” module in Python Linux, use the given below “pip” command:

# Installing Library
$ pip3 install -U scikit-learn
#verify installation
$ python3 -m pip show scikit-learn

The successful installation of the above command removes the module not found error.

Conclusion

The “ModuleNotFoundError: No mobile named sklearn” occurs when the user tries to import the “sklearn” module without installing it in Python. To resolve this error, install the “sklearn” library using the “pip” command in Python. To resolve the error in Python Linux, utilize the “pip” or “apt” command in the terminal. The article presented the reason and solutions for the “no module named sklearn” error in Python.