Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 289
PythonNewbie Avatar asked Oct 17 '19 13:10

PythonNewbie


People also ask

How do I enable virtual environment automatically?

Simply cd into a project directory with a python virtual environment setup (with any of these names: venv/ , . venv/ , env or . env ), and the script will activate it automatically for you (just as you would do with source ./venv/bin/activate ).

How do I know if Virtualenv is activated?

Check the $VIRTUAL_ENV environment variable. The $VIRTUAL_ENV environment variable contains the virtual environment's directory when in an active virtual environment. Once you run deactivate / leave the virtual environment, the $VIRTUAL_ENV variable will be cleared/empty.


1 Answers

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.

like image 96
Ahtisham Avatar answered Oct 01 '22 02:10

Ahtisham