Tools · Lesson 67 of 76
Environment variables
An environment variable is a value the operating system holds, outside your program, that your code can read with os.environ. They're the standard place to keep settings and secrets.
Python
You set them in your terminal before running a program:
# macOS / Linux
export API_KEY="abc123"
# Windows (PowerShell)
$env:API_KEY = "abc123"
Always read with .get(name, default) rather than os.environ[name] — the default keeps your program from crashing when a variable isn't set.
You’re reading for free. Sign in to keep your progress and earn a certificate when you finish.Sign in to keep my progress →