Tools & DevOps · reviewed in August 2026
Environment variables
An environment variable is a configuration value (a URL, a key, a debug flag) that lives outside the source code, in the operating system's or the running process's environment, and that the program reads at runtime. They let the same code behave differently across development, testing, and production without changing it.
import os
db_url = os.environ.get("DATABASE_URL", "sqlite:///local.db")Frequently asked questions
Why not put keys and passwords directly in the code?
Because code usually lives in a shared repository (sometimes public), and a secret written there stays exposed in git history forever, even if it's removed later. Environment variables keep secrets out of the code.
How do you read environment variables in Python?
With os.environ.get('VARIABLE_NAME'), which returns None (or a default value you pass) if the variable isn't set in that environment.
What is a .env file?
A text file with key=value pairs that's loaded as environment variables for local development; it should never be committed to version control (excluded via .gitignore) because it usually holds real secrets.