Python 如何跟踪与 Egg 一起安装的模块?
如果我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用
setuptools
(或其Distribute
分支)提供的easy_install
脚本将软件包安装为 Egg,您将看到,默认情况下,它会在 Python 安装的site-packages
目录中创建一个名为easy-install.pth
的文件。 路径配置文件是Python的标准功能:easy_install
大量使用了这个 Python 功能。当您使用easy_install
添加或更新发行版时,它会修改easy-install.pth
以添加 Egg 目录或 zip 文件。通过这种方式,easy_install
保持对模块搜索顺序的控制,并确保它安装的 Egg 出现在搜索顺序的前面。以下是easy-install.pth
的内容示例:正如您在此处所看到的,如果您检查
setuptools
中的代码,您会发现它适用于某些引导自身然后掩盖其踪迹的诡计,这可能会使site.py
和解释器启动的调试问题变得有点有趣。 (这是一些开发人员不喜欢使用它的原因之一。)如果您使用
easy_install
的-m
参数将发行版安装为 multi -version,它的easy-install.pth
条目不会被添加,如果已经存在则被删除。这就是easy_install
文档告诉您的原因在删除已安装的egg之前使用-m
。If you use the
easy_install
script provided bysetuptools
(or theDistribute
fork of it) to install packages as eggs, you will see that, by default, it creates a file namedeasy-install.pth
in thesite-packages
directory of your Python installation. Path configuration files are a standard feature of Python:easy_install
makes heavy use of this Python feature. When you useeasy_install
to add or update a distribution, it modifieseasy-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 aneasy-install.pth
: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 withsite.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 ofeasy_install
to install a distribution as multi-version, theeasy-install.pth
entry for it is not added or is removed if it already exists. This is why theeasy_install
documentation tells you to use-m
before deleting an installed egg.当您运行 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 canimport haha
from within python, or just put the haha module directly in site-packages.