I am following a guide that specifies how to manage different configurations (DB_URI, LOGGER_NAME, etc...) per development environments.
I create a config module that is structured like this
\my_project\config\__init__.py
import os
import sys
from . import settings
# create settings object corresponding to specified env
APP_ENV = os.environ.get('APP_ENV', 'Dev')
_current_config = vars(setting).get(f'{APP_ENV}Config')
# copy attributes to the module for convenience
for atr in [f for f in dir(_current_config) if '__' not in f]:
# environment can override anything
val = os.environ.get(atr, getattr(_current_config, atr))
setattr(sys.modules[__name__], atr, val)
\my_project\config\settings.py
class BaseConfig:
DEBUG = True
LOGGER_NAME = 'my_app'
class DevConfig(BaseConfig):
# In actuality calling a function that fetches a secret
SQLALCHEMY_DATABASE_URI = 'sqlite://user.db'
class ProductionConfig(BaseConfig):
DEBUG = False
# In actuality calling a function that fetches a secret
SQLALCHEMY_DATABASE_URI = 'sqlite://user.db'
calling the config
application.py
import config
config.SQLALCHEMY_DATABASE_URI
This approach is kind of a hit and miss for me.
on one hand it allows me to easily specify several environments (dev, stage, prod, dev) without having a complex if else logic in my settings.py.
On the other hand I dont like using setattr on sys.modules[__name__], seems a bit wonky to me. what do you think ?