virtualenv 上的 django+mod_wsgi 不起作用

发布于 2024-08-29 11:10:24 字数 2582 浏览 6 评论 0原文

我刚刚在 virtualenv 上完成了 django 应用程序的设置,使用 Fabric 脚本部署顺利,但现在 .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

I've just finished setting up a django app on virtualenv, deployment went smoothly using a fabric script, but now the .wsgi is not working, I've tried every variation on the internet but no luck. My .wsgi file is:

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()

I keep getting the same error:

[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

my virtual environment is on /home/user/webapps/kcdfweb
my app is /home/user/webapps/kcdf.web/releases/current/project_name
my wsgi file home/user/webapps/kcdf.web/releases/current/project_name/apache/project_name.wsgi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

追风人 2024-09-05 11:10:24

我建议您查看文档,了解如何将 Virtualenv 与 mod_wsgi 结合使用。他们提供了一些连接虚拟环境的替代方法,这可能更适合您。

I'd recommend that you look at the docs for using Virtualenv with mod_wsgi. They offer a few alternative approaches for hooking into your virtualenv which might work better for you.

怪我入戏太深 2024-09-05 11:10:24

您需要添加距 wsgi 文件向上两个目录,因此

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

您应该添加

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

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

You need to add the directory that is two up from your wsgi file, so instead of:

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

you should have

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

...as your wsgi file is in a directory called apache, under your project folder.

九命猫 2024-09-05 11:10:24

如果您使用的是 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))

If you're using a virtualenv, you'll need to activate it within the WSGI script to set the paths up correctly.

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))
多情出卖 2024-09-05 11:10:24

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

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

Do you have an __init__.py file in your "kcdf" directory? Without that your settings file cannot be imported.

Also, you should be calling site.addsitedir() on the virtualenv's site-packages directory, if you expect to be able to import stuff from the virtualenv. See the mod_wsgi docs for details. Though if it can't even import your settings, I don't think this is your current problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文