Python YAML Settings to ENV Variables
Convert Python application YAML settings (Django, Flask, FastAPI) to environment variables. Covers django-environ patterns and python-dotenv compatibility.
Detailed Explanation
Python web frameworks commonly use YAML for configuration during development and environment variables for production. Converting between them involves understanding framework-specific conventions and Python's type coercion patterns.
Django settings.yml (loaded via PyYAML):
django:
secret_key: "django-insecure-dev-key-change-me"
debug: true
allowed_hosts:
- localhost
- 127.0.0.1
- ".example.com"
database:
engine: django.db.backends.postgresql
name: myproject
user: postgres
password: localpass
host: localhost
port: 5432
cache:
backend: django.core.cache.backends.redis.RedisCache
location: "redis://localhost:6379/1"
email:
backend: django.core.mail.backends.smtp.EmailBackend
host: smtp.gmail.com
port: 587
use_tls: true
host_user: "user@example.com"
host_password: "app-password"
Equivalent .env file (for django-environ):
# Django core
DJANGO_SECRET_KEY=django-insecure-dev-key-change-me
DJANGO_DEBUG=True
DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1,.example.com
# Database (django-environ uses a DATABASE_URL pattern)
DATABASE_URL=postgres://postgres:localpass@localhost:5432/myproject
# Cache (django-environ URL pattern)
CACHE_URL=redis://localhost:6379/1
# Email
EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_USE_TLS=True
EMAIL_HOST_USER=user@example.com
EMAIL_HOST_PASSWORD=app-password
Python-specific conversion considerations:
django-environ URL patterns. Instead of separate
host,port,user,password,namekeys, django-environ uses a single URL:DATABASE_URL=postgres://user:pass@host:port/dbname. This is more compact but loses granularity.Boolean convention. Python's
True/Falsecapitalization is often used in ENV files targeting Python apps (vs lowercasetrue/falsefor other ecosystems). The django-environ library accepts both.List handling.
DJANGO_ALLOWED_HOSTS=localhost,127.0.0.1is parsed withenv.list('DJANGO_ALLOWED_HOSTS')which splits on commas.Type casting with django-environ:
import environ
env = environ.Env(
DEBUG=(bool, False), # Cast to bool, default False
EMAIL_PORT=(int, 587), # Cast to int, default 587
ALLOWED_HOSTS=(list, []), # Cast to list, default empty
)
Flask/FastAPI equivalent:
# Flask config.yml
flask:
secret_key: dev-key
sqlalchemy_database_uri: "sqlite:///app.db"
max_content_length: 16777216
FLASK_SECRET_KEY=dev-key
SQLALCHEMY_DATABASE_URI=sqlite:///app.db
MAX_CONTENT_LENGTH=16777216
Key pattern: Python frameworks typically expect UPPER_SNAKE_CASE ENV variable names that match the YAML key hierarchy with prefixes removed (e.g., django.secret_key -> DJANGO_SECRET_KEY or just SECRET_KEY).
Use Case
Preparing a Django application for deployment on platforms like Heroku, Render, or Railway by converting the development YAML config to environment variables using django-environ patterns for database URLs and secret management.