How Do I Add Environment Variables to launch.json in VSCode?

VS Code lets you pass environment variables to a debug session from .vscode/launch.json. This is useful when a Python app needs settings like an API base URL, feature flag, database name, or development mode value while you are debugging locally.

The simplest option is the env object inside a debug configuration. Each key becomes an environment variable for the launched process, and each value should be a string.

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Debug app",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/app.py",
            "console": "integratedTerminal",
            "env": {
                "APP_ENV": "development",
                "API_BASE_URL": "http://localhost:8000",
                "DEBUG_SQL": "1"
            }
        }
    ]
}

Inside Python, read those values with os.environ or os.getenv(). Using os.getenv() is convenient when you want to provide a fallback for local development.

import os

app_env = os.getenv("APP_ENV", "production")
api_base_url = os.getenv("API_BASE_URL")
debug_sql = os.getenv("DEBUG_SQL") == "1"

print(app_env, api_base_url, debug_sql)

If you have many variables, avoid making launch.json too noisy. The Python debugger also supports an envFile setting, commonly pointing to a .env file in the workspace. This keeps launch configuration readable while still making variables available during debugging.

{
    "name": "Python: Debug with env file",
    "type": "python",
    "request": "launch",
    "program": "${workspaceFolder}/app.py",
    "console": "integratedTerminal",
    "envFile": "${workspaceFolder}/.env"
}

A matching .env file can look like this:

APP_ENV=development
API_BASE_URL=http://localhost:8000
DEBUG_SQL=1

Use env for a few debug-specific overrides and envFile for a larger set of local settings. If both are present, values set directly in env are useful for overriding values from the file in that specific debug configuration.

If a value is missing during debugging, first make sure you started the program with the intended launch configuration. Running the file with a different button or an extension command may bypass your configuration. Also restart the debug session after editing launch.json; environment variables are copied when the process starts, not while it is already running.

For multi-root workspaces, be careful with ${workspaceFolder}. It points to the selected workspace folder, so an envFile path that works in one folder may not exist in another. When in doubt, print the value in Python and confirm the debugger is loading the file you expect.

Do not commit real secrets to launch.json or .env. For passwords, tokens, and production credentials, use your operating system secret store, a deployment secret manager, or a local ignored file. A safe pattern is to commit an example file such as .env.example and keep the real .env out of Git.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x