Python / Uvicorn Fundamentals Interview Questions
How do you configure Uvicorn with environment variables?
Most Uvicorn CLI flags can be set via UVICORN_* prefixed environment variables, making it easy to configure in containerised environments and CI/CD pipelines without changing command-line arguments.
# Environment variable equivalents of CLI flags export UVICORN_HOST=0.0.0.0 export UVICORN_PORT=8000 export UVICORN_WORKERS=4 export UVICORN_LOG_LEVEL=warning export UVICORN_LOOP=uvloop export UVICORN_HTTP=httptools export UVICORN_RELOAD=false export UVICORN_NO_ACCESS_LOG=true export UVICORN_PROXY_HEADERS=true export UVICORN_FORWARDED_ALLOW_IPS="10.0.0.1" # Then run with minimal flags: uvicorn main:app # Partial override: env vars + CLI flags (CLI takes precedence) export UVICORN_PORT=8000 uvicorn main:app --port 9000 # uses 9000 (CLI overrides env var) # WEB_CONCURRENCY is also respected for workers: export WEB_CONCURRENCY=4 uvicorn main:app # NOTE: UVICORN_* env vars cannot be set inside --env-file # --env-file is for configuring your APPLICATION, not Uvicorn itself
Important distinction - --env-file: the --env-file flag loads environment variables into the application process, but UVICORN_* prefixed settings in that file are ignored. The --env-file is intended for your application's configuration (e.g. DATABASE_URL), not for Uvicorn's own settings.
More Related questions...