当使用 pip 从 bitbucket repo 安装 django-piston 时,我注意到一些奇怪的东西(第一行缩进输出):
$ 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
Pip 不会安装活塞的 __init__.py ,表明这是因为 'piston' 被指定为 setup 中的 namespace_packages
之一。 py
.
此外,当我查看“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 是
],而且事情似乎在 runserver 上工作得很好。),但是我的项目在 apache mod_wsgi 上运行,在每个页面上都会抛出 500 错误,因为有“没有名为piston.handler 的模块”。
我已经排除了 python 路径问题; site-packages 目录位于所有尝试的路径中。我不知道它会表现得这样的任何其他原因,有什么想法吗?
When installing django-piston from the bitbucket repo with pip, I noticed something weird (first indented line of the output):
$ 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
Pip won't install piston's __init__.py
, indicating that this is because 'piston' is specified as one of the namespace_packages
in the setup.py
.
Further, when I looked inside the "django_piston-0.2.3rc1-nspkg.pth" file, I find this, what seems to be an attempt at "virtual packages":
# 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)
I can see what it's doing here; it's basically creating a "fake module", where piston should be, which essentially aggregates all of piston's sub-modules.
This seems to work fine for command-line work (I can import piston from the django shell [though its repr is <module 'piston' (built-in)>
], and things seem to work fine from runserver.), but my project, running on apache mod_wsgi, throws a 500 error on every page, because there's "No module named piston.handler".
I've ruled out python path issues; the site-packages dir is in the path for all attempts. I don't know any other reasons why it would behave like this, any ideas?
发布评论
评论(1)
经过更多查看后,我在mod_wsgi 文档中找到了答案:
将
site.addsitedir
调用添加到我的 wsgi 脚本中(而不是像我一直在做的那样附加到sys.path
)解决了所有问题。After looking some more, I discovered the answer in the docs for mod_wsgi:
Adding the
site.addsitedir
call to my wsgi script (in place of appending tosys.path
, as I had been doing) cleared up all the issues.