: Your logic doesn't need to know where a database is; it just needs the connection string provided by the config.
Depending on your project's complexity, there are several ways to structure this file:
Using libraries like Pydantic Settings allows you to create a "Settings" class that automatically pulls from environment variables and validates data types. 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
: You can easily swap between Development , Testing , and Production settings without rewriting core functions. Popular Modern Patterns : Your logic doesn't need to know where
: Strong typing prevents runtime crashes from bad config.
# 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. Copied to clipboard Best Practices for Your Config
The most straightforward approach is defining variables directly in a .py file and importing them.