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 matplotlibIf you are inside a Jupyter Notebook or JupyterLab cell, install directly into the active kernel and restart it:
%pip install matplotlibTo confirm which interpreter is actually running your code, print sys.executable:
import sys
print(sys.executable)- ty 1.0 Migration Guide: Move from mypy to ty and Fix the type: ignore Comments That Stop Working
- Best Agent Skills for Claude Code and Codex: What to Install and How to Audit a Repo First
- Claude Code Permissions Hardening: Block Writes Outside Your Repo, Deny Instead of Prompt, and Audit settings.local.json
- How to Convert Python to EXE: Runcell, PyInstaller, Nuitka, and cx_Freeze
- pandas 3.1 Upgrade Guide: Fix the inplace Pandas4Warning, Use df.select() and pd.col(), and Read CSVs Faster
- How to Upgrade to OpenCode 2.0: Migrate Your v1 Config, Plugins, and Sessions
- Qwen3.8 27B: Specs, Memory Requirements, and How to Run It
- Haiku pricing: Haiku 5.5 vs 4.5 vs GPT-6 Luna
- Python 3.15 Upgrade Guide: Install with uv, Use Lazy Imports, and Check Your Data Stack
- Claude Code Mods Tutorial: Build and Test Your First Mod, Plus 15 Community Examples
- How to Use Pi Coding Agent 1.0: Install, Connect a Model, and Run Your First Task
- How to Use Laya: Classify Text in a pandas DataFrame with the Open-Source Jev Alternative
- Claude Code Reads AGENTS.md Now: Setup, Loading Modes, and Sharing One File with Codex, OpenCode, and Cursor
- How to View Deleted Reddit Posts (2026 Guide): 5 Ways That Still Work
- GPT Image 2.5: How to Use It, Flare vs Sunburst, and API Pricing
- How to Use DeepSeek Harness: Install, Set Up, and Run Your First Agent
- Runcell Science: An Open Source Alternative to Claude Science for Research Workflows
- How to Make Mac Not Sleep: Keep Codex, Claude Code, and AI Agents Running
- OpenClaw vs ZeroClaw vs Pi Agent vs Nanobot: Which AI Agent Stack Should You Choose in 2026?
- Can Claude Code Analyze Jupyter Notebooks for Data Science? What It Actually Does
- Claude Code Routines: Why AI Agent Cron Jobs Matter
- Claude Code Desktop Bypass Permissions: How to Enable It
- How to Build Two Python Agents with Google’s A2A Protocol - Step by Step Tutorial
- Top 10 growing data visualization libraries in Python in 2025
Quick Diagnostic Table: Match Your Error to the Fix
Use this matrix to jump straight to the cause affecting your setup:
| Symptom or Environment | Most Likely Cause | Immediate Fix |
|---|---|---|
| Fresh Python install or new virtualenv | Matplotlib is not installed yet | python -m pip install matplotlib |
| Works in terminal, fails in Jupyter Notebook | Jupyter kernel uses a different Python environment | Run %pip install matplotlib in a cell, then restart kernel |
| Works in terminal, fails in VS Code / PyCharm | IDE selected a different interpreter | Switch interpreter to match which python / where python |
No module named 'matplotlib.backends.registry' | Partial/corrupted upgrade or mixed Matplotlib versions | Clean uninstall and reinstall with --no-cache-dir |
| Fails right after creating a test script | Local file named matplotlib.py shadows the package | Rename your file and delete __pycache__/ |
Runs via ./script.py on macOS/Linux | Hardcoded #!/usr/bin/python shebang | Change 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 --versionInstall Matplotlib into that exact interpreter:
python -m pip install --upgrade matplotlibOn Windows with the Python Launcher (py), target a specific version explicitly:
py -3.12 -m pip install matplotlibIf 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 matplotlibVerify 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 matplotlibRestart 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:
- Open the Command Palette (
Ctrl+Shift+Pon Windows/Linux,Cmd+Shift+Pon macOS). - Run Python: Select Interpreter.
- Pick the interpreter path that matches the virtual environment or Conda environment where you installed Matplotlib (for example
./.venv/bin/pythonor.\.venv\Scripts\python.exe). - Open a fresh integrated terminal so VS Code activates the selected environment, then re-run your script.
In PyCharm:
- Open Settings / Preferences → Project → Python Interpreter.
- Confirm
matplotlibappears in the installed packages list. - If not, select the correct existing virtualenv/Conda interpreter or click + to install
matplotlibinto 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.batConfirm your prompt shows the environment name and verify sys.executable points inside .venv:
python -c "import sys; print(sys.executable)"
python -m pip install matplotlibFix 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 matplotlibwas interrupted, leaving mixed files from two versions insidesite-packages. - Both
condaandpipinstalled 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 pltFix 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 pltIf 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
- Matplotlib Currently Using Agg Backend: How to Fix
- Fix Matplotlib pyplot Not Resolved in VS Code and Python
- How to Use Matplotlib Inline in Jupyter Notebook
- Matplotlib figsize: Set Figure Size, DPI, Subplots & Pixel Conversion
- Matplotlib Subplots: Create Multi-Panel Figures with plt.subplots()