13

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?

Ali_Sh
  • 2,667
  • 3
  • 43
  • 66
Piyush Pandita
  • 181
  • 1
  • 1
  • 12
  • do you have multiple pythons or python environments? you can tell vscode which python environment you want to use. – jkr Dec 01 '21 at 15:13
  • At firsrt try `pip install numpy` . If this does not work try this ["Import could not be resolved" reported by Pyright](https://stackoverflow.com/questions/61052890/import-could-not-be-resolved-reported-by-pyright) – Vaggelis_Lazarakis Dec 01 '21 at 15:14
  • my problem is solved – Piyush Pandita Dec 01 '21 at 15:17
  • always put code, data and full error message as text (not screenshot, not link) in question (not in comment). It will be more readable and more people will see it - so more people can help you. – furas Dec 01 '21 at 16:29

8 Answers8

35

Try this in VSCode IDE:

(1) ctrl-shift-p

(2) In search box, type python and then select Python: Select Interpreter

(3) select Python 3.10.4 or any one matches your verion

the problem usually caused by IDE didn't pick up the right interpreter

Andy Z
  • 406
  • 3
  • 4
  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 03 '22 at 20:14
4

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

2

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.

Piyush Pandita
  • 181
  • 1
  • 1
  • 12
1

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!!

1

I had same problem with Import "numpy" could not be resolved Pylance with numpy, pandas and mlflow. Here is how I resolved it.

My environment is Windows 10 and I have install python3 on path c:\python\python.exe.

When I run: which python in my WSL2 it show c:\python\python.exe

So I studied mlflow then I have install Anaconda from here and installed pyenv then pip install numpy.

Everything work well now and when I run which python in my WSL2 again it shows /home/sutee/.local/bin/anaconda3/bin/python.

I now have python on anaconda3 and python on my Windows 10.

The solution for clear warning:

  1. I try to change "Python Select Interpreter" but can have only see the one from C:\python\python.exe and can't enter path from WSL.
  2. I install numpy, pandas and mlflow from Windows environments by using Windows PowerShell in admin and running pip3.exe.

Example:

C:\python\Scripts\pip3.exe install numpy
C:\python\Scripts\pip3.exe install pandas
C:\python\Scripts\pip3.exe install mlflow

After installing each package, the warning is gone.

James Risner
  • 5,451
  • 11
  • 25
  • 47
Doun Pct
  • 31
  • 1
0

I had this issue and it was simply solved by making the environment a trusted workspace. VS Code had disabled various extensions (including numpy), and had a bar above the workspace but below the search pane.

  1. click Manage
  2. trust workspace

hope no one else spins their wheels like I did on this one!

jaggs
  • 1
0

Only adding this to the list of possible solutions because it worked for me when nothing else here did. I did not have pylint, numpy, or the matplotlib extensions installed globally, instead only installing them within my .venv environment. Linting kept reporting both numpy and matplotlib libraries as unresolved until I disabled VS Code - Global Linting via Ctrl + Shift + P => Python: Enable/Disable Linting => Disable

VS Code Linting

After that, linting behavior began acting as expected.

John Eisbrener
  • 642
  • 8
  • 17
0

This solved the problem for me:

conda upgrade numpy
Diego Borba
  • 1,282
  • 8
  • 22