gpt4 book ai didi

python - 使用 wsgi 和 virtualenv : could not import settings 时出现 django 错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:37:08 24 4
gpt4 key购买 nike

我已经阅读了大量关于 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

最佳答案

你在哪里添加 '/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 方法来激活演示文稿中提到的虚拟环境。

关于python - 使用 wsgi 和 virtualenv : could not import settings 时出现 django 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8775068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com