Last modified: Aug 11, 2026

Fix ModuleNotFoundError No module named sklearn

Encountering ModuleNotFoundError: No module named 'sklearn' is a common hurdle for Python developers. This error simply means Python cannot find the scikit-learn library in your current environment. It usually appears when you try to import the library without installing it first.

This guide will walk you through the causes and solutions. We will focus on clear, actionable steps. By the end, you will have scikit-learn running smoothly in your project.

Understanding the Error

The error message is straightforward. Python's import system searches for the module in specific directories. If it does not find sklearn, it raises this exception. This often happens because the library is not installed in the active Python environment.

Another common cause is using a different Python interpreter than the one where the package is installed. For example, you might have installed scikit-learn for Python 3.9 but are running a script with Python 3.11. This mismatch leads to the same error.

Let's look at a typical scenario. You write a script to train a simple model.


# my_script.py
from sklearn.linear_model import LinearRegression
import numpy as np

# Sample data
X = np.array([[1], [2], [3]])
y = np.array([2, 4, 6])

model = LinearRegression()
model.fit(X, y)
print(model.predict([[4]]))

When you run this script, you might see the following output in your terminal.


Traceback (most recent call last):
  File "my_script.py", line 1, in 
    from sklearn.linear_model import LinearRegression
ModuleNotFoundError: No module named 'sklearn'

This output is your starting point. The solution is to install the missing package correctly.

Solution 1: Install with pip

The most direct fix is to install scikit-learn using pip. Open your terminal or command prompt. Then, execute the following command.


pip install scikit-learn

This command fetches the latest version from the Python Package Index (PyPI) and installs it. After installation, try running your script again. It should work without errors.

Sometimes, you might have multiple Python versions. In that case, use pip3 instead. This ensures you install the package for Python 3.x.


pip3 install scikit-learn

If you are using a virtual environment, make sure it is activated first. A virtual environment isolates your project dependencies. This prevents conflicts with other projects.

Solution 2: Use a Virtual Environment

Virtual environments are best practice for Python projects. They keep dependencies separate. If you are not using one, you might face version conflicts. Here is how to create and use one.

First, create a virtual environment in your project directory.


python -m venv venv

Next, activate it. On Windows, use the following command.


venv\Scripts\activate

On macOS or Linux, use this command.


source venv/bin/activate

Now, install scikit-learn within this activated environment.


pip install scikit-learn

After this, your script should run without the ModuleNotFoundError. The virtual environment ensures the package is available only to your project. This is a robust solution for managing dependencies.

Solution 3: Check Your Python Environment

Sometimes, the issue is not about installation. It could be that you are running the wrong interpreter. Check which Python you are using.

Run this command to see the current Python path.


which python

Or on Windows, use:


where python

Then, check where pip installs packages. Run this command.


pip show scikit-learn

This shows the location of the installed package. Compare it with your Python path. If they differ, you have a mismatch. You need to align them.

For instance, if your Python is in /usr/bin/python and the package is in /usr/local/lib/python3.8/site-packages, there is a problem. You should install using the specific Python version.


python -m pip install scikit-learn

Using python -m pip ensures you are using pip associated with that specific Python. This is a reliable way to avoid environment mismatches.

Solution 4: Upgrade pip and Setuptools

Outdated pip or setuptools can cause installation issues. These tools are essential for installing packages correctly. Upgrading them often resolves hidden problems.

First, upgrade pip.


pip install --upgrade pip

Then, upgrade setuptools.


pip install --upgrade setuptools

After upgrading, try installing scikit-learn again. This simple step can fix many installation errors. It ensures your environment has the latest tools for package management.

This is especially helpful if you are using an older Python version. Newer versions of scikit-learn may require newer build tools. Keeping pip updated is a good habit.

Solution 5: Install with Conda

If you are using Anaconda or Miniconda, use conda to install scikit-learn. Conda manages packages and environments differently. It is excellent for data science projects.

Run this command in your terminal.


conda install scikit-learn

Conda will resolve dependencies automatically. This often avoids conflicts that pip might encounter. It is a preferred method for many data scientists.

If you are in a conda environment, make sure it is activated first. Use conda activate your_env_name to activate it. Then, run the install command.

This method ensures you get a version compatible with your other conda packages. It is a clean and efficient solution.

Troubleshooting Common Issues

Even after following the steps, you might still face issues. Here are some common problems and their fixes.

Permission Errors: If you see a permission denied error, use --user flag. This installs the package for your user only.


pip install --user scikit-learn

Network Issues: If you are behind a proxy, pip might fail. Configure your proxy settings or use a mirror. This is rare but can happen.

Corrupted Installation: Sometimes, a previous installation is corrupted. Uninstall and reinstall the package.


pip uninstall scikit-learn -y
pip install scikit-learn

This fresh start often resolves persistent issues. It clears out any broken files.

Verify Your Installation

After installation, verify it works. Open a Python interpreter and try to import the library.


import sklearn
print(sklearn.__version__)

If it prints a version number, you are good. For example, you might see something like this.


1.3.2

This confirms the installation was successful. Now, your original script will run without errors.

Testing your setup is always a good practice. It saves you from debugging later. A quick import test is all you need.

Conclusion

Resolving ModuleNotFoundError: No module named 'sklearn' is straightforward. The key is to ensure the package is installed in the correct environment. Start with a simple pip install scikit-learn. If that fails, check your Python environment and use virtual environments.

Remember to keep your tools updated. Use conda if you are in the Anaconda ecosystem. Always verify your installation with a quick import test. These steps will save you time and frustration.

With scikit-learn installed, you can now build powerful machine learning models. Happy coding!