How to Solve "ModuleNotFoundError: No module named 'sklearn'" in Python
The error ModuleNotFoundError: No module named 'sklearn'
in Python seems straightforward, but it often trips up beginners.
The key is that the package name you install (scikit-learn
) is different from the module name you import (sklearn
).
This guide explains the error, provides step-by-step installation instructions, and covers troubleshooting for various environments.
Understanding the sklearn
vs. scikit-learn
Distinction
This is the most important point to understand:
scikit-learn
: This is the name of the package you install usingpip
(orconda
). It's the name on PyPI (the Python Package Index).sklearn
: This is the name of the module you import within your Python code (e.g.,from sklearn import datasets
).
You install scikit-learn
, but you import sklearn
. This difference is the source of the confusion.
Basic Installation with pip
The standard installation command is:
pip install scikit-learn
# OR, for Python 3 (might be pip3, pip3.10, etc.)
pip3 install scikit-learn
# OR, if pip isn't in your PATH:
python -m pip install scikit-learn
python3 -m pip install scikit-learn
# OR, for Windows 'py' launcher:
py -m pip install scikit-learn
It's highly recommended to also install scipy
, matplotlib
, and numpy
, which are often used alongside scikit-learn
for machine learning tasks:
pip install scikit-learn scipy matplotlib numpy
# OR (adapt as above)
pip3 install scikit-learn scipy matplotlib numpy
# etc.
Troubleshooting ModuleNotFoundError
If you still get the error after installation, follow these steps:
Virtual Environments (Critical!)
Always use virtual environments. They isolate project dependencies and prevent conflicts.
-
Create:
python3 -m venv venv # Recommended
# OR (if the above fails)
python -m venv venv
# OR (Windows)
py -m venv venv -
Activate:
-
Linux/macOS (bash/zsh):
source venv/bin/activate
-
Windows (Command Prompt):
venv\Scripts\activate.bat
-
Windows (PowerShell):
venv\Scripts\Activate.ps1
(If PowerShell gives a script execution error, run this once as administrator:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
)
-
-
Install inside the activated environment:
pip install scikit-learn scipy matplotlib numpy
Verifying Installation
Confirm installation within the correct environment:
pip show scikit-learn
# OR
python -m pip show scikit-learn
The output should show package information, including the Location
. This path MUST be within your virtual environment (if used), or the correct site-packages of the interpreter.
Checking Your Interpreter (IDE/Terminal)
- IDE (VS Code, PyCharm, etc.): Make sure your IDE is configured to use the Python interpreter where
scikit-learn
is installed. In VS Code, use "Python: Select Interpreter". In PyCharm, check project settings. - Terminal: If running from the terminal, ensure you've activated your virtual environment.
Naming Conflicts (Shadowing)
Ensure you don't have a file or directory named sklearn.py
(or sklearn
) in your project or anywhere on your PYTHONPATH
. This will prevent Python from finding the installed scikit-learn
package. Rename your file/directory if needed.
Reinstalling scikit-learn
If you suspect a corrupted installation:
pip uninstall scikit-learn
pip install scikit-learn
Upgrading pip
It's a good general practice to keep pip
updated:
python -m pip install --upgrade pip
#OR
python3 -m pip install --upgrade pip
#OR
py -m pip install --upgrade pip
Installation in Specific Environments
Anaconda
conda install -c conda-forge scikit-learn
- It is recommended to use
conda-forge
when installing open-source packages in Anaconda.
Jupyter Notebook
!pip install scikit-learn scipy matplotlib numpy
- The
!
exclamation mark allows for running shell commands inside a Jupyter cell.
Basic scikit-learn
Example
Once installed, you can use scikit-learn
(imported as sklearn
):
from sklearn import datasets, svm, metrics
# Load a sample dataset (digits)
digits = datasets.load_digits()
# Create a classifier: a support vector classifier
clf = svm.SVC(gamma=0.001, C=100.)
# Train on the first half of the data
clf.fit(digits.data[:-1], digits.target[:-1])
# Predict on the last sample
predicted = clf.predict(digits.data[[-1]])
print(f"Prediction: {predicted}")
print(f"Actual: {digits.target[-1]}")
Conclusion
The ModuleNotFoundError: No module named 'sklearn'
error is resolved by installing the scikit-learn
package.
The most common problems are failing to install it in the correct environment (use virtual environments!), misconfiguring your IDE, or shadowing the package with a local file.
By following these steps, you can correctly install and use scikit-learn
for your machine learning projects.