📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials Python from Zero Environment Variables

Environment Variables

4 min read
Read environment variables with os.environ.get('KEY', 'default'). Use python-dotenv to load a .env file in development. Never commit your .env file — add it to .gitignore and document keys in .env.example.

Environment Variables

import os
from dotenv import load_dotenv  # pip install python-dotenv

load_dotenv()  # loads .env file

DB_URL = os.getenv("DATABASE_URL")
SECRET  = os.environ.get("SECRET_KEY", "default")

# .env file
# DATABASE_URL=postgresql://localhost/mydb
# SECRET_KEY=mysupersecret