Environment Variables & .env Files
Part of the Python Roadmap
In short
Hard-coding an API key in your script is how keys end up on public GitHub within minutes. Environment variables keep secrets and settings outside your code, so the same code runs safely on your laptop and on a server.
Environment variables keep settings and secrets like API keys outside your code. os.environ reads them, and a .env file loaded with python-dotenv is handy in development, as long as it is never committed.

Think of it like this
The locker combination in your head
You don't paint your locker combination on the locker door. You keep it in your head, and each person who needs it is told separately. Environment variables are that private note: the code says "use the payment key", and each machine supplies its own key quietly.
Watch it happen
Where a setting comes from
load_dotenv()
1/3In development, python-dotenv reads KEY=value lines from .env into the process's environment.
The big ideas
What you need to know
os.environ
A dict-like mapping of the environment. os.environ["KEY"] raises KeyError if missing, os.environ.get("KEY", default) does not.
.env for local dev
A text file of KEY=value lines, loaded by python-dotenv. Handy locally, but add .env to .gitignore on day one.
Commit a .env.example
List every variable name with placeholder values, so teammates know what to set without seeing your secrets.
Everything is a string
DEBUG=false is the string "false", which is truthy. Convert numbers and booleans yourself.
See it in code
Walk through the code
1import os2 3from dotenv import load_dotenv4 5load_dotenv() # reads .env if present; real env vars win by default6 7API_KEY = os.environ["SMS_API_KEY"] # required: crash early if missing8SENDER_ID = os.environ.get("SMS_SENDER_ID", "INSDGE")9DEBUG = os.environ.get("DEBUG", "false").lower() == "true"10TIMEOUT = int(os.environ.get("TIMEOUT_SECONDS", "10"))11 12print(f"Debug mode: {DEBUG}, timeout: {TIMEOUT}s")1/4load_dotenv() copies values from .env into os.environ. It does not overwrite variables already set.
Install python-dotenv with pip install python-dotenv. The .env file next to it holds lines like SMS_API_KEY=sk_test_123.
Save yourself the pain
Common mistakes
Oops
Committing the .env file to Git.
Do this instead
Add .env to .gitignore before your first commit. If a key leaks, revoke it and create a new one; deleting the file later does not erase Git history.
Oops
Writing if os.environ.get("DEBUG"): and expecting "false" to switch it off.
Do this instead
Any non-empty string is truthy. Compare explicitly: value.lower() == "true".
Oops
Printing secrets in logs while debugging.
Do this instead
Log whether a key is set, never the key itself.
Your mission
Move a secret out of your code
Quick check
What does os.environ.get("PORT", "8000") return when PORT is set to 5000 in the environment?
Go deeper
- 1Read a value with os.environ.get('API_KEY') and handle the missing case.
- 2Load a .env file with python-dotenv in a small script.
- 3Add .env to .gitignore and commit a .env.example with dummy values instead.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.