The Python error “No module named ‘sklearn’” usually means that scikit-learn is not installed in the Python environment running your code, or Python is using a different environment than the one where you installed it. You can typically fix this by installing the package with python -m pip install scikit-learn and verifying that your script uses the correct interpreter.
Quick Answer
To fix the No module named 'sklearn' error, install scikit-learn using python -m pip install scikit-learn. Then restart your Python session and try importing sklearn again. If the error persists, check your virtual environment, Python interpreter, Jupyter kernel, or IDE configuration.
What Does “No Module Named sklearn” Mean?
The error ModuleNotFoundError: No module named 'sklearn' is a Python import error. It occurs when Python cannot locate the sklearn module in the environment currently executing your program.
Scikit-learn is a popular machine learning library used for classification, regression, clustering, data preprocessing, model selection, and predictive analytics.
Although the library is called scikit-learn, its Python import name is sklearn. This difference is important because you must install the package using its distribution name but import it using its module name.
For example:
from sklearn.model_selection import train_test_splitfrom sklearn.ensemble import RandomForestClassifier
If scikit-learn is missing from your active environment, Python may display an error like this:
ModuleNotFoundError: No module named 'sklearn'
The error does not necessarily mean that your Python installation is broken. In most cases, installing the correct package or selecting the correct Python environment resolves the issue.
Why Does Python Say “No Module Named sklearn”?
Several common causes can trigger this error.
1. Scikit-learn Is Not Installed
The most common reason is that scikit-learn has not been installed in your current Python environment.
Python includes many built-in modules, but third-party libraries such as scikit-learn must generally be installed separately.
2. You Installed the Wrong Package Name
The package you need is named scikit-learn, not sklearn.
Use:
python -m pip install scikit-learn
Avoid using pip install sklearn as your standard installation command. The sklearn package name on PyPI is deprecated, and the supported package to install is scikit-learn.
3. Python Is Using a Different Environment
You might have multiple Python installations on your computer. For example, one installation may contain scikit-learn while another does not.
This often happens when using:
- Python virtual environments
- Conda environments
- Visual Studio Code
- PyCharm
- Jupyter Notebook
- Google Colab
- Windows Python installations
4. Your Jupyter Kernel Is Different
Jupyter Notebook may run a kernel connected to a different Python environment than your terminal.
Even if installation succeeds in the terminal, your notebook may still report that sklearn is missing.
5. An Installation Failed or Was Interrupted
A network problem, incompatible Python version, or package dependency conflict can prevent a successful installation. In these cases, check the installation output for errors instead of assuming the package was installed.
How to Fix the No Module Named sklearn Error
The best solution depends on where you run your Python code. Start with the basic installation method, then follow the instructions for your specific development environment if necessary.
Method 1: Install Scikit-Learn Using pip
For most Python users, installing scikit-learn with pip is the simplest solution.
Step 1: Open your terminal or command prompt.
On Windows, open Command Prompt or PowerShell. On macOS or Linux, open the Terminal application.
Step 2: Run the installation command.
python -m pip install scikit-learn
This command uses pip associated with the Python interpreter invoked by python.
If your system uses python3, try:
python3 -m pip install scikit-learn
Step 3: Verify the installation.
Run:
python -c "import sklearn; print(sklearn.__version__)"
If Python displays a version number, the import works in that environment.
Step 4: Run your original script again.
Try your original import statement:
import sklearn
If it works, the issue is resolved.
Method 2: Use pip3 on macOS or Linux
Some macOS and Linux systems distinguish between Python commands, particularly when multiple versions are installed.
If pip installs packages into the wrong Python environment, try:
python3 -m pip install scikit-learn
Then verify the package using the same interpreter:
python3 -c "import sklearn; print(sklearn.__version__)"
The important principle is to use the same Python command for installation and execution.
Method 3: Fix the Error in a Virtual Environment
A Python virtual environment keeps project dependencies separate from other Python projects. This is useful when working on machine learning applications or maintaining multiple projects with different package requirements.
Create and activate a virtual environment
On Windows:
python -m venv .venv
.venv\Scripts\activate
python -m pip install scikit-learn
On macOS or Linux:
python3 -m venv .venv
source .venv/bin/activate
python -m pip install scikit-learn
After activation, verify the installation:
python -c "import sklearn; print(sklearn.__version__)"
Run your Python program from the activated environment.
Why this works: The virtual environment has its own installed packages. Installing scikit-learn there makes it available to Python processes using that environment, rather than relying on packages installed elsewhere.
If you already have a virtual environment, activate it instead of creating another one unnecessarily.
Method 4: Fix “No Module Named sklearn” in Jupyter Notebook
Jupyter Notebook users often encounter this error because the notebook kernel and terminal use different Python interpreters.
First, check which Python executable your notebook is using:
import sysprint(sys.executable)
Next, install scikit-learn into that interpreter’s environment by running this cell:
%pip install scikit-learn
When installation finishes, restart the notebook kernel if necessary. Then test:
import sklearnprint(sklearn.__version__)
Using %pip inside a notebook generally helps target the environment associated with the active kernel.
If the error persists, confirm that the selected kernel is the environment where you installed the package.
Method 5: Fix the Error in Google Colab
Google Colab usually provides many popular machine learning libraries, but a package may be unavailable or need installation in a particular runtime.
Run this in a notebook cell:
%pip install scikit-learn
Then test the import:
import sklearnprint(sklearn.__version__)
If installation reports success but importing still fails, restart the runtime and execute the import again.
Remember that Colab runtimes are temporary. Packages installed in one runtime session may need to be installed again after the environment resets.
Method 6: Fix the Error in Visual Studio Code
Visual Studio Code can have multiple Python interpreters, making interpreter selection a frequent source of import errors.
Follow these steps:
- Open your Python project in Visual Studio Code.
- Open the Command Palette.
- Search for Python: Select Interpreter.
- Select the interpreter or virtual environment used by your project.
- Open a new terminal in VS Code.
- Install scikit-learn using the selected environment.
python -m pip install scikit-learn
Verify the import:
python -c "import sklearn; print(sklearn.__version__)"
If the terminal import works but the editor still displays a warning, reload VS Code or restart its Python analysis service. An editor warning does not always mean the program itself will fail.
Method 7: Fix the Error in PyCharm
PyCharm projects can use project-specific virtual environments. Installing a library into another environment will not automatically make it available to the current project.
To fix the problem:
- Open your project in PyCharm.
- Navigate to the project interpreter settings.
- Confirm that the selected interpreter is correct.
- Install
scikit-learninto that interpreter using the package manager or its terminal. - Run your Python file again.
You can also use the PyCharm terminal:
python -m pip install scikit-learn
If the error continues, compare the interpreter shown in PyCharm’s project settings with the executable returned by sys.executable in your script.
Compare the Best Solutions for Different Environments
| Environment | Recommended installation method | Important check |
|---|---|---|
| Windows Command Prompt | python -m pip install scikit-learn | Match the Python interpreter |
| macOS or Linux | python3 -m pip install scikit-learn | Verify the active Python version |
| Jupyter Notebook | %pip install scikit-learn | Check the active kernel |
| Google Colab | %pip install scikit-learn | Restart the runtime if needed |
| VS Code | python -m pip install scikit-learn | Select the correct interpreter |
| PyCharm | Project interpreter or terminal | Install into the project environment |
| Conda | conda install scikit-learn | Activate the correct Conda environment |
For most users, the key is not choosing a particular editor or installation tool. It is making sure the package is installed into the exact environment that executes the code.
How to Fix sklearn Import Errors With Conda
If you use Anaconda or Miniconda, installing scikit-learn with Conda is often the most straightforward option.
Open Anaconda Prompt or a terminal with Conda available, activate your environment, and run:
conda activate myenv
conda install scikit-learn
Replace myenv with your actual environment name.
To check which environments are available, use:
conda env list
After installation, verify the package:
python -c "import sklearn; print(sklearn.__version__)"
If you prefer pip inside a Conda environment, that can also work. However, avoid mixing package managers unnecessarily in complicated environments, especially when dependencies are already managed by Conda.
How to Diagnose the Problem When Installation Does Not Work
If you have installed scikit-learn but still see ModuleNotFoundError, investigate the environment before repeatedly reinstalling packages.
Check the Python Interpreter
Run this code inside the failing program or notebook:
import sysprint(sys.executable)
This prints the path to the Python executable running your code.
Then run pip through that interpreter:
python -m pip show scikit-learn
If the package information is missing, you may be checking a different interpreter or environment than the one where installation occurred.
For a more precise check, run the following from the relevant environment:
python -m pip --version
The output shows which Python installation is associated with pip.
Check Whether the Package Is Installed
Run:
python -m pip show scikit-learn
If scikit-learn is installed, pip normally displays information such as its version, location, and dependencies.
If it is not found, install it:
python -m pip install scikit-learn
Do not confuse the distribution name, scikit-learn, with the import name, sklearn.
Check Your Python Version
Scikit-learn releases support specific Python versions. An older Python installation may not be compatible with the version pip is trying to install.
Check your version:
python --version
If the installer reports that no compatible distribution is available, consult the official scikit-learn installation documentation and select a compatible Python and scikit-learn combination.
Do not assume that upgrading every package will fix the problem. Address the specific compatibility message shown during installation.
Look for Local Naming Conflicts
Occasionally, a local file or folder can interfere with imports.
For example, a project containing a file named sklearn.py or a folder named sklearn may cause unexpected import behavior.
Check your project directory for conflicting names. If you find one, rename it and remove any associated __pycache__ directory if appropriate. Then restart Python and try the import again.
This issue may produce an error different from a genuinely missing module, so examine the complete traceback before making changes.
Common Mistakes to Avoid When Fixing the sklearn Error
Some troubleshooting approaches can create additional problems instead of solving the original issue.
- Installing
sklearninstead ofscikit-learn: Use the official distribution name in installation commands. - Using
pipwithout checking the interpreter: Preferpython -m pipor the matchingpython3 -m pipcommand. - Forgetting to activate a virtual environment: Activate the intended environment before installing dependencies.
- Ignoring installation errors: Read the terminal output for dependency conflicts, permission problems, or unsupported Python versions.
- Restarting the wrong environment: Restart the notebook kernel or IDE session that actually runs your code.
- Reinstalling Python unnecessarily: A missing package usually does not require reinstalling Python itself.
- Confusing editor warnings with runtime errors: Confirm whether the import fails during execution or only appears underlined in your editor.
How to Add Scikit-Learn to requirements.txt
If you are developing a Python project that other people will run, include scikit-learn in your dependency file.
Add this line to requirements.txt:
scikit-learn
Then install the project’s dependencies:
python -m pip install -r requirements.txt
For reproducible projects, specify a tested version, such as scikit-learn==1.7.2, only if that version is compatible with your Python environment and project requirements.
This helps teammates and deployment environments install the dependencies needed to run your machine learning application.
Frequently Asked Questions
1. Why am I getting “No module named sklearn” even after installing it?
Your package may be installed in a different Python environment from the one running your code. Check sys.executable, install scikit-learn into the matching environment, and restart your notebook kernel or development session if necessary.
2. Should I install sklearn or scikit-learn?
Install scikit-learn using pip or Conda. Import it as sklearn in Python. The installation package name and import module name are different.
3. How do I install sklearn in Python 3?
Run python3 -m pip install scikit-learn in your terminal. Verify it with python3 -c "import sklearn; print(sklearn.__version__)", using the same Python installation that runs your application.
4. How do I fix the sklearn error in Jupyter Notebook?
Run %pip install scikit-learn in a notebook cell. If the error remains, inspect sys.executable to confirm the notebook kernel uses the environment where the library is installed.
5. Is scikit-learn included with Python?
No. Scikit-learn is a third-party machine learning library and is not included in a standard Python installation. You generally need to install it separately using pip or Conda.
6. Can I use pip install sklearn?
Use pip install scikit-learn instead. The sklearn distribution name is deprecated, so installing the supported scikit-learn package is the recommended approach.
7. How can I verify that scikit-learn is working?
Run python -c "import sklearn; print(sklearn.__version__)" in your terminal. If it prints a version number without an exception, the import works in that environment.
Final Thoughts
The “No module named sklearn” error is usually caused by a missing scikit-learn installation or a mismatch between Python environments. Start with python -m pip install scikit-learn, verify the installation, and confirm that your application uses the same interpreter.
If you use Jupyter Notebook, VS Code, PyCharm, Conda, or a virtual environment, check its interpreter or kernel before reinstalling packages repeatedly. With the correct environment and compatible dependencies, you can resolve the error and continue building your Python machine learning projects.