'Import "numpy" could not be resolved Pylance

I have wrote the following code:

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 5, 100)
y = x**2
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("graph of $x^2$")
plt.show()

When I run the code, it shows the following error:

enter image description here

I have installed NumPy, successfully, but still getting this warning. How can I get rid of this?



Solution 1:[1]

I was using two versions of python. Then I uninstalled both versions of it and installed the latest version of python 3.10.1 from https://www.python.org/. After this, installed pip Matplotlib and Numpy. Everything is fine now.

Solution 2:[2]

For people who use Visual Studio Code: sometimes happens that import could not be resolve because the Python interpreter is not selected. In this case ctrl + shift + p ---> Select python interpreter --> Python version installed on the computer

Solution 3:[3]

Maybe you have installed it in another env

  1. Try to change version from python interpreter
  2. Try to install python IDE
  3. Try to uninstall it and install numpy again

Hope this will fix your issue!!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Gianmarco Polizia
Solution 3 Nancy Thakur