Auto activate virtual environment in Visual Studio Code
Solution 1
This is how I did it in 2021:
-
Enter Ctrl+Shift+P in your vs code.
-
Locate your Virtual Environment:
Python: select interpreter > Enter interpreter path > Find
-
Once you locate your virtual env select your python version:
your-virtual-env > bin > python3.
-
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.
-
Now restart the terminal.
You should see your virtual environment activated automatically.
Solution 2
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).
Solution 3
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
.
Solution 4
There is a new flag that one can use: "python.terminal.activateEnvironment": true
Related videos on Youtube
PythonNewbie
Updated on July 09, 2022Comments
-
PythonNewbie almost 2 years
I want VS Code to turn venv on run, but I can't find how to do that. I already tried to add to settings.json this line:
"terminal.integrated.shellArgs.windows": ["source${workspaceFolder}\env\Scripts\activate"]
But, it throws me an 127 error code. I found what 127 code means. It means,
Not found
. But how it can be not found, if I see my venv folder in my eyes right now?I think it's terminal fault. I'm using Win 10 with Git Bash terminal, that comes when you install Git to your machine.
-
r.ook over 4 yearsI don't think you need this line at all. Try removing this entire line from your settings and VS Code should handle the workspace venv for you.
-
PythonNewbie over 4 years@Krrr yep, it did, but it did on .py file start, not IDE at all as I want
-
PythonNewbie over 4 years@Krrr nope, terminal shows line as env is not actived.
-
PythonNewbie over 4 years@Krrr yea, in my workspace i have
env
,.vscode
and my project folders.
-
-
Georgi Stoyanov almost 3 yearsthis solution requires creating a new terminal to activate the
venv
, the solution of @moojen is more elegant, activating thevenv
in the current terminal. -
Doug J. Huras over 2 yearsThis worked for me. However, I did not have to edit the settings.json file. Once I did the "find", I selected my virtual environment python.exe file from a Windows file dialog. I am using virtualenv instead of env. My path is: ".\.venvs\<project folder name>\Scripts\python.exe". Once I closed and re-opened the terminal, it was showing prefix of my <project folder name>. Only my virtual project has nose2 installed and it was recognized in the editor now.
-
niid over 2 years"python.terminal.activateEnvironment" is true by default. It will require closing and reopening the default terminal in order to activate the venv.
-
niid over 2 yearsIt will still not activate the venv in the terminal unless you create a new Terminal instance.
-
Aris Koning over 2 yearsThis works for the built-in terminal. However I have some defined build steps for my Python project. I like to you use VSCode tasks for these. Do you know how to also activate the Python venv for tasks in VSCode?
-
thoroc about 2 yearsIf you virtual environment is defined in at the root of the project's directory for the current project you should use:
${workspaceFolder}
as the prefix. -
Brian Reinhold almost 2 yearsThere was no 'settings.json' file in my .vscode directory. Just argv.json and launch.json