Skip to main content

What are environment variables?

Environment variables are settings stored outside your code. Think of them as configuration that your program can read.

The problem they solve

Problems:
  • Secrets visible in code
  • Can’t share code safely
  • Different settings for different computers

How environment variables work

Environment variables live in your system, not your code:

Setting environment variables

You can set them temporarily in your terminal:
But they disappear when you close the terminal!

Reading in Python

Common uses

  • API Keys: OPENAI_API_KEY, GITHUB_TOKEN
  • Database URLs: DATABASE_URL, REDIS_URL
  • App Settings: DEBUG=True, PORT=8000
  • File Paths: LOG_DIR, UPLOAD_FOLDER

Important notes

  • Environment variables are always strings
  • Names are UPPERCASE by convention
  • They’re different on each computer
  • They don’t persist between terminal sessions

What’s next?

Setting variables manually is tedious. Let’s use .env files for a better workflow.

Using .env files

Easier way to manage variables