In some of my Django apps I'm using a settings_local.py
file to override settings that are different on various environments (e.g. development, test and production). I have originally used the following code to include its contents in the settings.py
:
try:
from settings_local import *
except ImportError:
sys.stderr.write("The settings_local.py file is missing.\n")
DEBUG=False
I have recently found the execfile
function and switched to something like:
try:
execfile(path.join(PROJECT_ROOT, "settings_local.py"))
except IOError:
sys.stderr.write("The settings_local.py file is missing.\n"
DEBUG=False
Both work as intended, but I'm curious whether I'm missing any gotchas, and in general which approach is more recommended and why.
__file__
could be easily provided via arguments. The main difference that a module is imported exactly once no matter how many timesimport module
is executed (exec
executes the file every time it is called), andimport
doesn't require an explicit path to the file (so it would work inside a zip archive too). – Trichology