Install Scikit-Learn so it actually imports in your notebook
How to install scikit-learn for Jupyter Notebook using pip or a registered kernel, verify the install, and fix the sklearn import errors notebooks are prone to.
Updated August 26, 2023
Scikit-Learn is the standard library for classical machine learning in Python: classification, regression, clustering, and the preprocessing tools around them. Getting it working inside a Jupyter Notebook is usually easy, but it goes wrong in one particular way often enough to be worth understanding before you start.
The one thing to know first
A notebook does not run your code in your terminal. It runs code in a kernel — a separate Python process that Jupyter starts for you, with its own set of installed packages.
This means activating a virtual environment in your terminal does not change what your notebook can import. If you activate an environment, pip install scikit-learn, then open a notebook and get ModuleNotFoundError, nothing went wrong with the install — you installed into one Python and the notebook is running a different one.
Almost every problem in this article is a version of that. Keep it in mind and the rest is straightforward.
Prerequisites
- Python 3.11 or newer. The current scikit-learn release (1.9.0) requires it. On an older Python, pip won’t fail — it will quietly install an older scikit-learn that still supports your version, so you end up with an install that works but doesn’t match current tutorials.
- Jupyter Notebook, installed either through pip or Anaconda.
Scikit-Learn is built on NumPy and SciPy, and pulls in joblib and threadpoolctl. Those all install automatically. Matplotlib is not included — it’s an optional extra, so install it separately when you want to plot your results.
If you already have a notebook open
Run this in a cell:
%pip install scikit-learn
%pip is a notebook magic command, not a shell command. It installs into whichever Python is running the current kernel, which is exactly what you want — it removes the guesswork about which environment you’re installing into.
You’ll see a note when it finishes:
Note: you may need to restart the kernel to use updated packages.
Restart the kernel (Kernel → Restart), then verify it worked:
import sklearn
print(sklearn.__version__)
1.9.0
Your version number will differ. That’s the whole job for most people.
Use %pip, not !pip. The ! form shells out to whatever pip is first on your PATH, which lands you back in the mismatch problem.
Setting up from scratch
If you’re building a project environment rather than patching a running notebook, do it in this order so the kernel and the packages stay together.
Create and activate a virtual environment:
python3 -m venv .venv
source .venv/bin/activate
On Windows, use py -m venv .venv and then .venv\Scripts\activate.
Install everything into it:
python -m pip install --upgrade pip
python -m pip install jupyter scikit-learn
Use python -m pip rather than a bare pip. A bare pip is a separate program on your PATH and isn’t guaranteed to belong to the Python you’ll actually run; python -m pip asks a specific interpreter to install into itself.
Now register this environment as a Jupyter kernel so you can select it from inside any notebook:
python -m ipykernel install --user --name=myproject --display-name="Python (myproject)"
Launch Jupyter and pick that kernel from the Kernel → Change Kernel menu:
jupyter notebook
Registering the kernel is the step people skip, and it’s the one that makes the environment reachable from the notebook UI. Without it, Jupyter will happily start a kernel from some other Python and you’ll be back to ModuleNotFoundError.
Using Anaconda instead
If you already work in Anaconda, create an environment and install scikit-learn into it with conda install scikit-learn, then install ipykernel in the same environment so it appears in the kernel list. Don’t mix conda and pip installs of the same package in one environment — that’s a common source of the import errors below. See installing Scikit-Learn in Anaconda for the full walkthrough.
Verifying the installation
Run this in a notebook cell, not a terminal — the point is to confirm the kernel can see the library:
import sys
import sklearn
print(sys.executable)
print(sklearn.__version__)
The first line tells you which Python your notebook is actually running. If you set up an environment above, that path should be inside it. The second confirms scikit-learn is importable from there.
Troubleshooting
ModuleNotFoundError: No module named 'sklearn'
The install succeeded, but your notebook is running a different Python than the one you installed into. This is by far the most common problem, and re-running the install in your terminal will not fix it.
Find out which Python the kernel is using:
import sys
print(sys.executable)
Compare that path to the environment you installed into. If they don’t match, you have two options:
- Quick fix: run
%pip install scikit-learnin a cell and restart the kernel. This installs into the kernel you’re already using. - Durable fix: register your environment as a kernel with the
ipykernelcommand above, then switch to it via Kernel → Change Kernel.
ImportError: DLL load failed
Almost always Windows, and usually one of three things: a missing Microsoft Visual C++ Redistributable, a NumPy/SciPy version mismatch against the compiled scikit-learn, or an environment where conda and pip have both installed the same package.
Upgrading the numerical stack together sometimes resolves the mismatch case:
python -m pip install --upgrade numpy scipy scikit-learn
If that doesn’t work, a clean virtual environment installed entirely with pip is the reliable fix, and faster than diagnosing which of the three it was.
The kernel dies or restarts when you import sklearn
Usually a partially-installed or mixed environment. Rebuild it from scratch rather than reinstalling on top.
FAQs
1. What is Scikit-Learn used for?
Classical machine learning: classification, regression, clustering, dimensionality reduction, and the preprocessing and model-evaluation tooling around them. It’s built on NumPy and SciPy.
2. Can I use Scikit-Learn with other Python libraries?
Yes. It’s designed to work with Pandas for data manipulation and Seaborn or Matplotlib for visualization. Scikit-Learn estimators accept pandas DataFrames directly.
3. How do I update Scikit-Learn?
From a cell in the notebook you want to update:
%pip install --upgrade scikit-learn
Restart the kernel afterward. See updating Scikit-Learn in Jupyter Notebook for more.
4. Is Scikit-Learn suitable for deep learning?
No. It’s built for traditional machine learning algorithms. For neural networks, use PyTorch or TensorFlow.
5. Why does my notebook say sklearn isn’t installed when pip says it is?
Because your notebook’s kernel is a different Python than your terminal. See the troubleshooting section above.
6. Can I install Scikit-Learn without Anaconda?
Yes, and it’s the recommended path for most people — pip inside a virtual environment, as described above. Anaconda is one option, not a requirement.
Where to go next
Once print(sklearn.__version__) returns a version number in a notebook cell, you’re set up. From here:
- Check whether Scikit-Learn is installed or check which version you have
- Import Scikit-Learn in a notebook, or in Google Colab, where packages work a little differently
- Start using Scikit-Learn and make your first predictions
If you need scikit-learn outside a notebook, the general installation guide covers plain Python projects.


