ImportError No module named matplotlib.pyplot
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
ImportError: No module named matplotlib.pyplot usually means one of three things: Matplotlib is not installed in the interpreter you are using, the wrong environment is active, or something in your project is shadowing the real package. Solving it starts with identifying which Python executable is actually running your code.
Verify the Interpreter First
The most reliable installation command is to invoke pip through the same Python interpreter that will run the script.
If your machine has multiple Python versions, be explicit:
After installation, verify both the interpreter and the package:
If this command succeeds but your editor still reports the import error, the editor is probably using a different interpreter.
Confirm pyplot Imports Cleanly
Matplotlib can be installed while pyplot still fails due to a broken environment or shadowed module name. Test the exact import:
If that works in the terminal, the problem is not Matplotlib itself. It is the environment or tool that launches your script.
Watch for Local Name Collisions
One very common mistake is naming your own file matplotlib.py or creating a folder named matplotlib in the project. Python then imports your local file instead of the real package.
Bad example:
In that layout, this import can fail in surprising ways:
Rename the local file, delete any generated __pycache__ directories, and try again.
Virtual Environments and Notebooks
The error often appears after installing Matplotlib "successfully" because the installation happened in one environment while the script or notebook runs in another.
For example:
- a shell installs into the global interpreter
- VS Code runs a virtual environment
- Jupyter uses a different kernel entirely
A quick notebook check is:
Compare that path with the interpreter used for installation. If they differ, install Matplotlib into the notebook kernel's environment instead of the one from your normal shell.
Create a Clean Environment When in Doubt
If the environment is messy, it is often faster to create a clean virtual environment than to repair a broken one in place.
On Windows, activation uses a different command, but the principle is the same: create an isolated environment and install the package there.
A Minimal Working Script
Once the import works, confirm plotting with a tiny script:
If import succeeds but the window does not appear, that is now a backend or display issue, not the original import error.
Common Pitfalls
- Running
pip install matplotlibwith a different Python interpreter than the one running the script. - Naming a local file or folder
matplotlib, which shadows the real package. - Assuming a Jupyter notebook uses the same environment as the terminal.
- Fixing a plotting backend problem as if it were still an import problem.
Summary
- Install Matplotlib with
python -m pip install matplotlibsopipmatches the active interpreter. - Verify the exact interpreter path with
sys.executable. - Test
import matplotlib.pyplot as pltdirectly from the command line. - Check for local files that shadow the package name.
- If the environment is unclear, create a fresh virtual environment and test there.

