How to auto-activate the environment after the start in version 1.56?

Viewed 878

Is there any way how to auto-activate the environment in VS Code 1.56?

Previously, I used the following workspace settings to auto-activate the environment in the terminal:

"terminal.integrated.shellArgs.linux": [
    "/path/to/activate.sh",
],

It activated the environment right after starting up.

These settings were deprecated in the latest version:

https://code.visualstudio.com/updates/v1_56#_terminal

I tried to use the new settings:

"terminal.integrated.profiles.linux": {
    "bash": {
        "path": "bash",
        "args": [
            "/path/to/activate.sh",
        ],
    },
},

Unfortunately, it only works for New Terminals or when I have a different tab open after the start and switch to Terminal after VS Code is fully loaded.

Is there any way how to activate the environment rightaway?

2 Answers

This worked for me in my settings.json:

    "terminal.integrated.defaultProfile.linux": "zsh",
    "terminal.integrated.profiles.linux": {
        "bash": {
            "path": "bash"
        },
        "zsh": {
            "path": "zsh",
            "args": ["-l"]
        },
        "fish": {
            "path": "fish"
        },
        "tmux": {
            "path": "tmux",
            "icon": "terminal-tmux"
        },
        "pwsh": {
            "path": "pwsh",
            "icon": "terminal-powershell"
        }
    },

Make sure to add the "args" key to the shell you are using. In my case it is zsh so I added the "-l" argument to load my .zprofile script.

Hopes this helps you out! :)

For more details on Terminal Profiles Confs checkout: https://code.visualstudio.com/updates/v1_55#_terminal-profiles

To "auto" activate a python venv and load .env file on the integrated terminal when you open the project or open another terminal make sure you have the following settings set:

User Setting UI

Python: Env File
${workspaceFolder}/.env
Python>Terminal: Activate Env In Current Terminal
True
Python>Terminal: Activate Environment
True

Make sure you venv is at the root of you project folder, eg:

./MyProject (workspaceFolder)
./MyProject/venv
./MyProject/.env
./MyProject/djangoproject
./MyProject/djangoproject/[djangoapps]
./MyProject/djangoproject/manage.py
./MyProject/setup.py
./MyProject/requirements.txt

If that fails try setting the workspace settings to:

"python.pythonPath": "./venv/bin/python",
"python.terminal.activateEnvInCurrentTerminal": true,

As a reference here are my current setting:

User settings.json:

{
    "python.sortImports.path": "./venv/bin/python",
    "terminal.integrated.defaultProfile.linux": "zsh",
    "python.terminal.activateEnvInCurrentTerminal": true,
    "terminal.integrated.profiles.linux": {
        "bash": {
            "path": "bash"
        },
        "zsh": {
            "path": "zsh",
            "args": ["-l"]
        },
        "fish": {
            "path": "fish"
        },
        "tmux": {
            "path": "tmux",
            "icon": "terminal-tmux"
        },
        "pwsh": {
            "path": "pwsh",
            "icon": "terminal-powershell"
        }
    },
    "terminal.integrated.profiles.osx": {
        "bash": {
            "path": "bash"
        },
        "zsh": {
            "path": "zsh",
            "args": ["-l"]
        },
        "fish": {
            "path": "fish"
        },
        "tmux": {
            "path": "tmux",
            "icon": "terminal-tmux"
        },
        "pwsh": {
            "path": "pwsh",
            "icon": "terminal-powershell"
        }
    },
}

Workspace setting.json:

{
    "python.pythonPath": "./venv/bin/python",
    "python.terminal.activateEnvInCurrentTerminal": true,
}
Related