DEV Community

Alin Climente
Alin Climente

Posted on

How to solve Pylance 'missing imports' in vscode

Here is how you can solve this issue:
pylance_import_issue

Make sure you selected the right python interpreter for your project (in case you are using virtualenv/pipenv/other):
pythonenv

When you run pipenv shell, you will see which python interpreter is used. A folder named .vscode will be created once you select a different interpreter than the default one. Inside .vscode you will have a settings.json file.

In settings.json file you have to add the paths from which you import what's needed in extraPaths:

{
    "python.pythonPath": "/home/youruser/.virtualenvs/app-FzQGSFjf/bin/python",
    "python.analysis.extraPaths": ["app", "another/path/etc"]
}
Enter fullscreen mode Exit fullscreen mode

In my case, app folder was not known by Pylance.

Top comments (14)

Collapse
 
miguelcb84 profile image
Miguel Coronado

In my case, the fastest solution when imports are not missing is to launch vscode from the virtual environment. Basically, activate the venv as always, and then code .

Collapse
 
karanparekh profile image
Karan Parekh

Thanks for the solution. Just to add, incase of virtual env created from WSL, you need to set the interpreter as shown below.
code.visualstudio.com/docs/python/....

Collapse
 
carloswm85 profile image
Carlos

Is there any way to include as a path ALL of the folders below a given path? I'm working inside a folder, with several subfolders, each of them with its own test. This solution solves the problem by adding each individual folder to the extraPaths, every time, but no for a group of folders.

Collapse
 
climentea profile image
Alin Climente

Please comment if you find the solution. I am interested in this too :)

Collapse
 
karanparekh profile image
Karan Parekh

Wild card paths (/) should work something like `folder1/folder2/`
But I am not sure..

Collapse
 
gregorywaynepower profile image
Gregory Power

I had this issue as well. I selected my Python Interpeter to my conda/mamba environment. But the only thing that worked was Ctrl + Shift + P , Python: Clearing Cache & Reload Workbook

Collapse
 
officialksolomon profile image
officialksolomon

What is the app folder

Collapse
 
phideasambrosianus profile image
PhideasAmbrosianus

Adding a note here. You may need to re-select the interpreter after you've run pip to install a new library. Just re-selecting the same thing (instead of changing versions) worked for me. Presumably restarting the app does same thing, but who wants to do that?

Collapse
 
jmahendr profile image
Joshua Francis

Thanks, this makes it easier.

Collapse
 
vinijill profile image
Vin

Thanks for the suggestion. Changing the interpreter solved my problem...

Collapse
 
xlibraboy profile image
Budi Kurniawan

wow, just change interpreter from 3.9.5 to 3.9.4, tadaaa... its working now, thank you so much

Collapse
 
happeecoder profile image
Anandh Happeecoder

Thank you very much

Collapse
 
siggib007 profile image
Siggi Bjarnason

In my case I'm getting this error just on requests , not on anything else. My interpreter is selected correctly from what I can tell and always has been. The code runs fine from within VSCode too.

Collapse
 
siggib007 profile image
Siggi Bjarnason

Looks like adding c:\program files\python310\lib\site-packages to analysis.extraPaths did the trick for me.