关于virtualenv的django + mod_wsgi无法正常工作

时间:2010-04-18 13:49:03

标签: django mod-wsgi virtualenv

我刚刚在virtualenv上设置了一个django应用程序,使用结构脚本顺利部署,但现在.wsgi无法正常工作,我已经尝试了互联网上的每个变种但没有运气。我的.wsgi文件是:

import os
import sys
import django.core.handlers.wsgi

# put the Django project on sys.path
root_path = os.path.abspath(os.path.dirname(__file__) + '../')
sys.path.insert(0, os.path.join(root_path, 'kcdf'))
sys.path.insert(0, root_path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'kcdf.settings'

application = django.core.handlers.wsgi.WSGIHandler()

我一直收到同样的错误:

[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159] mod_wsgi (pid=16938): Exception occurred processing WSGI script '/home/kcdfweb/webapps/kcdf.web/releases/current/kcdf/apache/kcdf.wsgi'.
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159] Traceback (most recent call last):
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 230, in __call__
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     self.load_middleware()
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 33, in load_middleware
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/utils/functional.py", line 269, in __getattr__
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     self._setup()
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 40, in _setup
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     self._wrapped = Settings(settings_module)
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]   File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 75, in __init__
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159]     raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
[Sun Apr 18 12:44:30 2010] [error] [client 41.215.123.159] ImportError: Could not import settings 'kcdf.settings' (Is it on sys.path? Does it have syntax errors?): No module named kcdf.settings

我的虚拟环境位于/ home / user / webapps / kcdfweb 我的应用是/home/user/webapps/kcdf.web/releases/current/project_name 我的wsgi文件home / user / webapps / kcdf.web / releases / current / project_name / apache / project_name.wsgi

4 个答案:

答案 0 :(得分:6)

我建议您查看docs使用带有mod_wsgi的Virtualenv。他们提供了一些替代方法来连接你的virtualenv,这可能对你有用。

答案 1 :(得分:1)

您需要添加wsgi文件中的两个目录,而不是:

root_path = os.path.abspath(os.path.dirname(__file__) + '../')
你应该

root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../', '../'))

...因为您的wsgi文件位于项目文件夹下名为apache的目录中。

答案 2 :(得分:0)

如果您使用的是virtualenv,则需要在WSGI脚本中激活它以正确设置路径。

root_path = os.path.abspath(os.path.dirname(__file__) + '../')
activate_this = os.path.join(root_path, "bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))

答案 3 :(得分:0)

您的“kcdf”目录中是否有__init__.py个文件?没有它,您的设置文件将无法导入。

此外,如果您希望能够从virtualenv导入内容,则应该在virtualenv的site-packages目录中调用site.addsitedir()。有关详细信息,请参阅the mod_wsgi docs。虽然如果它甚至无法导入你的设置,我认为这不是你当前的问题。