I'm building an application that borrows some design ideas from Django - the use of a per-project manage.py and settings.py, for example- and am trying to decip开发者_StackOverflow中文版her the magic.
When I want to refer to my Django settings, I generally import from django.settings. Obviously, though, my settings are in their own project-specific module. Any idea how this is accomplished?
An environment variable: DJANGO_SETTINGS_MODULE points to your settings module.
You can use the __import__
function: See http://docs.python.org/library/functions.html#import
And try, assuming your main app module is myapp:
myapp.settings = __import__(SETTINGS_MODULE_NAME, globals(), locals(), [], -1)
This is the dynamic equivalent of the from module import *
statement
精彩评论