Import "numpy" could not be resolved Pylance
Asked Answered
F

14

16

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?

Famished answered 1/12, 2021 at 12:51 Comment(4)
do you have multiple pythons or python environments? you can tell vscode which python environment you want to use.Forsook
At firsrt try pip install numpy . If this does not work try this "Import could not be resolved" reported by PyrightBounden
my problem is solvedFamished
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.Manger
I
44

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

Incipit answered 3/6, 2022 at 20:10 Comment(2)
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.Rodarte
In case anaconda is used, the base environment might be selected by default instead of your custom one.Illyria
C
6

In case you are using VSCode:

  • press F1,
  • then select or write Developer: Reload Window
Cray answered 17/3 at 19:58 Comment(1)
Can't believe this answer was ranked as the first one for me. It actually works : ) thanksProtist
K
5

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

Keratoid answered 31/5, 2022 at 11:56 Comment(0)
F
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.

Famished answered 17/12, 2021 at 10:15 Comment(0)
A
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!!

Application answered 1/6, 2022 at 12:39 Comment(0)
A
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.

Arela answered 11/3, 2023 at 17:55 Comment(0)
J
1

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!

Jung answered 8/5, 2023 at 16:18 Comment(0)
T
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.

Treharne answered 20/6, 2023 at 12:16 Comment(0)
S
0

This solved the problem for me:

conda upgrade numpy
Sapless answered 28/8, 2023 at 1:52 Comment(0)
D
0

I had the same error message.

My problem was that, I selected the virtual environment after installing the numpy library.

The issue was resolved when I:

  1. Closed the terminal
  2. Reinstalled the numpy library
Deuterium answered 14/1 at 19:1 Comment(0)
S
0

This is only because of Interpreter. Python 3.9.0 is best for all lib. 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.9.0
Squamous answered 9/5 at 11:2 Comment(0)
E
0

I found a situation and a solution.
My which python3 is not the python3 in the conda environment, and pyright --verbose . shows that it is looking for python3. So I set the PATH path and then it can be executed successful.

> which python3
/opt/homebrew/bin/python3
> export PATH=/opt/homebrew/Caskroom/miniforge/base/envs/pyright/bin:$PATH
> pyright --verbose .

In VSCode, you also need to change the PATH env in .vscode/launch.json

Embezzle answered 5/8 at 2:23 Comment(0)
S
0

I have similar problem. Because I use the homebrew for installing many version python on my mac.

You can use the cmd to showing all version brew list | grep python

I resolve by removing old python versions except the last one and pip python -m pip uninstall pip --break-system-packages

And reinstall pip

NOTE: There will happen a problem that break-system-packages = true

Stoichiometry answered 15/8 at 10:51 Comment(0)
V
0

put pip install numpy into your terminal. Then Ctrl-shift-P, Developer: Reload Window

Vendue answered 17/8 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.