You ran your script, and Python hit you with this:
ModuleNotFoundError: No module named 'matplotlib'
Don’t worry, this is one of the most common Python errors, and it’s almost always fixable in under five minutes. The tricky part is that this error can happen for several different reasons, and the fix depends on which one applies to you.
In this guide, I’ll walk through every cause of this error and show you exactly how to fix it, whether you’re working in the terminal, VS Code, PyCharm, Jupyter Notebook, or Anaconda.
Why This Error Happens
Before jumping to fixes, it helps to understand what the error actually means. Python is telling you: “I looked through all the places I know to find a package called matplotlib, and I couldn’t find it.”
That can happen because of one of these reasons:
- Matplotlib was never installed in the first place
- Matplotlib was installed, but into a different Python than the one you’re running
- You’re using a virtual environment that doesn’t have Matplotlib installed
- Your IDE is pointing to the wrong Python interpreter
- You have a file in your project named matplotlib.py that’s shadowing the real library
- The installation got corrupted or is incomplete
Let’s go through each one.
Fix 1 — Install It (The Most Common Fix)
If you’ve never installed Matplotlib before, this is your fix:
python -m pip install matplotlib
I always use python -m pip instead of just pip. Here’s why: if you have multiple Python versions on your machine, plain pip might install Matplotlib into a different Python than the one you’re actually using. python -m pip guarantees it installs into the exact Python you’re running.
After installation, verify it worked:
python -c "import matplotlib; print(matplotlib.__version__)"
Refer to the screenshot below for the output.

If you see a version number like 3.10.1, you’re done.
Fix 2 — You Have Multiple Python Versions Installed
This is the #1 silent cause of this error. You install Matplotlib with pip, it says “Successfully installed”, you run your script, and still get ModuleNotFoundError.
What happened? You installed Matplotlib into Python 3.10, but your script is running on Python 3.12 (or vice versa).
Check which Python your terminal is actually using:
python --version
Check which Python pip is connected to:
pip --version
The output of pip –version will show something like:
pip 24.0 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)
Refer to the screenshot below for the output.

If the Python version in that path doesn’t match your python –version output, they’re mismatched.
Fix it by being explicit:
python3.12 -m pip install matplotlib
Replace 3.12 with whatever version you’re actually using. This way you’re 100% sure pip installs into the right Python.
Check out: Fix Matplotlib Stacked Bar Chart Error in Python
Fix 3 — Your Virtual Environment Isn’t Activated
If you’re working in a virtual environment, Matplotlib must be installed inside that environment — not globally.
Check if your virtual environment is active: Look at your terminal prompt. If you see (myenv) or the name of your environment at the start, it’s active. If not, activate it first:
On macOS/Linux:
source myenv/bin/activate
On Windows:
myenv\Scripts\activate
Then install Matplotlib inside the activated environment:
pip install matplotlib
Refer to the screenshot below for the output.

Then run your script while the environment is still active.
A common mistake I see is: someone activates the environment, installs Matplotlib, then opens a new terminal window to run their script — and that new terminal doesn’t have the environment activated. Always check that (myenv) prompt before running your code.
Fix 4 — VS Code Is Using the Wrong Python Interpreter
This trips up a lot of people. VS Code has its own Python interpreter setting that may be pointing to a different Python than the one where you installed Matplotlib.
To fix this in VS Code:
- Press Ctrl+Shift+P (or Cmd+Shift+P on Mac) to open the Command Palette
- Type Python: Select Interpreter
- Choose the Python interpreter that matches your environment — you’ll see the path next to each option
- If you installed Matplotlib in a virtual environment, select that environment’s interpreter (it’ll show the venv path)
After switching interpreters, open a new terminal inside VS Code (Terminal > New Terminal) and verify:
python -c "import matplotlib; print(matplotlib.__version__)"
If it prints the version, VS Code is now using the right Python.
Fix 5 — PyCharm Is Using the Wrong Interpreter
Same concept as VS Code — PyCharm manages its own Python interpreter per project.
To fix this in PyCharm:
- Go to File > Settings (Windows/Linux) or PyCharm > Preferences (Mac)
- Navigate to Project: YourProjectName > Python Interpreter
- Click the gear icon and select Add Interpreter or Show All
- Point it to the Python environment where Matplotlib is installed
Alternatively, install Matplotlib directly from within PyCharm:
- Go to the interpreter settings as above
- Click the
+button to add a package - Search for matplotlib and click Install Package
Read: Make Y-Axis Tick Labels Invisible in Matplotlib
Fix 6 — Jupyter Notebook or JupyterLab
Jupyter is the most common source of this confusion. Jupyter can have its own Python kernel that’s completely separate from the Python you use in the terminal.
Running pip install matplotlib in the terminal does not guarantee Matplotlib is available inside Jupyter.
The reliable fix — install directly from a notebook cell:
%pip install matplotlib
The %pip magic command installs into the same Python kernel that the notebook is running on. Don’t skip the % — without it, pip runs in a subshell and may install into a different environment.
After running %pip install matplotlib, restart the kernel (Kernel > Restart) and run your import again:
import matplotlib.pyplot as plt
print(plt.__version__)
If you’re using Anaconda with Jupyter:
conda install matplotlib
Or from inside the notebook:
%conda install matplotlib
Fix 7 — You’re on Anaconda / Conda Environment
If you’re using Anaconda or a conda environment, don’t mix conda and pip for the same environment when you can avoid it. Install Matplotlib through conda:
conda install matplotlib
Or from the community channel (usually more up to date):
conda install -c conda-forge matplotlib
If you tried pip install matplotlib inside a conda environment and it didn’t work, uninstall and reinstall through conda:
pip uninstall matplotlib
conda install -c conda-forge matplotlib
Fix 8 — The Installation Is Corrupted
Sometimes, a partial or broken installation can cause this error even when pip list shows Matplotlib as installed. The fix is to wipe it and reinstall clean:
pip uninstall matplotlib
pip cache purge
python -m pip install matplotlib
The pip cache purge step clears any cached files from a previous broken install, so pip downloads a fresh copy.
Fix 9 — You Have a File Named matplotlib.py in Your Project
This is an easy one to miss. If you have a file named matplotlib.py anywhere in your project folder, Python will try to import that file instead of the actual Matplotlib library, and fail.
Check your project directory:
ls *.py
If you see a matplotlib.py file, rename it to something else. The same goes for any folder named matplotlib in your working directory.
Fix 10 — You’re Running a Script With #!/usr/bin/python (Linux/Mac)
On Linux and macOS, Python scripts sometimes have a “shebang” line at the top:
#!/usr/bin/python
This tells the OS which Python interpreter to use when you run the script directly. The problem is /usr/bin/python might be Python 2 or a bare system Python with no packages installed.
Change it to:
#!/usr/bin/env python3
This uses whichever python3 is active in your current environment, much more reliable.
Quick Diagnostic: Figure Out What’s Going Wrong
If you’re not sure which fix applies to you, run these diagnostic commands and compare the output:
Which Python is running your script?
python -c "import sys; print(sys.executable)"
Where is Python looking for packages?
python -c "import sys; print('\n'.join(sys.path))"What packages does this Python have installed?
python -m pip list | grep matplotlib
If matplotlib appears in the pip list but the script still throws the error, the script is running on a different Python than the one pip installed into. That points to Fix 2, Fix 4, or Fix 5 above.
If matplotlib doesn’t appear in the pip list at all, run Fix 1.
All Fixes at a Glance
| Situation | Fix |
|---|---|
| Matplotlib was never installed | python -m pip install matplotlib |
| Multiple Python versions on your machine | python3.x -m pip install matplotlib (match your version) |
| Virtual environment not activated | Activate venv, then pip install matplotlib |
| VS Code wrong interpreter | Python: Select Interpreter from Command Palette |
| PyCharm wrong interpreter | File > Settings > Python Interpreter |
| Jupyter Notebook kernel mismatch | Run %pip install matplotlib inside the notebook |
| Anaconda environment | conda install -c conda-forge matplotlib |
| Corrupted installation | pip uninstall matplotlib, pip cache purge, reinstall |
File named matplotlib.py in your project | Rename that file |
| Wrong shebang line (Linux/Mac) | Change to #!/usr/bin/env python3 |
After Fixing the Error — Verify With a Quick Plot
Once the import error is gone, confirm everything is truly working by running a simple plot:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 15, 30, 25]
plt.plot(x, y, marker='o', color='steelblue', linewidth=2)
plt.title("Test Plot — Matplotlib is Working!")
plt.xlabel("X")
plt.ylabel("Y")
plt.grid(True, linestyle='--', alpha=0.6)
plt.show()
If a chart window appears, you’re good. If the import still fails after trying all the fixes above, drop the exact error message and the output of python -c “import sys; print(sys.executable)” in the comments, and I’ll help you debug it.
Common Questions
Why does pip install matplotlib say “already satisfied” but the error still appears?
pip is telling you Matplotlib is installed in the Python pip is connected to, but your script might be running on a different Python. Run python -m pip install matplotlib to guarantee both are the same Python.
I’m on Windows and still get the error after installing. What do I try?
Open Command Prompt and run where python to see all Python installations. Then run python -m pip install matplotlib and check which path it’s installing into. Make sure your script runs using the same Python path.
How do I fix this inside a Docker container?
Add RUN pip install matplotlib to your Dockerfile, or run docker exec -it <container> pip install matplotlib inside a running container.
I installed it globally, but I’m using a venv — will it work?
No. A virtual environment is isolated by design; it can’t see globally installed packages by default. Install Matplotlib inside your activated venv.
Can I use conda and pip together?
You can, but it’s safer to stick to one package manager per environment. If you’re in a conda environment, use conda to install packages. Mixing can sometimes cause dependency conflicts.
You may also like to read:
- Fix: import matplotlib.pyplot could not be resolved from source
- How to Turn Off Axis Labels in Matplotlib
- Matplotlib Axis Label Font Size
- How to Change Matplotlib Tick Label Font Size

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.