Auto activate virtual environment in Visual Studio Code

This is how I did it in 2021:

  1. Enter Ctrl+Shift+P in your vs code.

  2. Locate your Virtual Environment:

    Python: select interpreter > Enter interpreter path > Find

  3. Once you locate your virtual env select your python version:

    your-virtual-env > bin > python3.

  4. Now in your project you will see .vscode directory created open settings.json inside of it and add:

    "python.terminal.activateEnvironment": true

    don't forget to add comma before to separate it with already present key value pair.

  5. Now restart the terminal.

You should see your virtual environment activated automatically.


There is a new flag that one can use: "python.terminal.activateEnvironment": true


Actually the earlier suggested solutions didn't work for me, instead I added the following in my settings:

"settings": {
    "python.terminal.activateEnvInCurrentTerminal": true,
    "python.defaultInterpreterPath": "~/venv/bin/python"
}

Of course replace the defaultInterpreterPath (used to be pythonPath) setting with your own path (so don't copy/paste the second line).


You don't need this line at all. Just remove it and switch your Python interpreter to point to the one within your venv. Here's a relevant documentation (italicized emphasis mine):

To select a specific environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P).

... and opening a terminal with the Terminal: Create New Integrated Terminal command. In the latter case, VS Code automatically activated the selected environment.

Once you switch the interpreter VS code should create a .vscode folder within your workspace with a settings.json indicating the python interpreter. This will give VS code the direction of where to locate the venv.