: You can easily swap between Development , Testing , and Production settings without rewriting core functions. Popular Modern Patterns
# config.py DEBUG = True DATABASE_URL = "sqlite:///./test.db" Use code with caution. Copied to clipboard : No extra libraries needed; very fast. Cons : No built-in validation for types or missing values.
: Strong typing prevents runtime crashes from bad config. config.py
class Config: SECRET_KEY = "base-key" class ProductionConfig(Config): DEBUG = False Use code with caution. Copied to clipboard Best Practices for Your Config Working with Credentials and Configurations in Python
The most straightforward approach is defining variables directly in a .py file and importing them. : You can easily swap between Development ,
Using libraries like Pydantic Settings allows you to create a "Settings" class that automatically pulls from environment variables and validates data types.
: By keeping sensitive credentials in a config.py (and adding it to your .gitignore ), you prevent accidentally leaking secrets to public repositories. Cons : No built-in validation for types or missing values
from pydantic_settings import BaseSettings class Settings(BaseSettings): database_url: str port: int = 8080 settings = Settings() Use code with caution. Copied to clipboard