Skip to content

Instantly share code, notes, and snippets.

@drinks
Created April 20, 2012 19:24
Show Gist options
  • Save drinks/2431175 to your computer and use it in GitHub Desktop.
Save drinks/2431175 to your computer and use it in GitHub Desktop.
Heroku-compatible local settings
# Define any global settings here
FOO = 'bar'
# Import local settings or from os.environ
try:
from myapp.local_settings import *
except ImportError:
import imp
import os
import sys
try:
with open(os.path.join(os.path.dirname(__file__), 'local_settings.example.py'), 'rb') as fp:
for key in imp.load_module(
'example_settings', fp, 'local_settings.example.py',
('.py', 'rb', imp.PY_SOURCE)
).__dict__.keys():
if os.environ.get(key) is not None:
setattr(sys.modules[__name__], key, os.environ.get(key))
except Exception, e:
raise ImportError('Got %s trying to initialize settings.' % e)
finally:
del sys.modules[__name__].imp
del sys.modules[__name__].os
del sys.modules[__name__].key
del sys.modules[__name__].fp
del sys.modules[__name__].sys
@drinks
Copy link
Author

drinks commented Apr 20, 2012

Assumes you have local_settings.example.py in the same directory with a bunch of empty settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment