已安装matplotlib仍触发ModuleNotFoundError的技术求助
Hey there, it’s super frustrating when you’ve clearly installed a package but Python can’t find it—this almost always boils down to Python environment mismatches. Let’s walk through the fixes step by step based on your setup (you mentioned matplotlib 1.3.1 is in your installed packages list, so we know it’s there somewhere):
1. Make sure you’re using the same Python environment for installation and execution
The most common issue is installing matplotlib with one version of pip/Python, but running your code with a different one. Here’s how to confirm and fix it:
- First, check which Python version your script is using:
python --version # or python3 --version if that's your default - Then verify which pip belongs to that exact Python version (this avoids confusion between global and virtual env pips):
python -m pip --version - If the pip’s Python version doesn’t match your runtime version, reinstall matplotlib using the matching pip to align them:
python -m pip install matplotlib --upgrade
2. Check if matplotlib is in Python’s accessible path
Sometimes packages get installed in a directory Python doesn’t check by default. Let’s verify:
- Run this to see all directories Python scans for modules:
python -c "import sys; print('\n'.join(sys.path))" - Then get matplotlib’s installation location:
python -m pip show matplotlib - Look for the
Locationline in the output—if this path isn’t in the list fromsys.path, you have two options:
a. Reinstall matplotlib using the matching pip (as step 1 suggests) to put it in the right directory, or
b. Temporarily add the path to your script (not ideal long-term, but great for testing):import sys sys.path.append("/path/to/matplotlib/location") # Replace with the Location from pip show import matplotlib
3. Rule out virtual environment mix-ups
If you use virtual environments (venv, conda, etc.), double-check:
- You activated the correct environment when you installed matplotlib, and
- You’re running your script in that same activated environment.
If you installed it globally but are using a virtual env, just activate the env and runpython -m pip install matplotlibagain.
4. Check for outdated version compatibility (less likely, but worth trying)
Your matplotlib version (1.3.1) is pretty old—released back in 2014. If you’re running a newer Python version (3.5+), this outdated version might not play nice. Upgrade to a supported release with:
python -m pip install matplotlib --upgrade
内容的提问来源于stack exchange,提问作者bhaskar

