Chapter 10: Configuration and Settings
ASP.NET Core has layered configuration providers and options binding. Python commonly uses environment variables with a typed settings class.
from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
model_config = SettingsConfigDict(
env_file=".env",
env_prefix="RECIPEVAULT_",
extra="ignore",
)
app_name: str = "RecipeVault"
database_url: str = Field(default="sqlite:///recipevault.db")
secret_key: str
debug: bool = False
Rules:
- Treat settings as immutable values at app startup.
- Never commit real secrets.
- Use
.envonly for local development. - In production, use the host's secret manager or environment injection.
- Fail fast when required settings are missing.
- Keep framework settings separate from domain configuration.
Configuration is one area where Python is more fragmented than .NET. Pick one settings approach and use it everywhere.