How to Fix ‘ModuleNotFoundError: No Module Named ‘matplotlib’’ in Python
Have you ever been happily coding away, tried to import matplotlib.pyplot as plt and then boom:
‘ModuleNotFoundError: No Module Named ‘matplotlib’
It’s one of those frustrating bugs that seem simple, but can stump you for a few minutes. I’ll walk you through exactly how to fix ‘ModuleNotFoundError: No module named ‘matplotlib’’ in Python, step by step. I’ll also compare what other blog posts talk about, show you where they fall short, and add some bonus tips you rarely see.
Understanding the Error
Let’s break it down. When you see the message:
‘ModuleNotFoundError: No Module Named ‘matplotlib’
it simply means: your Python interpreter tried to find the module named matplotlib, and it couldn’t find it in any of the places it looked. In other words, Python is saying “I looked, I searched, no such module here.” (Also applies to sub-modules like matplotlib.pyplot.)
There are several reasons this happens some obvious, some subtle. Let’s journey through them.
Matplotlib Not Installed
The most common cause: you simply haven’t installed the library in that Python environment.
How to fix:
- Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux)
- Run:
python -m pip install matplotlibor on systems wherepython3is required:python3 -m pip install matplotlib - Wait for the install to finish. Then run:
import matplotlib print(matplotlib.__version__)If you see a version number and no error good, you installed it.
Why using python -m pip matters:
When you run plain pip install matplotlib, it might install for a different Python interpreter than the one you use to run your code. Using python -m pip ties the pip to the exact Python executable you’ll use later.
OS specific notes:
- On Windows, you might need
py -m pip install matplotlib. - On Linux/macOS, you might need
sudo python3 -m pip install matplotlibif installing globally. - If you use Anaconda or Miniconda:
conda install matplotlib
If after doing this the error persists, then we are into a different cause.
Wrong Python Interpreter or Virtual Environment
You might have installed matplotlib, but for a different Python interpreter than the one you’re using to run your code.
How this happens:
- You have multiple Python versions (e.g., Python 3.8 and Python 3.11).
- You’re using a virtual environment (venv/conda) but forgot to activate it.
- Your IDE or script is pointing to a different interpreter than you think.
How to check:
In your code (or terminal) run:
import sys
print(sys.executable)
print(sys.version)
print(sys.path)
This tells you which Python executable is actually running. Compare that path with where you installed matplotlib.
If they differ it means “I installed here, but running there”.
How to align them
- Activate your virtual environment (if using):
- Windows:
.\venv\Scripts\activate.bator.\venv\Scripts\Activate.ps1 - macOS/Linux:
source venv/bin/activate
- Windows:
- Install
matplotlibinside that environment. - Ensure your IDE is configured to use that interpreter: in Visual Studio Code click interpreter selector, in PyCharm set Project Interpreter.
- If you’re just running from terminal, check you’re calling the correct
python.
New tip hardly discussed
If you use a shebang (#!) at top of your Python script, ensure it points to the correct interpreter. E.g. #!/usr/bin/env python3 ensures you pick up the right version. If the script uses #!/usr/bin/python but python is v2.x without matplotlib, you’ll get the error.
Jupyter Notebook / Kernel Mismatch
Jupyter adds a layer of complexity: your notebook might use a different Python kernel than your terminal.
How this shows up:
You install matplotlib with pip install matplotlib in your terminal. In the notebook you do import matplotlib and still get the error.
How to check:
In a notebook cell run:
import sys
print(sys.executable)
print(sys.path)
Look at the interpreter path and compare with where you installed the library.
How to fix:
- Install inside the notebook environment: In a cell run:
%pip install matplotlibThis ensures installation in the notebook kernel’s environment. - Or register the correct kernel:
python -m ipykernel install --user --name=myenv --display-name="Python (myenv)"Then in Jupyter choose the “Python (myenv)” kernel. - Ensure when you start Jupyter you activate the virtual environment first (so Jupyter uses that env by default).
Naming Conflicts and Shadowing
Even when matplotlib is installed and you’re using the correct interpreter, you might still get the error because you’ve accidentally overshadowed the module.
Example pitfalls:
- You have a file named
matplotlib.pyin your working directory. - You have a folder named
matplotlib(without the library contents) in yoursys.path. - You declared a variable named
matplotlibearlier in your code.
Why it matters:
When Python does import matplotlib, it searches current directory first. If it finds your file/folder named matplotlib, it uses that instead of the real library. Then when code tries matplotlib.pyplot, it fails because your file isn’t a real library.
How to fix:
- Rename your file/folder (for example to
my_plot_code.pyinstead ofmatplotlib.py). - Delete any
matplotlib.pycor__pycache__related to the wrong file. - Avoid naming variables the same as modules. Don’t do:
matplotlib = "something" import matplotlib.pyplot as plt # Boom, shadowing!
Corrupted or Partial Installation
Sometimes you did install matplotlib, you are using the right environment, but it still fails. That suggests the installation is broken or dependencies are missing.
What can go wrong:
- Interrupted install or cancelled process, leaving partial files.
- Version of
matplotlibnot compatible with your Python version. matplotlibdependencies (likenumpy) are missing or incompatible.- Permission issues (install tried, but failed silently).
How to fix:
- Uninstall and reinstall:
python -m pip uninstall matplotlib python -m pip install matplotlibor for conda:conda remove matplotlib conda install matplotlib - Check dependencies: run:
pip checkThis lists packages with broken dependencies. - Upgrade pip:
python -m pip install --upgrade pipThen reinstall. - If you have older Python (e.g., 3.5 or earlier), check that the
matplotlibversion you install still supports it—although nowadays many versions require Python 3.6+. - On Linux, ensure you don’t have permission issues (install as
--useror withsudoas needed).
Miscellaneous / Less Common Causes
Here are some extra scenarios you might hit (not always covered by others):
Path / PYTHONPATH Problems:
If you manually set PYTHONPATH environment variable to some weird directory, Python might ignore the site-packages where matplotlib lives. Check echo $PYTHONPATH (or on Windows echo %PYTHONPATH%) and ensure it doesn’t force Python to ignore standard install paths.
Running code as root/sudo:
If you install matplotlib as root but run the code as non-root (or vice-versa), or if you use a system environment vs user environment, mismatches can happen.
Docker/Container or CI environment:
If you build a container (a Docker image), you might install libraries in one layer but run your code in another—or forget to install at all. One blog touches Docker but not deeply.
Inside Docker, ensure your Dockerfile has something like:
RUN pip install matplotlib
And that the python path inside container is the same one you run.
A Full Checklist Follow This Order
Here’s a practical checklist to diagnose and fix the “No module named ‘matplotlib’” error:
- Install the library:
python -m pip install matplotlibIf using conda:conda install matplotlib - Check interpreter path in your code:
import sys print(sys.executable) print(sys.version) - Ensure you’re in the correct environment (venv/conda) and it’s activated before installing and running.
- In IDE/Jupyter, verify the correct interpreter or kernel is used:
- In VS Code: Select interpreter
- In PyCharm: Project interpreter settings
- In Jupyter: Check kernel name; run
sys.executablefrom a cell
- Check for naming conflicts:
- File named
matplotlib.py? Rename it. - Folder named
matplotlibin project? Rename it.
- File named
- If still failing, reinstall:
python -m pip uninstall matplotlib python -m pip install matplotlib - Check dependencies and versions:
pip check python -c "import numpy; print(numpy.__version__)" - If using Docker/CI: confirm install is in correct stage and
pythonversion matches. - Restart your IDE/kernel after installation. Sometimes the environment changes aren’t picked up until restart.
Why This Fix Works
Now you might ask: why are all these steps necessary? Because Python’s module system works like this:
- When you run
python, it has a specific interpreter binary (sys.executable). - That interpreter has its own “site-packages” folder.
- When you
import matplotlib, Python searches throughsys.pathin order and picks the first matching folder/module namedmatplotlib. - If nothing matches (module not installed) or if something wrong shadows the real module (e.g., wrong file name), you get the error.
- Virtual environments, multiple interpreters, IDE misconfigurations all break this chain by making you think you’re using one Python when you’re using another.
By aligning those pieces installing in the same interpreter, using the correct one, avoiding conflicts you’re fixing the root cause, not just the symptom.
Conclusion
So there you have it your complete guide on how to fix ‘ModuleNotFoundError: No module named ‘matplotlib’’ in Python. We covered installation, interpreter mismatches, naming issues, environment pitfalls, Jupyter quirks, corrupted installs and more.