"Import could not be resolved" reported by Pyright
Question:
I’ve just started using Pyright. Running it on files that run perfectly well I get plenty of errors.
This question is similar, but refers to one’s own modules.
For example Import "numpy" could not be resolved
. What does it mean, and how do I resolve it?
Answers:
On my computer I have 3 Pythons, a 3.6 from Anaconda, and a 2.7 & 3.7 that are regular python. Prompted by a nudge from this GH issue, I switched from the Anaconda 3.6 to the 3.7, and back again, and the problem went away.
I think that this is the case because your .vscode/settings.json
(the following is mine), doesn’t have that last line until you change your python, at which point, that last line is put in and Pyright has something to look at.
{
"python.linting.enabled": true,
"python.formatting.provider": "black",
"python.pythonPath": "C:\Users\ben\Anaconda3\python.exe"
}
I’m not sure if you have already fixed this, but I was having a lot of troubles with stuff I got with pip install and they were installed successfully, but when I tried to import them, they didn’t import
I found out that for me, some of the installs get installed to a separate folder, and not the correct folder that they need to be in to import.
If you try installing it again with pip install, then it will say you already have it installed and it will give you the location where the file is, and I copied the files from there and pasted them into the correct location which is (on windows)
C:Users{Username}AppDataLocalProgramsPython{Python Version}Libsite-packages
I was trying to find a place to share this and this was the closest to the issue, so I hope it helps!
edit: the above answer is definitely a short-term solution, but I was able to fix this completely by reinstalling pip. I forgot how I reinstalled it, but I thought I’d add this to point you in the right direction if you need a long-term solution
python -m pip install -U pylint
python -m pip install --upgrade pip
Open VS Code -> CTRL + SHIFT P
-> Search ‘select linter
‘ [Python: Select Linter
] -> Hit Enter and Select Pylint
If not solved.
Try deactivate
your environment pip install numpy
in your global environment.
And if you are using Local Environment getting unresolved imports error then add In .vscode/settings.json "python.analysis.extraPaths": ["./path-to-your-code"],
for example "python.analysis.extraPaths": [ "*.lib" ]
I work in Local Environment and I can solve this problem by going to Setting.json and search python analysis but I just addressed this path C:Users{Username}AppDataLocalProgramsPython{Python Version}Libsite-packages
Setting the interpreter as Python with current version worked just fine for me. You can do it by clicking Python in lower left corner and selecting your project.
I solved mine by just adding the conda executable path.
Here is how I solved it on my Mac.
In a terminal window (system’s terminal not the vscode one) type:
which python
On my setup I got: "/Volumes/Attic/miniforge3/bin/python"
Then in VsCode copy that whole path to Setting > Extensions > Default Interpreter Path
That’s it. That instantly resolved the issue.
I also did the same for Setting > Extensions > Conda Path populating that field with the path returned by
which conda
But that’s a separate issue.
I’ve just started using Pyright. Running it on files that run perfectly well I get plenty of errors.
This question is similar, but refers to one’s own modules.
For example Import "numpy" could not be resolved
. What does it mean, and how do I resolve it?
On my computer I have 3 Pythons, a 3.6 from Anaconda, and a 2.7 & 3.7 that are regular python. Prompted by a nudge from this GH issue, I switched from the Anaconda 3.6 to the 3.7, and back again, and the problem went away.
I think that this is the case because your .vscode/settings.json
(the following is mine), doesn’t have that last line until you change your python, at which point, that last line is put in and Pyright has something to look at.
{
"python.linting.enabled": true,
"python.formatting.provider": "black",
"python.pythonPath": "C:\Users\ben\Anaconda3\python.exe"
}
I’m not sure if you have already fixed this, but I was having a lot of troubles with stuff I got with pip install and they were installed successfully, but when I tried to import them, they didn’t import
I found out that for me, some of the installs get installed to a separate folder, and not the correct folder that they need to be in to import.
If you try installing it again with pip install, then it will say you already have it installed and it will give you the location where the file is, and I copied the files from there and pasted them into the correct location which is (on windows)
C:Users{Username}AppDataLocalProgramsPython{Python Version}Libsite-packages
I was trying to find a place to share this and this was the closest to the issue, so I hope it helps!
edit: the above answer is definitely a short-term solution, but I was able to fix this completely by reinstalling pip. I forgot how I reinstalled it, but I thought I’d add this to point you in the right direction if you need a long-term solution
python -m pip install -U pylint
python -m pip install --upgrade pip
Open VS Code -> CTRL + SHIFT P
-> Search ‘select linter
‘ [Python: Select Linter
] -> Hit Enter and Select Pylint
If not solved.
Try deactivate
your environment pip install numpy
in your global environment.
And if you are using Local Environment getting unresolved imports error then add In .vscode/settings.json "python.analysis.extraPaths": ["./path-to-your-code"],
for example "python.analysis.extraPaths": [ "*.lib" ]
I work in Local Environment and I can solve this problem by going to Setting.json and search python analysis but I just addressed this path C:Users{Username}AppDataLocalProgramsPython{Python Version}Libsite-packages
Setting the interpreter as Python with current version worked just fine for me. You can do it by clicking Python in lower left corner and selecting your project.
I solved mine by just adding the conda executable path.
Here is how I solved it on my Mac.
In a terminal window (system’s terminal not the vscode one) type:
which python
On my setup I got: "/Volumes/Attic/miniforge3/bin/python"
Then in VsCode copy that whole path to Setting > Extensions > Default Interpreter Path
That’s it. That instantly resolved the issue.
I also did the same for Setting > Extensions > Conda Path populating that field with the path returned by
which conda
But that’s a separate issue.