2010-11-13 43 views
2

当从PIP到位桶回购安装django-piston,我发现了一些奇怪的(第一缩进输出线):Django项目(阿帕奇,mod_wsgi的)不能导入命名空间的包

$ pip install hg+http://bitbucket.org/jespern/django-piston 
Downloading/unpacking hg+http://bitbucket.org/jespern/django-piston 
Cloning Mercurial repository http://bitbucket.org/jespern/django-piston to /tmp/pip-v1h8Sh-build 
Running setup.py egg_info for package from hg+http://bitbucket.org/jespern/django-piston 
Installing collected packages: django-piston 
Running setup.py install for django-piston 
    Skipping installation of [venv]/lib/python2.6/site-packages/piston/__init__.py (namespace package) 
    Installing [venv]/lib/python2.6/site-packages/django_piston-0.2.3rc1-py2.6-nspkg.pth 
Successfully installed django-piston 
Cleaning up 

皮普不会安装活塞的__init__.py,表明这是因为'活塞'在setup.py中被指定为namespace_packages之一。

此外,当我看了“django_piston-0.2.3rc1-nspkg.pth”文件中,我发现这一点,似乎是在“虚拟软件包”的尝试:

# File: [virtualenv]/lib/python2.6/site-packages/django_piston-0.2.3rc1-py2.6-nspkg.pth 
# Originally all on one line; broken apart here for readability. 

import sys,new,os; 
p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('piston',)); 
ie = os.path.exists(os.path.join(p,'__init__.py')); 
m = not ie and sys.modules.setdefault('piston',new.module('piston')); 
mp = (m or []) and m.__dict__.setdefault('__path__',[]); 
(p not in mp) and mp.append(p) 

我可以看到它在这里做什么;它基本上创建了一个“假模块”,活塞应该是,它基本上聚集了所有活塞的子模块。

这似乎对命令行工作正常(我可以从django shell导入活塞[虽然它的repr是<module 'piston' (built-in)>],而且事情似乎在runserver中很好。),但是我的项目运行在apache mod_wsgi上,会在每个页面上引发500错误,因为有“没有名为piston.handler的模块”。

我排除了python路径问题; site-packages目录位于所有尝试的路径中。我不知道为什么它会有这样的表现,有什么想法吗?

回答

2

寻找一些之后,我发现在the docs for mod_wsgi答案:

另外还有一个步骤然而,在说明中描述的WSGI脚本文件将被修改,以覆盖虚拟环境之上的应用基准环境。这将通过在WSGI脚本文件最开始的添加来完成下列操作:

import site 
site.addsitedir('/usr/local/pythonenv/PYLONS-1/lib/python2.5/site-packages') 

注意,在这种情况下,需要指定到“站点包”目录为虚拟环境中的完整路径不只是虚拟环境的根源。

使用'site.addsitedir()'与将目录简单地添加到'sys.path'有点不同,因为该函数将打开位于该目录中的任何'.pth'文件并处理它们。这对确保任何与Python鸡蛋相关的特殊目录自动添加到'sys.path'中是必要的。

添加site.addsitedir调用我的WSGI脚本(到位追加到sys.path的,因为我一直在做),清理所有问题。

相关问题