Skip to content

ModuleNotFoundError: No Module Named 'matplotlib' — Quick Fix Guide

Published on

Updated on

ModuleNotFoundError: No module named 'matplotlib' means the Python interpreter executing your script or notebook does not have matplotlib installed in its active environment. Even if you already ran pip install matplotlib, it usually fails because pip and python point to different environments.

Run the command that matches your environment to fix the error immediately:

# Standard terminal / virtualenv (guarantees pip matches your active python)
python -m pip install matplotlib
 
# macOS / Linux when python points elsewhere
python3 -m pip install matplotlib
 
# Conda environment
conda install matplotlib

If you are inside a Jupyter Notebook or JupyterLab cell, install directly into the active kernel and restart it:

%pip install matplotlib

To confirm which interpreter is actually running your code, print sys.executable:

import sys
print(sys.executable)
Trending reads

Quick Diagnostic Table: Match Your Error to the Fix

Use this matrix to jump straight to the cause affecting your setup:

Symptom or EnvironmentMost Likely CauseImmediate Fix
Fresh Python install or new virtualenvMatplotlib is not installed yetpython -m pip install matplotlib
Works in terminal, fails in Jupyter NotebookJupyter kernel uses a different Python environmentRun %pip install matplotlib in a cell, then restart kernel
Works in terminal, fails in VS Code / PyCharmIDE selected a different interpreterSwitch interpreter to match which python / where python
No module named 'matplotlib.backends.registry'Partial/corrupted upgrade or mixed Matplotlib versionsClean uninstall and reinstall with --no-cache-dir
Fails right after creating a test scriptLocal file named matplotlib.py shadows the packageRename your file and delete __pycache__/
Runs via ./script.py on macOS/LinuxHardcoded #!/usr/bin/python shebangChange shebang to #!/usr/bin/env python3

Fix 1: Install Matplotlib Into the Same Python You Run (pip & Conda)

Running bare pip install matplotlib often installs into a different Python version than python script.py executes. Invoking pip as a module (python -m pip) eliminates that mismatch.

Check your Python and pip versions first to verify they point to the same path:

python --version
python -m pip --version

Install Matplotlib into that exact interpreter:

python -m pip install --upgrade matplotlib

On Windows with the Python Launcher (py), target a specific version explicitly:

py -3.12 -m pip install matplotlib

If you use Conda or Miniconda, activate the environment first and install with conda (or conda-forge for the latest release):

conda activate your_env_name
conda install -c conda-forge matplotlib

Verify the import from the same shell:

import matplotlib
import matplotlib.pyplot as plt
print(matplotlib.__version__, matplotlib.__file__)

Fix 2: Fix Jupyter Notebook and JupyterLab Kernel Mismatches

Jupyter kernels do not automatically follow the virtual environment where you ran pip install. Even if Matplotlib works in your terminal, a notebook running on the base kernel will still throw ModuleNotFoundError.

Check which Python binary your notebook kernel uses by running this in a cell:

import sys
print(sys.executable)

Install Matplotlib directly into that kernel using the %pip magic command (prefer %pip over !pip because %pip is bound to the notebook's active interpreter):

%pip install matplotlib

Restart the kernel (Kernel → Restart Kernel), then re-run import matplotlib.pyplot as plt.

If you want your existing virtual environment or Conda environment to appear as a selectable kernel in Jupyter, register it with ipykernel:

python -m pip install ipykernel matplotlib
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"

Then switch to Python (myenv) from the kernel picker in the top-right corner of Jupyter.

Fix 3: Select the Right Python Interpreter in VS Code and PyCharm

IDEs frequently default to a system Python or a cached base environment while your integrated terminal has a project .venv activated.

In VS Code:

  1. Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on macOS).
  2. Run Python: Select Interpreter.
  3. Pick the interpreter path that matches the virtual environment or Conda environment where you installed Matplotlib (for example ./.venv/bin/python or .\.venv\Scripts\python.exe).
  4. Open a fresh integrated terminal so VS Code activates the selected environment, then re-run your script.

In PyCharm:

  1. Open Settings / Preferences → Project → Python Interpreter.
  2. Confirm matplotlib appears in the installed packages list.
  3. If not, select the correct existing virtualenv/Conda interpreter or click + to install matplotlib into the project interpreter.

Fix 4: Activate Your Virtual Environment Before Running

Installing Matplotlib inside a virtual environment has no effect if you run python outside that environment.

Activate the environment in your current shell before running your script:

# macOS / Linux
source .venv/bin/activate
 
# Windows (PowerShell)
.\.venv\Scripts\Activate.ps1
 
# Windows (Command Prompt)
.\.venv\Scripts\activate.bat

Confirm your prompt shows the environment name and verify sys.executable points inside .venv:

python -c "import sys; print(sys.executable)"
python -m pip install matplotlib

Fix 5: ModuleNotFoundError: No module named 'matplotlib.backends.registry'

If you specifically see:

ModuleNotFoundError: No module named 'matplotlib.backends.registry'

Matplotlib is partially present, but your environment has a version mismatch or corrupted upgrade. The matplotlib.backends.registry module was introduced in Matplotlib 3.9, so this error typically appears when:

  • An IDE helper (such as PyCharm's SciView or a newer Jupyter backend integration) expects Matplotlib 3.9+ while an older Matplotlib 3.8 or earlier is still installed.
  • A previous pip install --upgrade matplotlib was interrupted, leaving mixed files from two versions inside site-packages.
  • Both conda and pip installed Matplotlib into the same environment, or a stale user-site install (~/.local/lib/...) shadows the virtualenv version.

Run a clean uninstall and reinstall inside your active environment to clear out mixed files:

# 1. Remove all pip-installed copies (run twice if both user and venv copies exist)
python -m pip uninstall -y matplotlib
 
# 2. Verify no leftover matplotlib package is still importable
python -c "import matplotlib; print(matplotlib.__file__)"

If step 2 still prints a file path instead of raising ModuleNotFoundError, either uninstall the remaining Conda copy (conda remove matplotlib) or remove that stale matplotlib directory from site-packages (and check for a ~/.local/lib/pythonX.Y/site-packages/matplotlib folder).

Once Python confirms no stray copy remains, install a clean build without cached wheels and verify dependencies:

python -m pip install --no-cache-dir --upgrade matplotlib
python -m pip check
python -c "import matplotlib, matplotlib.backends.registry; print(matplotlib.__version__)"

If you are using Conda exclusively, reinstall from conda-forge instead of mixing pip:

conda remove --force matplotlib
conda install -c conda-forge "matplotlib>=3.9"

Fix 6: Resolve Local File Shadowing (matplotlib.py) and Import Typos

Python searches the current working directory before site-packages. If your project folder contains a file named matplotlib.py or a subfolder named matplotlib/, Python imports your local file instead of the real library—often producing No module named 'matplotlib.pyplot' or No module named 'matplotlib'.

Check whether a local file is shadowing the package:

import os
print([f for f in os.listdir(".") if "matplotlib" in f.lower()])

Rename any conflicting script (for example to plot_demo.py) and delete the generated __pycache__/ folder before running again.

Also double-check import spelling and lowercase casing—Python imports are case-sensitive:

# Correct
import matplotlib
import matplotlib.pyplot as plt

Fix 7: Check Shebang Lines, sys.path, and PYTHONPATH

On macOS and Linux, running an executable script with ./plot.py uses the interpreter declared on line 1. A hardcoded #!/usr/bin/python points to system Python rather than your active virtual environment.

Replace the first line with a portable env shebang so it respects your active PATH:

#!/usr/bin/env python3
import matplotlib.pyplot as plt

If Matplotlib is installed (python -m pip show matplotlib prints a Location), yet Python still cannot import it, inspect sys.path and check whether a custom PYTHONPATH environment variable is overriding site-packages:

python -m pip show matplotlib
python -c "import os, sys; print('PYTHONPATH:', os.environ.get('PYTHONPATH')); print(sys.path)"

Unset any broken PYTHONPATH override (unset PYTHONPATH on macOS/Linux or Remove-Item Env:PYTHONPATH in PowerShell) rather than appending hardcoded paths to sys.path.

FAQ

Why do I still get No module named 'matplotlib' after running pip install matplotlib?

Your pip command installed Matplotlib into a different Python installation than the one running your script or notebook. Run python -m pip install matplotlib (or %pip install matplotlib inside Jupyter) so installation and execution use the exact same interpreter.

How do I fix ModuleNotFoundError: No module named 'matplotlib.backends.registry'?

This error happens when a Matplotlib 3.9+ backend loader encounters an older (< 3.9) or corrupted Matplotlib installation, or mixed Conda/pip installs. Uninstall Matplotlib completely (python -m pip uninstall -y matplotlib), verify no stray copy remains on sys.path, and reinstall cleanly with python -m pip install --no-cache-dir --upgrade matplotlib.

How do I fix No module named 'matplotlib' in VS Code?

Press Ctrl+Shift+P (Cmd+Shift+P on macOS), choose Python: Select Interpreter, and select the virtual environment or Conda environment where Matplotlib is installed. Then open a new terminal in VS Code and re-run the file.

Do I need to restart the Jupyter kernel after installing Matplotlib?

Yes. After running %pip install matplotlib in a notebook cell, restart the kernel so Python reloads site-packages cleanly before importing matplotlib.pyplot.

Related Guides