how do I solve the Pylance(reportMissingImports)?
Asked Answered
C

2

12

I used pip install fastapi to download it in my virtual environment, in terminal but when I try to import from it it shows up as a missing import.

Copalite answered 26/4, 2022 at 18:50 Comment(0)
D
31

If you want to install a package into your specific virtual environment, you need to first "activate" that environment. Likewise, if you want to run your script in that environment, you need to first "activate" it. You can do this manually or preferably let VSCode handle it for you.

In order to tell VSCode(especially the language server which is pylance) to use that environment:

  1. Open up your Command Palette(press ctrl+shift+P or f1) and type : "python: select interpreter".
  2. Browse/Select your newly created python interpreter's path inside your venv.
  3. Add "python.terminal.activateEnvironment": true to your setting.json file(It's also possible via GUI setting of course by searching for python: activate environment). This will automatically detect and activate your venv whenever you open your integrated terminal. Obviously If the path should points to a venv interpreter. (Note: you have to have a Python file opened in your editor).

You can also set your Python's interpreter path manually:

  1. Create a folder called .vscode in your workspace.
  2. Create a file called settings.json inside it.
  3. Add these to settings.json:
{
    "python.defaultInterpreterPath": "PATH_TO_VENV_INTERPRETER",
    "python.terminal.activateEnvironment": true
}

Note: What I normally do is, I insert a "python.defaultInterpreterPath" key to my User settings.json which points to my global interpreter. Then I create Workspace settings.json for each of my projects and add the same key which points to my venv's interpreter. Remember, workspace settings.json will overwrite user's settings.json.

This way whenever you open VSCode in a project folder it automatically knows it should activate your venv's interpreter(I told it to do so with "python.terminal.activateEnvironment") and if you open VSCode in a normal folder it correctly use your global's interpreter.

Difference between User and Workspace settings.json.

Deuteronomy answered 26/4, 2022 at 18:59 Comment(3)
This is it. This freaking line is the answer to all questions.Filament
yep, adding "python.terminal.activateEnvironment": true to my settings.json resolves this issue.Shingles
The first step has solved my issue. God blessed.Ensepulcher
K
5

Please select the appropriate interpreter for your running environment.

ctrl+shift+P

and then choose the one like picture.

enter image description here

Kynewulf answered 27/4, 2022 at 6:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.