Course
Scikit-learn
, often abbreviated as sklearn
, is a widely used Python library for data analysis and modeling tasks, including machine learning. New users who are eager to try sklearn
might become frustrated, however, when encountering an unfamiliar error message that isn’t easy to troubleshoot.
This tutorial will explore fixing the No module named 'sklearn'
error message so we can get back on track using sklearn’s extensive collection of algorithms and tools for data analysis and modeling. In the process, we will also learn more general skills in troubleshooting the installation of packages, making us stronger Python programmers. For a refresher on the scikit-learn
package, feel free to check out our Scikit-Learn Cheat Sheet before we start.
Understanding the No module named 'sklearn' Error Message in Python
Let’s take a look at the following image. Here, we tried to import sklearn
and got back a ModuleNotFoundError
exception.
ModuleNotFoundError in Python environment
As we can infer from the name, ModuleNotFoundError
is a specific type of ImportError
that indicates that Python cannot find a specific module. In this case, the module is sklearn
, but the module could have been any module, such as numpy
or pandas
.
Our Introduction to Data Science in Python contains lessons with many examples of how to import and work with modules such as numpy
.
Common Causes of the Python ModuleNotFoundError Message
You might encounter the No module named 'sklearn'
error message for several common reasons. Chief among them are:
-
Sklearn not installed: The most common cause of this error is that
scikit-learn
is not installed in our Python environment. -
Incorrect installation: Sometimes,
scikit-learn
may be installed but not installed correctly, leading to issues importing the library modules. -
Environment configuration or Python path issues: This happens most often when working in a virtual environment or using a different Python interpreter.
Luckily, by taking a step-by-step approach, we will likely resolve our issue. Let’s keep going.
Troubleshooting the Python ModuleNotFoundError Error Message
Verify scikit-learn installation
The first step in troubleshooting the error is to ensure that scikit-learn
is installed correctly in our Python environment. We can do this by running the following command in our terminal or command prompt:
py -m pip list
This produces an alphabetical list of all the libraries installed in our Python environment. We scroll through our list and make sure scikit-learn
is on it.
Example list of installed packages
If, after scrolling through our list, we discover that scikit-learn
is not installed, we can install it using either the pip
or conda
command, depending on our environment:
pip install scikit-learn
conda install scikit-learn
For more information on using package managers such as pip
, check out our Pip Python Tutorial for Package Management.
Restart Python kernel
If we’re working in a Jupyter Notebook or an IDE like Spyder, our next step is to consider restarting our Python kernel. We do this because we know that, even though we have recently installed or re-installed scikit-learn
, the change might not be immediately available in our current interactive session. Restarting the kernel reloads the environment and library paths. This is an easy and essential step that can fix all sorts of errors.
Restarting our Jupyter kernel
Check environment variables and Python path
Next, we must ensure that our Python environment is configured correctly to locate the scikit-learn
package. This step is crucial because it helps ensure that Python searches the correct directories for our installed scikit-learn
package.
Here, we check the PYTHONPATH
environment variable to ensure it includes the directory where scikit-learn
is installed. We can do this by running the following command in our Python script or interpreter:
import sys
print(sys.path)
By running import sys
followed by print(sys.path)
, we can see the directories Python is currently checking. If the path to the directory where scikit-learn
is installed isn't in our list, Python won't be able to find and import scikit-learn
.
Finally, we can verify that the Python interpreter being used corresponds to the environment where scikit-learn
is installed. Follow our detailed tutorial to learn How to Setup a Data Science Environment on your computer.
Reinstall scikit-learn
If all else fails, consider reinstalling scikit-learn
to address any potential issues with the installation. It’s possible that the file got corrupted during the download or installation process due to network issues, interruptions, or disk errors. It’s also possible that there were dependency conflicts from previous installations.
We can uninstall scikit-learn using pip
or conda
and then reinstall it to ensure a clean installation.
pip uninstall scikit-learn
pip install scikit-learn
conda uninstall scikit-learn
conda install scikit-learn
Verifying Installation and Environment Setup
As a final step, once we believe we have resolved the No module named 'sklearn' error
, we may want to verify that scikit-learn
is installed correctly and that our environment is configured properly, just for peace of mind.
We can do this by importing scikit-learn
in a Python script or interpreter and checking for import errors.
import sklearn
print(sklearn.__version__)
If we don't encounter any errors, scikit-learn
should be installed correctly, and we can start using it in your machine-learning projects!
Conclusion
Encountering the No module named 'sklearn'
error can be frustrating, but with the right troubleshooting steps, we can quickly resolve it and return to our machine-learning tasks.
I encourage you to learn more about the underlying theory of machine learning and some common models, such as k-means clustering, logistic regression, or svm. You can also learn some coding best practices to avoid more frustrating error messages going forward.
Become a ML Scientist

I am a PhD with 13 years of experience working with data in a biological research environment. I create software in several programming languages including Python, MATLAB, and R. I am passionate about sharing my love of learning with the world.