Sunday, September 4, 2022

[SOLVED] Visual Studio Code overrides python interpreter in integrated terminal

Issue

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):

user@host:~/repos/project$ source /home/user/repos/project/.env/bin/activate
(.env) user@host:~/repos/project$ which python
/usr/bin/python
(.env) user@host:~/repos/project$ which python3
/usr/bin/python3
(.env) user@host:~/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.


Solution

You should ad the reference to your working folder within the settings.json:

.vscode/settings.json:
{"python.pythonPath": "${workspaceFolder}/.env/bin/python",}


Answered By - リカルド
Answer Checked By - David Goodson (WPSolving Volunteer)