senseiwa
senseiwa

Reputation: 2499

Use environment variables in VS Code launch configurations

This question is related to this question about conda, and it is pretty straightforward:

How can I use an external environment variable inside launch.json?

For instance, selecting the python executable inside my home with $HOME, or the executable:

    {
        "name": "Python: From Home",
        "type": "python",
        "request": "launch",
        "program": "$HOME/Documents/a.py", // nor does "${HOME}" work
        "console": "internalConsole",
        "cwd": "${workspaceFolder}"
    }

or

    {
        "name": "Python: With Anaconda",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "console": "internalConsole",
        "python": "${HOME}/anaconda3/bin/python3",
        "cwd": "${workspaceFolder}"
    }

This would simplify sharing launch.json with coworkers.

Upvotes: 12

Views: 44908

Answers (2)

Mykola Zotko
Mykola Zotko

Reputation: 17912

You can use the attribute "env" in settings.json. For example:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,

            // ENVINRONMENT VARIABLES
            "env": {                 
                "VAR_A": "value_a",
                "VAR_B": "value_b"

            }
        }
    ]
}

Upvotes: 4

rioV8
rioV8

Reputation: 28783

You can use Environment variables

The syntax is like ${env:USERNAME}

Upvotes: 7

Related Questions