Python如何跟踪用鸡蛋安装的模块?

Cla*_*diu 12 python installation module egg easy-install

如果我有一个模块,fooLib/site-packages,我可以import foo,它会工作.然而,当我从鸡蛋中安装东西时,我得到的东西就像blah-4.0.1-py2.7-win32.egg文件夹一样,里面有模块内容,但我仍然只需要做import foo,而不是更复杂.Python如何跟踪鸡蛋?它不仅仅是dirname匹配,就好像我将该文件夹放入Python安装而不通过dist-utils,它找不到模块.

更清楚:我刚刚安装了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
>>>
Run Code Online (Sandbox Code Playgroud)

我创建了一个"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
>>>
Run Code Online (Sandbox Code Playgroud)

但这确实如此:

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
>>>
Run Code Online (Sandbox Code Playgroud)

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

Ned*_*ily 19

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

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

easy_install大量使用这个Python功能.当您使用easy_install添加或更新分发时,它会修改easy-install.pth以添加egg目录或zip文件.通过这种方式,easy_install保持对模块搜索顺序的控制,并确保其安装的蛋在搜索顺序的早期出现.以下是一个示例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)
Run Code Online (Sandbox Code Playgroud)

正如你在这里看到的那样,如果你检查一下代码setuptools,你会发现引导自己然后覆盖它的轨道可能会使调试问题site.py和解释器启动有点兴趣.(这是一些开发人员不喜欢使用它的原因之一.)

如果使用-m参数easy_install来将分发安装为多版本,easy-install.pth则不会添加或删除该条目(如果已存在).这就是easy_install文档告诉您-m在删除已安装的鸡蛋之前使用的原因.