DEV Community

Nihal
Nihal

Posted on

setup config.py

Every project starts the same way…
You hardcode a few values, sprinkle some os.getenv() calls, and tell yourself “I’ll clean this up later.”

Later never comes.

Instead, just do this 👇

Install package:
pip install pydantic-settings

config.py

from pydantic_settings import BaseSettings

class Settings(BaseSettings):
    APP_ENV: str = "development"
    APP_TITLE: str = "App Title"
    APP_VERSION: str = "1.0.0"
    FRONTEND_URL: str = "http://localhost:5173"
    SECRET_KEY: str
    CORS_ORIGINS: list[str] = []

    class Config:
        env_file = ".env"
        env_file_encoding = "utf-8"
        case_sensitive = True
        extra = "ignore"


settings = Settings()
Enter fullscreen mode Exit fullscreen mode

Why This Is Actually Nice

  • You get defaults, so your app runs instantly
  • You get env overrides, so prod doesn’t break
  • You get type safety, no weird bugs later

How You Use It

import config.settings

print(settings.APP_ENV)
Enter fullscreen mode Exit fullscreen mode

Boom. Done.

Top comments (0)