Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Environment Variables for VSCode Debug session

I want to set an environment variable FOO to be available in VS Code debug sessions started on the current file by hitting the Debug button in the top right corner

This one: enter image description here.

I tried setting the env dictionary in the launch.json file like so:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "env": {
                "FOO": "BAR"
            }
        }
    ]
}

But when I try to read the variable in my code, is get a KeyError since the variable hasn't been set.

import os
print(os.environ["FOO"])

yields this:

Traceback (most recent call last):
  File ".../save_model.py", line 74, in <module>
    print(os.environ["FOO"])
  File ".../lib/python3.10/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'FOO'
like image 497
Daniel Schneider Avatar asked Mar 08 '26 22:03

Daniel Schneider


1 Answers

So, it turns out that if you hit F5, everything works as expected. If, however you use the button shown above, environment variables are not set per the launch.json file.

like image 197
Daniel Schneider Avatar answered Mar 10 '26 15:03

Daniel Schneider



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!