Fix the no module named sklearn error in Python
You receive the modulenotfounderror for sklearn when trying to invoke scikit-learn or one of its methods before installing it on your Python development environment. Use conda (for Anaconda environments) or pip to install the scikit-learn library on your development environment and then go ahead and import the library modules into your Python program.
Why the modulenotfound error for scikit-learn is displayed?
Scikit learn is a third party library that is not part of the Python standard library. You will receive the error if invoking scikit-learn modules such as line_model.logistic, cross_validation, utils, externals and so forth before installing scikit learn in your Python environment.
When you try to invoke a scikit-learn in Jupyter (or other IDE such as PyCharm, Spyder, VS Code, Google Colab etc’) without properly installing the library, you will receive an error:
from sklearn import linear_model liner_reg = linear_model.LinearRegression()
Fixing the error
Manually configured environments / virtual environments (virtualenvs):
- Save your program / notebook.
- Invoke terminal or the command prompt.
- In the terminal / command prompt navigate to your Python installation directory (location of python.exe).
- Invoke the Python Package Manager (pip) to install scikit-learn:
pip install sklearn
- Package will be collected and installed.
- Once finished, open your Python IDE or return to your Notebook and import your sklearn module.
If you are using Miniconda or Anaconda distributions:
- Open the terminal (or on Windows, the Anaconda Prompt).
- Activate your conda environment by typing
conda activate <path_to_your_conda_env>
- Then install sklearn:
conda install sklearn
- Last, reopen your Python script or Notebook and import the scikit library
Follow up learning
How to solve the no module named error in Python for Windows and macOS?