Here's the answer. Add this to your user and/or workspace settings.json file:
"python.defaultInterpreterPath": "${env:VIRTUAL_ENV}"
.
Then the first time you launch a workspace from an active virtual environment, vscode will set the interpreter correctly. Thereafter it will use whatever interpreter was set the last time the workspace was closed. As long as you don't manually change it, you're set. For existing workspaces, just manually set the interpreter and vscode will always use the interpreter from the prior session. It will never use anything in settings.json (or .env or .venv) except the first time a workspace is launched (and in that case, I think it only uses the settings.json name-value pair shown above).
That will work as-is for virtualenvs managed by pyenv-virtualenv
(or virtualenvwrapper
). Should work for regular virtualenv
too. For conda
, replace VIRTUAL_ENV
with whatever it uses, assuming it sets a similar variable. Just activate something and type env
to see all the environment variables.
This is the solution as long as you create a virtualenv, then launch a workspace for the first time, and the association between the workspace and virtualenv does not change. Unfortunately, it appears you have to set the interpreter manually if the association changes, but you only have to do it once.
The official explanation is here, specifically where it says the interpreter is stored internally i.e. not in any configuration file exposed to the user:
