Are you facing the error “modulenotfounderror: no module named sklearn“? Yes, you have come to the right place. Today in this tutorial, I will show you how to resolve this error.
This error “no module named ‘sklearn’” occurs when you forget to install “scikit-learn” before importing it or install it in the wrong environment.
[Solution] modulenotfounderror: no module named sklearn
To solve this error, you need to install the scikit-learn Python package using the pip install scikit-learn command.
Here is the command to install scikit-learn on your system:
# If you are in a virtual environment or using Python 2 pip install scikit-learn # For python 3, could also be pip3.10 pip3 install scikit-learn # If you get permissions error you can run command as admin sudo pip3 install beautifulsoup4 pip install scikit-learn--user # If you don't have pip in your PATH environment variable python -m pip install scikit-learn # For python 3 if you don't have pip in your PATH environment variable python3 -m pip install scikit-learn # Using py alias (Only works for Windows) py -m pip install scikit-learn # Alternative for Ubuntu/Debian sudo apt-get install python3-scikit-learn # If you are coidng with Anaconda conda install -c anaconda scikit-learn # If you are using Jupyter Notebook !pip install scikit-learn
When you successfully install sklearn, you can import it like this:
from sklearn import datasets print(dir(datasets))
The Python error “modulenotfounderror no module named ‘sklearn’” can occur for multiple reasons:
- Not having scikit-learn package installed. If you have not installed the sklearn package then you can install it with this command “pip install scikit-learn“.
- Installing the scikit-learn package in a different python version than you are using.
- Installing the package in the global environment when you are using the virtual environment.
- You are using the incorrect version of python on your IDE. (e.g. VS Code)
- Using the file name “sklearn.py“. If you are using a name like this sklearn.py then consider it change to something else.
- Declaring a variable with the name “sklearn“. If you have a variable sklearn change its name to something else.
ModuleNotFoundError: no module named ‘sklearn.cross_validation’
The error “modulenotfounderror no module named sklearn.cross_validation” is different from the above error and the solution is also different.
[Fixed] modulenotfounderror no module named sklearn.cross_validation
If you facing this error then I think you are importing like this:
from sklearn.cross_validation import train_test_split
Now you need to import train_test_split like below to get rid of this error:
from sklearn.model_selection import train_test_split
Conclusion on “modulenotfounderror: no module named sklearn“
Programmers, we discussed the Python error “modulenotfounderror no module named ‘sklearn’“. I hope you have solved your Python error. If you have any questions about the tutorial or are still facing the error, please let us know in the comments section.