ModuleNotFoundError No module named 'sklearn'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
ModuleNotFoundError: No module named 'sklearn' means the active Python interpreter cannot find scikit-learn in its current environment. The error is usually not about the import statement itself. It is almost always an installation, interpreter, or environment mismatch problem, especially when notebooks, IDEs, and terminal sessions point at different Python executables.
Install The Correct Package
The import name is sklearn, but the package you install is scikit-learn.
After installation, this import should work:
Using python -m pip is better than calling pip directly because it makes the target interpreter explicit.
If your machine has multiple Python installations, that detail matters.
Confirm Which Python You Are Running
A frequent cause is installing scikit-learn into one environment and executing the script in another.
Both commands should refer to the same environment. If pip show lists a package location unrelated to the interpreter from sys.executable, the import error makes sense.
The same check is useful inside notebooks.
If the notebook kernel is not the interpreter where you installed the package, import sklearn will still fail.
Virtual Environments And Conda Environments
If you use venv, activate the environment before installing and running code.
For conda, install inside the active environment.
The rule is simple: install and run from the same environment.
Verify The Installation With Real Code
Do not stop at pip output. Run a tiny script that imports scikit-learn and uses it.
If this works, the package is installed correctly and the import path is valid.
Other Causes That Look Similar
The package may be installed and the import can still fail for less obvious reasons:
- your project contains a file named
sklearn.py - your IDE is configured to use a different interpreter
- a Jupyter kernel still points to an old environment
- the installation was interrupted or partially upgraded
You can check for local shadowing by listing the project files.
If you see sklearn.py or a local sklearn directory, rename it. Python may be importing that local name first.
Common Pitfalls
The most common mistake is running pip install scikit-learn in one shell and then launching code with a different python binary.
Another mistake is trying to install a package literally named sklearn. The distribution to install is scikit-learn, even though the import is sklearn.
Developers also often forget that IDEs and notebooks may use a different interpreter from the terminal. The package can be installed correctly and still remain invisible to the active runtime.
Finally, be careful about file shadowing. A local file named sklearn.py can produce import errors that look like missing installation.
Summary
- Install scikit-learn with
python -m pip install scikit-learn. - Import it with
import sklearn. - Check
sys.executableto confirm which interpreter your code is using. - Make sure installation and execution happen in the same virtual or conda environment.
- Watch for local files that shadow the real package name.
- Verify the installation with a small working import, not just with
pipoutput.

