Home > Back-end >  Unable to use environment variables in VS Code launch configuration
Unable to use environment variables in VS Code launch configuration

Time:11-11

I have the following in my workspace settings.json file:

"terminal.integrated.env.osx": {
    "AUTH_TOKEN": "secret_XXXXXX"
}

However, when trying to pass this via a launch command (defined in launch.json):

    {
        "name": "Example: Query",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/examples/query.py",
        "args": [ "${env:AUTH_TOKEN}" ]
    }

The resulting command contains an empty string for the argument:

/usr/bin/env /.../.venv/bin/python /.../debugpy/launcher 58644 -- /.../examples/query.py ""

However, if I print the variable from within the script, it is set properly.

I believe there is an ordering issue, such that the launch.json commands are generated before the terminal environment is set up - resulting in empty vars. Any ideas how to propagate the env value to the command line?

Update: I have also tried using a .env file for the variables (rather than settings.json), but the result is the same.

CodePudding user response:

You can create a .env file, and then put the variable in there, and read it from the environmental variables in the program instead of it being an argument.

CodePudding user response:

Try using "env" in launch.json...

{
          "name": "Example: Query",
          "type": "python",
          "request": "launch",
          "program": "${workspaceFolder}/examples/query.py",
           
          "env": {
                "AUTH_TOKEN": "XXXX",
                "ENV2" : "XXX"
          }
            
}

  • Related