Programming5 min read10 interview questionsNew · November
Environment variables: settings that live outside your code
How programs read configuration and secrets safely, with Python examples.
In one sentenceAn environment variable is a named setting given to a program from outside, so the same code can run with different values in different places.
Think of it like thisIt is like a label on a lunchbox: the same box (code) can hold different food (settings) depending on who is eating.
The big picture
Tap any step to see where to use it, how to use it and an example result.
Why not put settings in the code
Passwords and API keys in code get copied into Git history, shared by accident and are hard to rotate. Settings such as a database address also differ between your laptop, testing and production. Environment variables keep code the same and values separate.
Reading them in Python
Use the os module. Every value is a string, so convert numbers yourself.
- os.environ["DB_URL"] reads a value and raises an error if it is missing, which is good for required settings.
- os.getenv("PORT", "8000") returns a default when the variable is not set.
- int(os.getenv("PORT", "8000")) converts the string to a number.
.env files
For local work, store variables in a file named .env and load it with a library such as python-dotenv. Add .env to .gitignore and commit an example file with fake values, such as .env.example, so others know what is needed.
Production secrets
Servers and platforms have their own places to set variables. For sensitive values, prefer a secrets manager, restrict who can read them and rotate them regularly. Never print them in logs.
Common mistakes
- Committing a .env file to Git.
- Forgetting that values are strings, so "false" is truthy in an if statement.
- Printing all variables in an error message or log.
- Using the same secrets in development and production.
Quick recap
- Settings live outside the code.
- Required values should fail loudly when missing.
- Use .env locally and keep it out of Git.
- Use a secrets manager for real secrets.
Try it yourself
Write a small script that reads GREETING from the environment, with a default of "Hello", and prints it. Run it with and without the variable set.
Top 10 interview questions
Ordered from basic to advanced. Try answering out loud first, then open the answer.
What is an environment variable?Basic
A named value supplied to a process by its surroundings, such as the shell or the platform. Programs read it to adjust their behaviour.
Why use them instead of hard-coding values?Basic
It keeps secrets out of source code, and lets one codebase run in many environments with different settings.
How do you read one in Python?Basic
Import os and use os.environ["NAME"] for required values or os.getenv("NAME", "default") for optional ones.
What type are environment variable values?Basic
Always strings. Convert them yourself to numbers or booleans, for example int(os.getenv("PORT", "8000")).
What is a .env file and how is it used?Intermediate
A text file of NAME=value lines for local development. A library such as python-dotenv loads it into the environment. It must never be committed.
What is the difference between os.environ["X"] and os.getenv("X")?Intermediate
The first raises KeyError when X is missing. The second returns None or a default you give. Use the strict form for settings the app cannot run without.
Are environment variables a safe place for secrets?Intermediate
Better than code, but not perfect. They can show up in process listings, crash dumps or logs, and are inherited by child processes. For high-value secrets use a secrets manager and limit access.
How do child processes see environment variables?Intermediate
A child process inherits a copy of its parent's variables at the moment it starts. Changes in the child do not affect the parent, and later changes in the parent do not reach the child.
How should an application validate its configuration?Advanced
Read all settings at start-up in one place, check that required ones exist and have valid types or ranges, and exit with a clear message if not, instead of failing later in the middle of a request.
How do environment variables fit into the twelve-factor approach and containers?Advanced
The twelve-factor method says configuration belongs in the environment, separate from code. In containers you pass variables when starting the container or through orchestrator config, so the same image runs everywhere.
Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.