How to fix ModuleNotFoundError in Jupyter Notebook & Google Colab
ModuleNotFoundError is one of the first “real” errors most Python and ML students hit in
Jupyter or Google Colab. You install something, import it, and boom:
ModuleNotFoundError: No module named 'sklearn'
The good news: it’s almost always fixable with a few checks. In this guide, we’ll walk through a simple mental model to **diagnose → fix → prevent** ModuleNotFoundError – and why it matters for keeping your notebooks reproducible.
Step 1: Understand what ModuleNotFoundError really means
Python is telling you: “I looked in all the places I know about for this package, and it’s not there.” That could be because:
- the package isn’t installed in this environment,
- you installed it in a different environment (or different Python version),
- you misspelled the module name,
- you’re trying to import a local file that isn’t on the Python path.
Step 2: Fix ModuleNotFoundError in Jupyter Notebook (local)
1. Check which Python you’re using inside the notebook
In a cell, run:
import sys print(sys.executable) print(sys.version)
This tells you which Python binary your notebook is using. Make sure you installed the package into this environment, not some other one.
2. Install the package from inside the notebook
Best pattern: install using pip or conda directly in the notebook:
!pip install scikit-learn import sklearn
If you’re using Conda and Jupyter together, check that the notebook kernel is tied to the correct Conda environment where the package exists.
3. Fix imports for local modules
If you see something like:
ModuleNotFoundError: No module named 'config'
but you have config.py in your project, check your folder structure:
project/ notebook.ipynb config.py
If the notebook is in a subfolder (e.g. notebooks/notebook.ipynb), you may need to
adjust sys.path or move the file so Python can find it.
Step 3: Fix ModuleNotFoundError in Google Colab
1. Install packages in the Colab runtime
Installing Python packages on your laptop doesn’t affect Colab. It runs on Google’s servers, in a fresh environment each time.
Install from a cell at the top of your notebook:
!pip install scikit-learn pandas matplotlib
2. Re-run installs after runtime reset
When Colab disconnects or the runtime resets, installed packages vanish. If you see
ModuleNotFoundError again after a reset, just re-run your installation cell.
3. Handle custom / private packages
If you’ve got a private package on GitHub or a local module, use:
!pip install git+https://github.com/username/repo.git
or mount Google Drive and adjust the Python path so Colab can see your module.
Step 4: Make your fix reproducible (for future-you and others)
Fixing the error once is good. Making sure you never have to debug it again on this project is better. That means:
- keeping a list of your dependencies, and
- capturing the project state when everything works.
1. Capture dependencies in a file
From your notebook environment, run:
!pip freeze > requirements.txt
Commit this file to your project or at least keep it next to your notebook. Anyone wanting to run your project can install from it:
pip install -r requirements.txt
2. Use NoteCapsule to snapshot code + deps + data layout
To go one level beyond just fixing the error, you can use NoteCapsule to create a Capsule:
!pip install notebookcapsule -q
from notebookcapsule import create_capsule
create_capsule(
name="after-fixing-modulenotfounderror",
data_dirs=["./data"]
)
This writes a folder under ./capsules/ with:
- your notebook,
requirements_suggested.txt(from your imports),data_manifest.jsonlisting the files you use,- a
README_template.mdto explain how to run the project.
That way, the next time you (or someone else) run this project, you’re far less likely to hit
ModuleNotFoundError again.
Quick checklist for fixing ModuleNotFoundError
- ✅ Check which Python / environment your notebook is using.
- ✅ Install the package from inside the notebook / Colab runtime.
- ✅ Fix imports and folder structure for local modules.
- ✅ Create a
requirements.txtor similar dependency file. - ✅ Snapshot a working state using NoteCapsule so you don’t lose the fix.
Want fewer “why is this import broken?” moments?
NoteCapsule helps you capture working notebook states with code, dependencies, and data layout in one Capsule – so you can debug once and move on.
Join the NoteCapsule early accessDrop your email on the homepage. We’ll send you a quick setup guide and an example Capsule you can try on your current Jupyter or Colab project.