Skip to content

Instantly share code, notes, and snippets.

@dgarana
Created July 7, 2016 16:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgarana/c052a3287629dd7c0b0c9d7921081e9d to your computer and use it in GitHub Desktop.
Save dgarana/c052a3287629dd7c0b0c9d7921081e9d to your computer and use it in GitHub Desktop.
Python load dynamic settings module through environment
# -* coding: utf-8 *-
"""
Config import handler
This let us import settings, and don't care about:
>>> settings = importlib.import_module('module_name')
"""
import os
import importlib
my_module = importlib.import_module(os.getenv('SETTINGS_MODULE'))
my_module_dict = my_module.__dict__
try:
to_import = my_module.__all__
except AttributeError:
to_import = [name for name in my_module_dict if not name.startswith('_')]
globals().update({name: my_module_dict[name] for name in to_import})
# -* coding: utf-8 *-
MY_SETTINGS = 'local'
MY_DB = 'localDB'
# -* coding: utf-8 *-
MY_SETTINGS = 'production'
MY_DB = 'productionDB'
from my_package import settings
print settings.MY_DB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment