使用wsgi和virtualenv时django错误:无法导入设置

时间:2012-01-08 03:05:58

标签: python django virtualenv wsgi

关于wsgi没有看到Django settings.py文件的问题,我已经阅读了相当多的帖子,我能得出的最好结论是我的Django项目不在我的PYTHONPATH中。

复杂的是我在系统级运行旧版本的Django,但是这个项目需要在virtualenv中运行。

我在/ usr / local / pythonenv / election设置了virtualenv,我的项目位于/ usr / local / pythonenv / election / src / dev / election

我的.wsgi文件位于/ usr / local / pythonenv / election / src / dev / election / config /

我可以使用$ python manage.py runserver

运行Django服务器

但是当使用apache和mod_wsgi时,我收到错误:

ImportError: Could not import settings 'election.settings' (Is it on sys.path? Does it have syntax errors?): No module named election.settings

这是我的.wsgi文件:

import os
import sys
import site

sys.stdout = sys.stderr

HERE = env_root = os.path.abspath(os.path.dirname(__file__))
found = False
while env_root != '/':
    env_root = os.path.abspath(os.path.dirname(env_root))
    if os.path.exists(os.path.join(env_root, 'bin', 'activate')):
        found = True
        break
assert found, "didn't find a virtualenv in any parent of %s" % HERE

sitepackages_root = os.path.join(env_root, 'lib')
assert os.path.exists(sitepackages_root), "no such dir %s" % sitepackages_root
for d in os.listdir(sitepackages_root):
    if d.startswith('python'):
        site.addsitedir(os.path.join(sitepackages_root, d, 'site-packages'))
        break
else:
    raise RuntimeError("Could not find any site-packages to add in %r" % env_root)

os.environ['DJANGO_SETTINGS_MODULE'] = 'election.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp/election-python-eggs'

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

这是追溯:

[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]   File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py", line 33, in load_middleware
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]   File "/usr/lib/pymodules/python2.6/django/utils/functional.py", line 269, in __getattr__
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]     self._setup()
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]   File "/usr/lib/pymodules/python2.6/django/conf/__init__.py", line 40, in _setup
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]     self._wrapped = Settings(settings_module)
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]   File "/usr/lib/pymodules/python2.6/django/conf/__init__.py", line 75, in __init__
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122]     raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
[Sat Jan 07 21:54:19 2012] [error] [client 192.168.150.122] ImportError: Could not import settings 'election.settings' (Is it on sys.path? Does it have syntax errors?): No module named election.settings

1 个答案:

答案 0 :(得分:3)

你在哪里将'/ usr / local / pythonenv / election / src / dev'添加到sys.path?

您还使WSGI文件的其余部分变得比它需要的更复杂。

确保你去看:

http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations

同时阅读:

http://code.google.com/p/modwsgi/wiki/VirtualEnvironments

并使用activate_this方法激活演示文稿中提到的虚拟环境。