Python
ImportError
matplotlib
error-solving
programming

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.

bash
python -m pip install matplotlib

If your machine has multiple Python versions, be explicit:

bash
python3 -m pip install matplotlib

After installation, verify both the interpreter and the package:

bash
python -c "import sys, matplotlib; print(sys.executable); print(matplotlib.__version__)"

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:

bash
python -c "import matplotlib.pyplot as plt; print('ok')"

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:

text
my_project/
  matplotlib.py
  test_plot.py

In that layout, this import can fail in surprising ways:

python
import matplotlib.pyplot as plt

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:

python
import sys
print(sys.executable)

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.

bash
1python -m venv .venv
2source .venv/bin/activate
3python -m pip install --upgrade pip
4python -m pip install matplotlib
5python -c "import matplotlib.pyplot as plt; print('ready')"

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:

python
1import matplotlib.pyplot as plt
2
3plt.plot([1, 2, 3], [1, 4, 9])
4plt.title("Test Plot")
5plt.show()

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 matplotlib with 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 matplotlib so pip matches the active interpreter.
  • Verify the exact interpreter path with sys.executable.
  • Test import matplotlib.pyplot as plt directly 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.

Course illustration
Course illustration

All Rights Reserved.