TutorialStudyMite

Fixing the 'Module Not Found' Issue in Jupyter Notebook

Beginner friendly

Track completion, mastery, and revision.

The ModuleNotFoundError is one of the most common and frustrating errors you can encounter when working in a Jupyter Notebook. It occurs when the Python interpreter running your notebook cannot locate the library you are trying to import.

While it sounds simple, this error can stem from several underlying issues—ranging from a missing installation to complex virtual environment mismatches. In this guide, we will explore the common causes of this error and provide step-by-step solutions to resolve them.


Cause 1: The Module is Not Installed

The most straightforward cause of this error is that the package has not been installed on your system.

For example, if you try to import pandas without installing it first, Python will throw a ModuleNotFoundError.

import pandas as pd
# Output: ModuleNotFoundError: No module named 'pandas'

Solution

You need to install the missing module using a package manager like pip or conda in your terminal.

To install pandas via pip:

pip install pandas

To install pandas via conda (if you are using the Anaconda distribution):

conda install pandas

Cause 2: Environment Mismatch (The Jupyter Kernel Issue)

This is the most common and confusing scenario: you installed the package in your terminal, but Jupyter Notebook still claims the module does not exist.

This happens because Jupyter Notebook runs on a specific Kernel (a distinct Python environment), which may not be the same Python environment where you ran the pip install command.

The Quick (But Unreliable) Fix

Many users try to install packages directly inside a Jupyter Notebook cell using the exclamation mark (!) to run a shell command:

!pip install pandas

⚠️ Warning: While this sometimes works, it can fail. The !pip command targets the default Python installation in your operating system's $PATH variable, which might still differ from the active Python kernel running your current Jupyter Notebook.

The Robust Fix

To guarantee that a package is installed in the exact Python environment currently running your Jupyter Notebook, use the sys library to target the active executable:

import sys
!{sys.executable} -m pip install pandas

How It Works:

  • sys.executable dynamically retrieves the absolute file path of the Python interpreter running your active Jupyter kernel.
  • The -m pip flag ensures that pip executes specifically for that Python instance, installing the package in the correct directory.

Cause 3: Mismatched Package and Import Names

Sometimes, the name you use to install a package is different from the name you use to import it in your code.

For example, you cannot import scikit-learn or PyYAML directly using their installation names.

Common Examples:

| To Install (Terminal) | To Import (Python Code) | | :--- | :--- | | pip install scikit-learn | import sklearn | | pip install PyYAML | import yaml | | pip install opencv-python | import cv2 |

Solution

Double-check the official documentation of the library you are using to ensure you are using the correct name in your import statement.


Cause 4: Typos and Local File Shadowing

If you have verified the installation and environment but still see the error, check for typos or file naming conflicts.

1. Typos in the Import Statement

A simple typographical error will trigger the error:

# Incorrect
import pandass as pd 

# Correct
import pandas as pd

2. Local File Shadowing (Naming Conflicts)

If you name a local Jupyter Notebook or Python file the same name as an installed library (e.g., naming your file pandas.py), Python will attempt to import your local file instead of the actual library. This is known as shadowing.

💡 Tip: Never name your local scripts, files, or notebooks after popular Python libraries (such as math.py, random.py, csv.py, or pandas.py).


Summary Checklist

When troubleshooting a ModuleNotFoundError in Jupyter Notebook, run through this quick checklist:

  1. Check Spelling: Is the module name spelled correctly in your import statement?
  2. Verify the Import Name: Does the import name differ from the installation name (e.g., sklearn vs. scikit-learn)?
  3. Install via the Active Kernel: Run import sys; !{sys.executable} -m pip install <package_name> inside a notebook cell to ensure it installs to the correct environment.
  4. Restart the Kernel: After installing a new package, go to the Jupyter menu and select Kernel -> Restart to refresh the environment.

Finished reading?

Was this helpful?

Your feedback shapes better tutorials for everyone.