Python 如何跟踪与 Egg 一起安装的模块?

发布于 2024-10-05 00:05:56 字数 1498 浏览 4 评论 0原文

如果我在 Lib/site-packages 中有一个模块 foo,我只需 import foo 就可以了。但是,当我从 Egg​​ 安装东西时,我得到了类似 blah-4.0.1-py2.7-win32.egg 的文件夹,其中包含模块内容,但我仍然只需要 < code>import foo,没有什么更复杂的。 Python 如何跟踪鸡蛋?这不仅仅是目录名匹配,就好像我在不通过 dist-utils 的情况下将该文件夹放入 Python 安装中一样,它也找不到该模块。

更清楚地说:我刚刚安装了 zope。文件夹名称为“zope.interface-3.3.0-py2.7-win32.egg”。这是有效的:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import zope.interface
>>>

我创建了一个“blah-4.0.1-py2.7-win32.egg”文件夹,其中包含一个空模块“haha”(和__init__.py)。这行不通:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import blah.haha
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named blah.haha
>>>

但是,这行得通:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pkg_resources import require
>>> require("blah>=1.0")
[blah 4.0.1 (c:\python27\lib\site-packages\blah-4.0.1-py2.7-win32.egg)]
>>> import haha
>>>

那么我如何让它在没有 require 的情况下工作呢?

If I have a module, foo, in Lib/site-packages, I can just import foo and it will work. However, when I install stuff from eggs, I get something like blah-4.0.1-py2.7-win32.egg as a folder, with the module contents inside, yet I still only need do import foo, not anything more complicated. How does Python keep track of eggs? It is not just dirname matching as if I drop that folder into a Python installation without going through dist-utils, it does not find the module.

To be clearer: I just installed zope. The folder name is "zope.interface-3.3.0-py2.7-win32.egg". This works:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import zope.interface
>>>

I create a "blah-4.0.1-py2.7-win32.egg" folder with an empty module "haha" in it (and __init__.py). This does not work:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import blah.haha
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named blah.haha
>>>

This does, though:

Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pkg_resources import require
>>> require("blah>=1.0")
[blah 4.0.1 (c:\python27\lib\site-packages\blah-4.0.1-py2.7-win32.egg)]
>>> import haha
>>>

So how do I make it work without a require?

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

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

发布评论

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

评论(2

也只是曾经 2024-10-12 00:05:56

如果您使用 setuptools(或其 Distribute 分支)提供的 easy_install 脚本将软件包安装为 Egg,您将看到,默认情况下,它会在 Python 安装的 site-packages 目录中创建一个名为 easy-install.pth 的文件。 路径配置文件是Python的标准功能:

路径配置文件是一个文件
其名称格式为 package.pth
并存在于四个之一
上述目录;它是
内容是附加项目(每个
行)添加到 sys.path。

easy_install 大量使用了这个 Python 功能。当您使用 easy_install 添加或更新发行版时,它会修改 easy-install.pth 以添加 Egg 目录或 zip 文件。通过这种方式,easy_install 保持对模块搜索顺序的控制,并确保它安装的 Egg 出现在搜索顺序的前面。以下是 easy-install.pth 的内容示例:

import sys; sys.__plen = len(sys.path)
./appscript-0.21.1-py2.6-macosx-10.5-ppc.egg
./yolk-0.4.1-py2.6.egg
./Elixir-0.7.1-py2.6.egg
./Fabric-0.9.0-py2.6.egg
import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginse
rt',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)

正如您在此处所看到的,如果您检查 setuptools 中的代码,您会发现它适用于某些引导自身然后掩盖其踪迹的诡计,这可能会使 site.py 和解释器启动的调试问题变得有点有趣。 (这是一些开发人员不喜欢使用它的原因之一。)

如果您使用 easy_install-m 参数将发行版安装为 multi -version,它的 easy-install.pth 条目不会被添加,如果已经存在则被删除。这就是easy_install文档告诉您的原因在删除已安装的egg之前使用-m

If you use the easy_install script provided by setuptools (or the Distribute fork of it) to install packages as eggs, you will see that, by default, it creates a file named easy-install.pth in the site-packages directory of your Python installation. Path configuration files are a standard feature of Python:

A path configuration file is a file
whose name has the form package.pth
and exists in one of the four
directories mentioned above; its
contents are additional items (one per
line) to be added to sys.path.

easy_install makes heavy use of this Python feature. When you use easy_install to add or update a distribution, it modifies easy-install.pth to add the egg directory or zip file. In this way, easy_install maintains control of the module searching order and ensures that the eggs it installs appear early in the search order. Here is an example of the contents of an easy-install.pth:

import sys; sys.__plen = len(sys.path)
./appscript-0.21.1-py2.6-macosx-10.5-ppc.egg
./yolk-0.4.1-py2.6.egg
./Elixir-0.7.1-py2.6.egg
./Fabric-0.9.0-py2.6.egg
import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginse
rt',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)

As you can see here and if you examine the code in setuptools, you will find it goes to some trickery to bootstrap itself and then cover its tracks which can make debugging problems with site.py and interpreter startup a bit interesting. (That is one of the reasons that some developers are not fond of using it.)

If you use the -m parameter of easy_install to install a distribution as multi-version, the easy-install.pth entry for it is not added or is removed if it already exists. This is why the easy_install documentation tells you to use -m before deleting an installed egg.

拥抱我好吗 2024-10-12 00:05:56

当您运行 easy_install 时,它会将 Egg 复制到站点包中,并将该 Egg 的路径放入 sys.path 变量中。 (请注意,sys.path 不是您的 PATH 环境变量,它是由 PYTHONPATH 和其他环境变量构建的。因此,您使用 easy_install 安装的 .egg 文件会被放入某个环境变量中,并且 python 知道在以下情况下将其添加到 sys.path 中: python 解释器启动)。

要让 blah.haha 在您的示例中工作,请运行 easy_install blah-4.0.1-py2.7-win32.egg,然后您可以从 python 中import haha​​ ,或者直接将 haha​​ 模块放入 site-packages 中。

When you run easy_install it copies the egg into site-packages and puts the path to that egg on your sys.path variable. (Note that sys.path is not your PATH environment variable, it is constructed from PYTHONPATH and other environment variables. So the .egg file you install with easy_install gets put in some environment variable and python knows to add it to sys.path when the python interpreter starts).

To get blah.haha to work in your example, either run easy_install blah-4.0.1-py2.7-win32.egg and then you can import haha from within python, or just put the haha module directly in site-packages.

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