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.
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:
- Open up your Command Palette(press
ctrl+shift+P
orf1
) and type :"python: select interpreter"
. - Browse/Select your newly created python interpreter's path inside your venv.
- Add
"python.terminal.activateEnvironment": true
to yoursetting.json
file(It's also possible via GUI setting of course by searching forpython: 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:
- Create a folder called
.vscode
in your workspace. - Create a file called
settings.json
inside it. - 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.
Please select the appropriate interpreter for your running environment.
ctrl+shift+P
and then choose the one like picture.
© 2022 - 2024 — McMap. All rights reserved.