简体   繁体   中英

Auto activate virtual environment in Visual Studio Code

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.

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.

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 .

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

my scenario was pretty much the same. I am running VSCode on Windows, wanting to used git bash as my default Terminal but after the venv got created, it was doing some weird stuff when a Terminal would open where it couldn't find the correct python interpeter in the venv/Scripts folder even though I did ctrl-shift-p a bunch of times to reset it to the python.exe there. I also wanted to make sure the activate script was run on Terminal open. I just couldn't get the debugger to work right and it kept complaining that it could not find the python interpreter, so I basically couldn't debug at all.

So for anyone who is having weird stuff happen trying to use a Git Bash Terminal in VSCode in Windows related to a python project using a virtual env, here is what I found out;

  1. I noticed that when I opened a new Git Bash Terminal, and looked at the $PATH variable to make sure it could find the interpreter in the venv, the path to the venv/Scripts folder would be prepended to the $PATH, but not with linux path separators like everything else in $PATH but with a Windows style path;
echo $PATH
C:\Users\blah\Documents\blah\Stock-down\Dev\this_api\venv/Scripts:/c/Users/blah/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/blah/bin:/c/Program Files/Go/bin:/c/Python39/Scripts: 

So whenever I would try to debug, or just even run which python, It would get confused and thought the interpreter was in here;

which python
C:\Users\blah\Documents\blah\Stock-down\Dev\this_api\venv/Scripts/C/Users/blah/Documents/blah/Stock-down/Dev/this_api/venv/Scripts/python.exe
  1. I ran the "printenv" command in the Terminal to see what env vars were getting set and why the venv interpreter path was getting messed up. And I found a env var I didn't know existed - VIRTUAL_ENV. But I didn't know how it was getting set or where it came from. After some pain, and hunting around I found it - when you run "python -m venv venvname" to create the virtual env in the project folder, as you know, it creates the activate (and activate.bat for windows) scripts in the./venv/Scripts folder. Inside these files this VIRTUAL_ENV variable is not only exported, but but prepended to the $PATH variable on Terminal open with the "/Scripts" folder name added in linux path style. The problem with is that it sets the VIRTUAL_ENV value with windows type path - I know its painful and wrong to do this, but I just changed it to what Git Bash is expecting, see below;
#unset irrelevant variables
deactivate nondestructive

#VIRTUAL_ENV="C:\Users\blah\Documents\blah\Stock-down\Dev\this_api\venv"
VIRTUAL_ENV="/c/Users/blah/Documents/blah/Stock-down/Dev/st_api/venv"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/Scripts:$PATH"
export PATH
  1. Now when a new Git Bash Terminal is opened it prepends the venv/Scripts path correctly to $PATH;
echo $PATH
/c/Users/blah/Documents/blah/Stock-down/Dev/st_api/venv/Scripts:/c/Users/blah/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/blah/bin:/c/Program Files/Go/bin:/c/Python39/Scripts: 
  1. I also made a copy of the executable python.exe in the./venv/Scripts dir and just called it "python" and now the command "which python" works and I can debug fine. I still set ctrl-shift-P when I choose the interpreter in VSCODE to "python.exe" when I just use "find" during the selection.

For more information on VIRTUAL_ENV var, see this doc -- python venv docs

It feels like jumping through a bunch of hoops I know, but this way I can open a new Git Bash Terminal, have the activate script run correctly on Terminal Open, debug, and operate normally without having to float between Git Bash AND WSL Ubuntu AND Powershell, etc.

My environment started to activate automatically after the advice from this article

When you create a new virtual environment, a prompt will be displayed to allow you to select it for the workspace. This will add the path to the Python interpreter from the new virtual environment to your workspace settings. That environment will then be used when installing packages and running code through the Python extension. For examples of using virtual environment in projects, see the Python, Django, and Flash tutorials.

My solution was to create /.vscode/settings.json manually

Here is the tree;

├───django
│   ├───.vscode
│   │   ├───settings.json

I created /.vscode/settings.json then added this code in settings.json (I am using windows so I used double backslash for path location otherwise it gives unicode error, and don't copy paste this, find your own.virtualenvs)

{
    "python.defaultInterpreterPath": "C:\\Users\\Talha\\.virtualenvs\\django-okd21pq9\\Scripts\\python.exe"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM