We do this exact thing (with Pyramid and global app settings) with a top-level app/config.py
which is just a list of config variables (and possibly their defaults). Then in our main function we load the config and set the various attributes of the config
module so the application can use them.
For example, here's app/config.py
:
image_dir = '/mnt/images'
s3_access_key = None # these must be specified in the config INI
s3_secret_key = None
Here's the relevant part of our config INI (eg: app/development.ini
):
[app:main]
app.image_dir = ./test_images
app.s3_access_key = ABCD1234...
app.s3_secret_key = EFGH5678...
And the relevant config loading code in our app/main.py
:
from . import config
def main(global_config, **settings):
config.image_dir = settings.get('app.image_dir', config.image_dir)
config.s3_access_key = settings['app.s3_access_key']
config.s3_secret_key = settings['app.s3_secret_key']
configurator = Configurator(settings=settings)
# ... configure and add routes
configurator.scan()
return configurator.make_wsgi_app()
And then other application code can simply from app import config
or from . import config
and use the settings, for example: os.path.join(config.image_dir, 'my_image.jpg')