VS Code's terminal using a different python interpreter than the one you've selected
By default, it doesn't know about your interpreter, and will initialize using the default .bashrc
or equivalent in the OS.
I found two relevant settings from an issue in Feb 2021.
Checking the second option Python > Terminal: Activate Environment
enables automatic activation of virtual environment:
In settings.json
it is called "python.terminal.activateEnvironment": true
.
Update (8 Aug 2021):
Today when I opened the terminal from VS Code on Windows, it automatically inserted a line of code & C:/Users/[UserName]/[venv]/Scripts/Activate.ps1
to activate the appropriate environment associated with the selected python interpreter!
It appears the aforementioned settings is now the default behavior.
While there are changes to Terminal behavior in the release notes of July 2021 (version 1.59), I don't see virtual environment activation being explicitly mentioned.
The new behavior is documented here, in "Environments and Terminal windows".
Therefore most previous answers are outdated.
Use "python.defaultInterpreterPath"
instead:
2021.6.0 (16 June 2021)
5. Added python.defaultInterpreterPath setting at workspace level when in pythonDeprecatePythonPath experiment. (#16485)
8. Show python.pythonPath deprecation prompt when in pythonDeprecatePythonPath experiment. (#16485)
2020.7.0 (16 July 2020)
9. Prompt users that we have deleted pythonPath from their workspace settings when in Deprecate PythonPath experiment. (#12533)
2020.5.0 (12 May 2020)
6. Do a one-off transfer of existing values for python.pythonPath setting to new Interpreter storage if in DeprecatePythonPath experiment. (#11052)
8. Added prompt asking users to delete python.pythonPath key from their workspace settings when in Deprecate PythonPath experiment. (#11108)
12. Rename string ${config:python.pythonPath} which is used in launch.json to refer to interpreter path set in settings, to ${config:python.interpreterPath}. (#11446)
2020.4.0 (20 April 2020)
13. Added a user setting python.defaultInterpreterPath to set up the default interpreter path when in Deprecate PythonPath experiment. (#11021)
If you wish to set a default python interpreter for all workspaces, open settings with Ctrl+Shift+P
, Preferences: Open User Settings
and search for Python: Default Interpreter Path
. Otherwise, if you want to set it for only the current workspace, use Preferences: Open Workspace Settings
instead.
In your case, you wish to set it to ${workspaceFolder}/env/bin/python3.6
.
If you edit settings.json
directly instead of using the GUI:
{
"python.defaultInterpreterPath": "${workspaceFolder}/env/bin/python3.6"
}
Detailed instructions can be found in the documentation "Manually specify an interpreter", including using environment variables as the interpreter's path.
zsh
and defaulted to2.7
. After changing interpreter to3.6
& openingbash
in VS Code all was well. – Crusted