FastAPI documentation recommends using lru_cache decorated functions to retrieve the config file. That makes sense to avoid I/O getting the env file.
config.py
from pydantic import BaseSettings
class Settings(BaseSettings):
app_name: str = "Awesome API"
admin_email: str
items_per_user: int = 50
class Config:
env_file = ".env"
and then in other modules, the documentation implemented a function that gets the settings
#module_omega.py
from . import config
@lru_cache()
def get_settings():
return config.Settings()
settings = get_settings()
print(settings.ENV_VAR_ONE)
I am wondering if this method is better practice or advantageous to just initializing a settings object in the config module and then importing it like below.
#config.py
from pydantic import BaseSettings
class Settings(BaseSettings):
app_name: str = "Awesome API"
admin_email: str
items_per_user: int = 50
class Config:
env_file = ".env"
settings = Settings()
#module_omega.py
from .config import settings
print(settings.ENV_VAR_ONE)