繁体   English   中英

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

[英]Python app import error in Django with 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

服务器成功启动,

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

但是当我导航到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"

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

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

MIDDLEWARE_CLASSES = (
    'turk.middleware.subdomain.SubdomainMiddleware',
    'turk.middleware.removewww.RemoveWWWMiddleware',
)

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

PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(1, PROJECT_ROOT+'/turk/')

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

sys.path.insert(1, PROJECT_ROOT+'/turk/')

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

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

python top/manage.py run_gunicorn

python top/manage.py runserver

工作得很好,因为根据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__)))

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

以上方法现已被gunicorn弃用。 当我尝试相同但如果失败并带有警告信息!

(venv)root @ ip-172-31-23-172:〜/ myproj #python manage.py run_gunicorn 0.0.0.0:8001

警告:不推荐使用此命令。

您现在应该使用WSGI接口运行应用程序

与您的项目一起安装。 例:

gunicorn myproject.wsgi:应用程序

请参阅https://docs.djangoproject.com/en/1.5/howto/deployment/wsgi/gunicorn/

了解更多信息。

因此,我尝试了以下命令,它对我有用。

gunicorn myproject.wsgi:application

将“gunicorn”添加到settings.py/INSTALLED_APPS并使用

python manage.py run_gunicorn 127.0.0.0:8001

(无论您喜欢什么端口)欲了解更多信息,请访问: https//pypi.python.org/pypi/gunicorn/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM