Home » Python » Visual Studio Code overrides python interpreter in integrated terminal-Exceptionshub

Visual Studio Code overrides python interpreter in integrated terminal-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

When launching an Integrated Terminal for Visual Studio Code for a Python project that relays on its own virtual environment, the path to the python binary rests unmodified. In other words, the output of the integrated terminal is as follows (after launching it):

[email protected]:~/repos/project$ source /home/user/repos/project/.env/bin/activate
(.env) [email protected]:~/repos/project$ which python
/usr/bin/python
(.env) [email protected]:~/repos/project$ which python3
/usr/bin/python3
(.env) [email protected]:~/repositories/karbon/kuring$ echo $PATH

/home/user/repos/project/.env/bin:/home/user/local/bin:/home/user/.cargo/bin:/home/user/local/bin:/home/user/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

The virtual environment is configured in the project settings as follows:

.vscode/settings.json:

{"python.pythonPath": ".env/bin/python",}

The interpreter has been selected in the command palette to point to the same file path.

It is strange that even running “source .env/bin/activate” in a separate terminal seems to be loading the virtual environment, but the Python path remains unaltered.

Question: shouldn’t VS Code be loading the interpreter from the virtual environment instead of the one from the operating system?

Please notice than in a regular BASH terminal outside VS code, I have no problems when it comes to use the virtual environment.

How to&Answers:

VS code’s Python extension modifies the path in both Linux and MAC. This behavior is intended since when a new terminal is created, the environment is inherited by default from the VS code IDE rather than from the regular OS.

This problem an be fixed setting up a variable in the user settings JSON file.

More details here: https://github.com/microsoft/vscode-python/issues/10131