gpt4 book ai didi

python - mod_wsgi 找不到我的 python 包?

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

我在 Apache/mod_wsgi 中加载的 django 应用程序无法找到我的外部包,即使我在 sys.path 中指定了它?

设置如下:

a) 外部包在/var/www/site/my/python/config

b) django 网站(带有 settings.py 文件)位于/var/www/site/domain

c) wsgi配置文件是:

import os, sys
sys.stdout = sys.stderr

# Add the virtual Python environment site-packages directory to the path
import site
site.addsitedir('/var/www/virtualenv/lib/python2.6/site-packages')

# Avoid ``[Errno 13] Permission denied: '/var/www/.python-eggs'`` messages
os.environ['PYTHON_EGG_CACHE'] = '/var/www/egg-cache'

# Add project directory to PYTHONPATH
sys.path.append('/var/www/site') # <---- THIS SHOULD FIND EXTERNAL PACKAGES?
os.environ['DJANGO_SETTINGS_MODULE'] = 'domain.settings'

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

d) settings.py 文件以以下内容开头:

print "prior to import"
from my.python.config import ConfigParser # <---- THIS EXIST AS INDICATED IN a), but fails!
print after to import"

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
# ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

....

我得到的错误是:

prior to import
mod_wsgi (pid=3465): Exception occurred processing WSGI script '/var/www/mod_wsgi/django.wsgi'.
Traceback (most recent call last):
File "/var/www/virtualenv/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
self.load_middleware()
File "/var/www/virtualenv/lib/python2.6/site-packages/django/core/handlers/base.py", line 39, in load_middleware
for middleware_path in settings.MIDDLEWARE_CLASSES:
File "/var/www/virtualenv/lib/python2.6/site-packages/django/utils/functional.py", line 276, in __getattr__
self._setup()
File "/var/www/virtualenv/lib/python2.6/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/var/www/virtualenv/lib/python2.6/site-packages/django/conf/__init__.py", line 89, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'domain.settings' (Is it on sys.path?): No module named python.config

您可以看到 settings.py 文件在打印“导入前”时尝试加载,但代码在导入“my.python.config.ConfigParser”时失败。它似乎将它检测为 python.config 而不是?

有什么线索吗?

我整天都在撕头发。然而,一切都在 eclipse 环境中工作,并在设置 python 路径和 django 环境变量后直接调用“django-admin.py runserver 80”。我还验证了 python 路径在 sys.path 中。

我没有想法,所以任何帮助将不胜感激!

最佳答案

在settings.py开头添加:

import my
print my.__file__

验证打印出的路径是否针对您期望的“我的”包。

换句话说,它可能会选择一个不同的“我的”包,因为这样的包存在于多个位置并且 sys.path 的顺序是错误的。

还要确保你去阅读:

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

其中讨论了 sys.path 排序问题。

关于python - mod_wsgi 找不到我的 python 包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8768255/

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