0

In Visual Studio Code, I can use numpy library but not the matplotlib.pyplot library even though I have installed both

The example code in visual studio code:

mport matplotlib.pyplot as plt

import numpy as np

xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
plt.plot(xpoints, ypoints)

plt.show()

I get in the terminal

ModuleNotFoundError: No module named 'matplotlib.pyplot'; 'matplotlib' is not a package

However, when I type pip3 show matplotlib, I get the message that it is alr installed:

Name: matplotlib
Version: 3.7.1
Summary: Python plotting package
Home-page: https://matplotlib.org
Author: John D. Hunter, Michael Droettboom
Author-email: [email protected]
License: PSF
Location: /home/huinker/.local/lib/python3.10/site-packages

However, VS code works with other library like numpy. Example code

import numpy
A, B=numpy.array(list(map(int, input().split()))), numpy.array(list(map(int, input().split())))
print (numpy.inner(A, B))
print (numpy.outer(A, B))

the code works with numpy library. when i try in the termial pip3 show numpy:

Name: numpy
Version: 1.24.3
Summary: Fundamental package for array computing in Python
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email:License: BSD-3-Clause
Location: /home/huinker/.local/lib/python3.10/site-packages
Requires:
Required-by: contourpy, matplotlib

What is my problem with the environment?

7
  • 3
    most likely Visual Studio Code is not using the interpreter you think it is using, check with import sys; print(sys.executable) Commented May 26, 2023 at 10:16
  • 1
    Quick guess: did you name your own file matplotlib.py or have such file in the directory? Current directory gets prepended to path, so current files get searched before the installed ones Commented May 26, 2023 at 10:17
  • @SembeiNorimaki wouldn't he get another error? Commented May 26, 2023 at 10:22
  • 1
    @stunlocked that advice is unrelated to the question asked and can cause even more interpreter confusion (in case if it's what dankel444 said and not what I said) Commented May 26, 2023 at 10:29
  • 2
    @h4z3 i changed the name and it works now. it is the file name problem Commented May 26, 2023 at 10:38

1 Answer 1

1

Have a look into the VS Code Docs regarding environments. I am pretty sure the guess of @dankal444 is correct. Type Ctrls+Shift+P to open the command palette and search for Python: Select Interpreter. Choose the one called /home/huinker/.local/lib/python3.10/.

how to select python interpreter GIF

GIF is from this video.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.