r/learnpython • • 3h ago

Can't run matplotlib

I'm trying to make a graph using data points using matplotlib but for some reason, it's not working. I installed it using python -m pip install matplotlib and got a whole block of text with all the lines starting with, Requirement already satisfied. But, when I try to run, import matplotlib.pyplot as plt in my code, I get an error saying ModuleNotFoundError: No module named 'matplotlib'

Anyone know what the issue is and how I can get it resolved?

0 Upvotes

11 comments sorted by

2

u/Ender_Locke 3h ago

are you installing globally but using a venv to run your python(or vice versa)

0

u/Striking-Village3154 3h ago

I'm pretty sure I'm doing both globally

1

u/ResidentPomegranate6 3h ago

compare python -m pip -V against python -c "import sys;print(sys.executable)". the pip on your path is usually a different interpreter than the one running your file, which is how satisfied-yet-modulenotfound happens.

1

u/Striking-Village3154 2h ago

How would I change the path?

1

u/ee_control_z 3h ago

Do you have multiple versions of Python installed on your computer? It could be that you downloaded the module to another version of Python and the interpreter is linked to that version. To verify which modules you have installed you can type in the Python prompt:

help('modules')

To verify which Python versions you have installed on your computer, type the following in the command (CMD) prompt:

py -0

1

u/Striking-Village3154 2h ago

It's the same version

1

u/ee_control_z 2h ago

You only have one version installed?

4

u/Vorarbeiter 3h ago

You probably didn't install it in the same place you're trying to run it from

1

u/bra1n_gym 3h ago

run pip list in your terminal to verify you installed it. stay in the terminal start python then import matplotlib.pyplot as plt of there's no error then exit() to get out of python. 

1

u/AlexMTBDude 2h ago

This is a very common problem as all the commenters note: Different environments between where you run your code compared to where you pip installed matplotlib.

Simply: If you're running your Python code from an IDE like Visual Code or Pycharm then make sure that that's also where you did your installation of matplotlib.

On the other hand if you did "pip install matplotlib" from the terminal then also execute your Python code from the same terminal "python myprogram.py", i.e NOT from Pycharm/Visual code.

1

u/Necessary-Flounder52 3h ago

Are you running the code in the same environment in which you installed it, ie. the command line?