使用WSGI gunicorn在Django中导入Python应用程序错误

Int*_*nex 5 python django import wsgi gunicorn

我正在尝试在Heroku上部署一个带有gunicorn的Django应用程序,但我遇到了几个问题.

当我开始我的项目时,我的Django版本是1.3并且不包含标准的wsgi.py模块,所以我添加了标准的wsgi模块作为top/wsgi.py(顶部是我的项目名称,turk是我的应用程序名称,topturk是包含目录 - 保留,以便错误日志在下面有意义).

现在我跑的时候

gunicorn top.wsgi:application -b 0.0.0.0:$PORT
Run Code Online (Sandbox Code Playgroud)

服务器成功启动,

19:00:42 web.1     | started with pid 7869
19:00:42 web.1     | 2012-07-25 19:00:42 [7869] [INFO] Starting gunicorn 0.14.5
19:00:42 web.1     | 2012-07-25 19:00:42 [7869] [INFO] Listening at: http://0.0.0.0:5000 (7869)
19:00:42 web.1     | 2012-07-25 19:00:42 [7869] [INFO] Using worker: sync
19:00:42 web.1     | 2012-07-25 19:00:42 [7870] [INFO] Booting worker with pid: 7870
Run Code Online (Sandbox Code Playgroud)

但是当我导航到0.0.0.0:5000时,我收到一个内部服务器错误:

19:00:45 web.1     | 2012-07-25 17:00:45 [7870] [ERROR] Error handling request
19:00:45 web.1     | Traceback (most recent call last):
19:00:45 web.1     |   File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 102, in handle_request
19:00:45 web.1     |     respiter = self.wsgi(environ, resp.start_response)
19:00:45 web.1     |   File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
19:00:45 web.1     |     self.load_middleware()
19:00:45 web.1     |   File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
19:00:45 web.1     |     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
19:00:45 web.1     | ImproperlyConfigured: Error importing middleware turk.middleware.subdomain: "No module named turk.middleware.subdomain"
19:00:47 web.1     | 2012-07-25 17:00:47 [7870] [ERROR] Error handling request
19:00:47 web.1     | Traceback (most recent call last):
19:00:47 web.1     |   File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 102, in handle_request
19:00:47 web.1     |     respiter = self.wsgi(environ, resp.start_response)
19:00:47 web.1     |   File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 219, in __call__
19:00:47 web.1     |     self.load_middleware()
19:00:47 web.1     |   File "/Users/intenex/Dropbox/code/django/topturk/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 47, in load_middleware
19:00:47 web.1     |     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
19:00:47 web.1     | ImproperlyConfigured: Error importing middleware turk.middleware.subdomain: "No module named turk.middleware.subdomain"
Run Code Online (Sandbox Code Playgroud)

我假设这是一个python路径错误,服务器不知道如何从我的应用程序目录导入

相关的导入代码在设置中:

MIDDLEWARE_CLASSES = (
    'turk.middleware.subdomain.SubdomainMiddleware',
    'turk.middleware.removewww.RemoveWWWMiddleware',
)
Run Code Online (Sandbox Code Playgroud)

我试图通过将我的app目录插入到sys.path中来解决这个问题,就像我在settings.py文件的顶部一样,如下所示:

PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(1, PROJECT_ROOT+'/turk/')
Run Code Online (Sandbox Code Playgroud)

我已经验证过将app目录添加到路径中,但仍然没有骰子.有任何想法吗?也

sys.path.insert(1, PROJECT_ROOT+'/turk/')
Run Code Online (Sandbox Code Playgroud)

似乎是hackish并且至少将两个目录副本添加到路径中,在Django中附加到PYTHON_PATH的正确方法是什么?谢谢!

Int*_*nex 10

找出我的问题.需要将项目目录添加到Python路径,而不是app目录 - 即topturk/top而不是topturk/top/turk以便导入turk目录模块.

python top/manage.py run_gunicorn
Run Code Online (Sandbox Code Playgroud)

python top/manage.py runserver
Run Code Online (Sandbox Code Playgroud)

工作得很好,因为根据Python路径文档,调用模块的目录总是作为元素0添加到Python路径元组中 - 所以当使用top/manage.py时,topturk/top总是在Python路径中.

然而,使用heroku,Procfile位于项目的父目录中,topturk而不是topturk/top,因此当procfile命令运行时,topturk被添加到Python路径但不是topturk/top,因此也就是错误.

事后看来,弄清楚这是Django文档在本节的最后一句中引用的内容:https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/gunicorn/#running-django-in -gunicorn-as-a-generic-wsgi-application,他们说为了运行这个命令,项目必须在Python路径上.

添加问题解决了

sys.path.insert(1, os.path.dirname(os.path.realpath(__file__)))
Run Code Online (Sandbox Code Playgroud)

到settings.py或wsgi.py - 添加到settings.py,因为这似乎是其他人推荐的(http://codespatter.com/2009/04/10/how-to-add-locations-to- python-path-for-reusable-django-apps /),但不确定插入的最佳位置是什么.谁知道?