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+Porf1) and type :"python: select interpreter". - Browse/Select your newly created python interpreter's path inside your venv.
- Add
"python.terminal.activateEnvironment": trueto yoursetting.jsonfile(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
.vscodein your workspace. - Create a file called
settings.jsoninside 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.
Difference between User and Workspace settings.json.
Videos
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+Porf1) and type :"python: select interpreter". - Browse/Select your newly created python interpreter's path inside your venv.
- Add
"python.terminal.activateEnvironment": trueto yoursetting.jsonfile(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
.vscodein your workspace. - Create a file called
settings.jsoninside 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.
Difference between User and Workspace settings.json.
Please select the appropriate interpreter for your running environment.
ctrl+shift+P
and then choose the one like picture.

Hint: The original question is about VsCode but this answer is about VsCodium.
a) In VsCodium, Pylance is not shown as extension by default. As a hack, one could adapt the extensionsGallery settings to show and install Pylance:
In VSCodium\resources\app\product.json
set
"extensionsGallery": {
"serviceUrl": "https://marketplace.visualstudio.com/_apis/public/gallery",
"cacheUrl": "https://vscode.blob.core.windows.net/gallery/index",
"itemUrl": "https://marketplace.visualstudio.com/items"
},
Source:
https://github.com/VSCodium/vscodium/discussions/1641
However, that might not be compatible with the license terms of pylance.
b) If Pylance does not work, try basedpyright as an alternative extension (open source instead of closed source):
c) Or switch to the Jedi Language server in VsCodium settings.
https://github.com/detachhead/basedpyright/
Search for Pylance in the extension panel and click Install

This extension is included in the Python extension, you can also uninstall Python and reinstall it.
I found this link that informs us that we should add an extra path.
These extra roots must be specified to the language server. The easiest way to do this (with the VS Code Python extension) is to create a workspace configuration which sets python.analysis.extraPaths. For example, if a project uses a sources directory, then create a file .vscode / settings.json in the workspace with the contents:
{
"python.analysis.extraPaths": ["./sources"]
}
https://github.com/microsoft/pylance-release/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings
Another easy way to solve this on VSCODE:
ctrl+ ','- type "extrapaths"
- Down you should have something like "add element" (I have VSCode on Spanish so in my case I have "Agregar elemento")
- type './sources/'
Also if you have problems importing local files, you can do the same thing and add your working directory path to solve the problem :) just add 'C: your working directory goes here' in addition to './sources' in the same way ;)