mod_wsgi找不到我的python包?

时间:2012-01-07 08:00:45

标签: python django apache mod-wsgi

我在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。

我没有想法,所以非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

在settings.py开始时添加:

import my
print my.__file__

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

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

另外请务必阅读:

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

讨论了sys.path排序问题。

答案 1 :(得分:0)

在/ var / www / site

的所有子目录及其子项中是否有__init__.py

当你访问my.python.config时,__init__.py需要在我的目录,python目录和config目录中。

关于权限,wsgi将作为apache用户(您在wsgi.conf中指定的用户)运行。 搞定此事。