ModuleNotFoundError: No module named 'sklearn'

Posted in Python by Dirk - last update: Feb 02, 2024

Python raises the ModuleNotFoundError: No module named 'sklearn when it is unable to find the sklearn library. The most likely cause is that you didn’t install scikit-learn in the environment where you are running your code. Quick fix: install scikit-learn using: the pip install scikit-learn command

Possible causes and solutions

Scikit-learn (or sklearn) is an open-source machine learning library for the Python programming language. It provides simple and efficient tools for data analysis and modeling, including various machine learning algorithms for tasks such as classification, regression, clustering, dimensionality reduction, and more.

from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score

We have all been there: the dreaded ’no module named error’ - you try running your code, and there it is:

Traceback (most recent call last):
  File "myfile.py", line 3, in <module>
ModuleNotFoundError: No module named 'sklearn'

Luckily, it is easy to fix. Below you can find the most common causes and how to fix them:

‘scikit-learn’ Library is not installed

In your code you are importing the scikit-learn module

import sklearn

which generates the ModuleNotFoundError: No module named 'sklearn’. The the most common cause for this error: the environment where you are running your code doesn’t have the scikit-learn library installed.

You can use the following command in the terminal or command prompt to check if the scikit-learn library is installed:

pip show scikit-learn

If scikit-learn is not installed you will get this result:

WARNING: Package(s) not found: scikit-learn

Solution:

pip install scikit-learn

‘scikit-learn’ Module installed in the Wrong Directory

It is possible that you did install the scikit-learn module, but that the directory where you installed it is not in the Python path.

You can check the Python path in a few different ways:

1. Using sys module in Python:

Open a Python script or the Python interpreter and run the following code:

import sys
print(sys.path)

This will print a list of directories that make up the Python path.

2. Using Command Line

Open a command prompt or terminal and run:

  • Windows
echo %PYTHONPATH%
  • Unix/Linux/macOS
echo $PYTHONPATH

If the PYTHONPATH environment variable is set, it will display the directories in the Python path.

Solution: Move the scikit-learn module to a directory that is included in the Python path or add the scikit-learn module’s directory to the sys.path using sys.path.append().

‘scikit-learn’ not installed in Virtual Environment

Even if you have scikit-learn installed in on your computer, if you are using a virtual environment you cannot access it from this environment.

If you are using virtual environments, ensure that the virtual environment is activated, and that the scikit-learn modules is installed within this environment.

Installation is simple:

pip install scikit-learn

File Naming Conflicts:

It is not really likely - but if you happen to have a sklearn.py file in your environment and you also installed the standard sklearn library it will cause a conflict. Rename your script/module to avoid conflicts with existing module names.

Other articles