Django 项目(apache、mod_wsgi)无法导入命名空间包

发布于 2024-10-01 22:48:25 字数 1846 浏览 4 评论 0 原文

当使用 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?

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

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

发布评论

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

评论(1

埖埖迣鎅 2024-10-08 22:48:25

经过更多查看后,我在mod_wsgi 文档中找到了答案:

但是,作为附加步骤,将修改说明中描述的 WSGI 脚本文件,以将应用程序的虚拟环境覆盖在基线环境之上。这可以通过在 WSGI 脚本文件的开头添加以下内容来完成:

导入站点
site.addsitedir('/usr/local/pythonenv/PYLONS-1/lib/python2.5/site-packages')

请注意,在这种情况下,需要指定虚拟环境的“site-packages”目录的完整路径,而不仅仅是虚拟环境的根目录。

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

site.addsitedir 调用添加到我的 wsgi 脚本中(而不是像我一直在做的那样附加到 sys.path)解决了所有问题。

After looking some more, I discovered the answer in the docs for mod_wsgi:

As an additional step however, the WSGI script file described in the instructions would be modified to overlay the virtual environment for the application on top of the baseline environment. This would be done by adding at the very start of the WSGI script file the following:

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

Note that in this case the full path to the 'site-packages' directory for the virtual environment needs to be specified and not just the root of the virtual environment.

Using 'site.addsitedir()' is a bit different to simply adding the directory to 'sys.path' as the function will open up any '.pth' files located in the directory and process them. This is necessary to ensure that any special directories related to Python eggs are automatically added to 'sys.path'.

Adding the site.addsitedir call to my wsgi script (in place of appending to sys.path, as I had been doing) cleared up all the issues.

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