How to autoload venv/bin/activate in vscode on mac

Question:

I have django project folder with venv environment.
when opening vscode it has terminal opened in vscode.

Is there a way that I don’t have to venv/bin/activate all the time when opening the project folder?

Asked By: XJOJIX

||

Answers:

Use the command palette to trigger the "Python: select interpreter" command. It should allow you to pick your virtual environment.

enter image description here

The article Using Python environments in VS Code migth be of interest to you.

Answered By: Marijn

Edit (credit to @XJOJIX) from the comment in this answer. This will active the virtual environment without having to close or open terminals. A Python file still needs to be selected to load the Python extension.

Add this parameter in VS Code to "launch.json" or ".code-workspace"

    "settings": {
        "python.terminal.activateEnvInCurrentTerminal": true
    }

Previous Answer:

To have a VS Code terminal automatically activate a virtual environment when first launching VS Code:

  1. Close the terminal before exiting VS Code.
  2. Open VS Code. Ensure a Python file is selected to direct VS Code to load the Python extension.
  3. Wait for the Python extension to finishing loading (very bottom
    left of VS Code terminal).
  4. Open a new terminal after Python extension has loaded. The venv will automatically activate.

The trick is to open the terminal only after the Python extension has loaded.

If the directory of the terminal contains a virtual environment, VS Code will also automatically activate a virtual environment if a new terminal is opened. As before, a Python file must be selected and the Python extension must be fully loaded.

Answered By: Jason Cook

If you use window machine, it might locate at

env/Scripts/activate

You can run above in your vscode terminal to activate your venv

Answered By: Jaden

If you are on a mac OS, the simplest thing is to make ENV for your python projects.

Follow these simple Command in your Vs Code Terminal and you get your ENV activated :

Python3 -m venv env
source env/bin/activate 

(The env in the first line is your env name so you can type any name)

Answered By: R0BB1NX