Created
July 7, 2016 16:13
-
-
Save dgarana/c052a3287629dd7c0b0c9d7921081e9d to your computer and use it in GitHub Desktop.
Python load dynamic settings module through environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -* 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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -* coding: utf-8 *- | |
MY_SETTINGS = 'local' | |
MY_DB = 'localDB' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -* coding: utf-8 *- | |
MY_SETTINGS = 'production' | |
MY_DB = 'productionDB' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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