我有一个 django 应用程序(https://github.com/wjdp/nt-tickets),其 settings.py 文件位于根目录而不是 nt-tickets 子目录中。这可以正常工作,因为在 wsgi.py 和 manage.py 中,以下行已被更改以反映更改:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
我计划使用 gunicorn 进行部署,但遇到了问题。Gunicorn 忽略此行并假定设置文件将是nt_tickets.settings
. 我正在像这样开始gunicorn:
gunicorn wsgi:application
并收到此错误:
ImportError: Could not import settings 'nt_tickets.settings' (Is it on sys.path?): No module named nt_tickets.settings
我发现放在--settings "settings"
命令末尾可以解决问题,但是为什么我必须这样做呢?wsgi.py 中设置的环境变量肯定就足够了吗?这可能看起来很琐碎,我试图不重复自己。
完整跟踪:
2013-07-10 01:12:12 [29417] [ERROR] Error handling request
Traceback (most recent call last):
File "/home/will/env/nt_tickets/local/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 126, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/home/will/env/nt_tickets/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
self.load_middleware()
File "/home/will/env/nt_tickets/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 45, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/home/will/env/nt_tickets/local/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/home/will/env/nt_tickets/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
File "/home/will/env/nt_tickets/local/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'nt_tickets.settings' (Is it on sys.path?): No module named nt_tickets.settings