Unable to use matplotlib after installation "no module named 'matplotlib'"

Question:

I am brand new to Python and am following the tutorial at this link:
https://code.visualstudio.com/docs/python/python-tutorial

I was able to execute

python -m pip install matplotlib

However, then when trying to run script:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show()

I get:

PS C:CodeAstrosPrism> & C:/Users/USER/AppData/Local/Programs/Python/Python38/python.exe c:/Code/Astros/Prism/Prism.py
Traceback (most recent call last):
  File "c:/Code/Astros/Prism/Prism.py", line 1, in <module>
    import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
PS C:CodeAstrosPrism> 

Any guidance would be much appreciated.

Asked By: user8788429

||

Answers:

open your terminal in same directory:
make venv by typing :
python -m venv .
then activate it
venv/scripts/activate
then install matplotlib by pip.
this should work

Can you check where the matplotlib package has been installed through this command?

pip show matplotlib

And you are using the global Python on your computer:

C:/Users/USER/AppData/Local/Programs/Python/Python38/python.exe

It should be different.

You can switch to the python interpreter which you have installed the packages you want to import. Or install the packages in the interpreter you are using.

Answered By: Steven-MSFT

you can try:

python3 -m pip install matplotlib
pip3 install matplotlib
Answered By: lam vu Nguyen