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:
.
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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With