easy_install 破坏了我的 easy-install.pth 并在文件的开头和结尾删除了 python-commands

easy_install clobbers my easy-install.pth and drops the python-commands in the beginning and end of the file

我的 sys.path 遇到了一个奇怪的问题;由于我要清楚地描述问题,因此 post 会有点长。

我的 /usr/lib/python2.7/site-packages/easy-install.pth 看起来像这样:

import sys; sys.__plen = len(sys.path)
./setuptools-27.2.0-py2.7.egg
./jmespath-0.9.3-py2.7.egg
./chardet-3.0.4-py2.7.egg
./certifi-2018.01.18-py2.7.egg
./urllib3-1.22-py2.7.egg
./requests-2.18.4-py2.7.egg
./docutils-0.14-py2.7.egg
./python_dateutil-2.7.5-py2.7.egg
./enum34-1.1.6-py2.7.egg
./six-1.10.0-py2.7.egg
./ipaddress-1.0.18-py2.7.egg
./asn1crypto-0.22.0-py2.7.egg
./idna-2.6-py2.7.egg
./pyOpenSSL-17.2.0-py2.7.egg
/usr/lib64/python2.7/site-packages/cryptography-2.0.3-py2.7-linux-x86_64.egg
/usr/lib64/python2.7/site-packages/cffi-1.10.0-py2.7-linux-x86_64.egg
import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)

有了这些东西对我来说很好用;具体来说,pyOpenSSL 的正确版本包含在我的 sys.path:

[root@sandbox ~]# python -c "import OpenSSL; print OpenSSL.__version__"
17.2.0

当我删除第一行和最后一行时,python 命令我的 pyOpenSSL 版本发生了变化;这意味着当我的 easy-install.pth 看起来像这样时:

./setuptools-27.2.0-py2.7.egg
./jmespath-0.9.3-py2.7.egg
./chardet-3.0.4-py2.7.egg
./certifi-2018.01.18-py2.7.egg
./urllib3-1.22-py2.7.egg
./requests-2.18.4-py2.7.egg
./docutils-0.14-py2.7.egg
./python_dateutil-2.7.5-py2.7.egg
./enum34-1.1.6-py2.7.egg
./six-1.10.0-py2.7.egg
./ipaddress-1.0.18-py2.7.egg
./asn1crypto-0.22.0-py2.7.egg
./idna-2.6-py2.7.egg
./pyOpenSSL-17.2.0-py2.7.egg
/usr/lib64/python2.7/site-packages/cryptography-2.0.3-py2.7-linux-x86_64.egg
/usr/lib64/python2.7/site-packages/cffi-1.10.0-py2.7-linux-x86_64.egg

那么pyOpenSSL版本是旧版本:

[root@sandbox ~]# python -c "import OpenSSL; print OpenSSL.__version__"
0.13.1

我认为这是因为这里存在旧版本的 pyOpenSSL /usr/lib64/python2.7/site-packages/pyOpenSSL-0.13.1-py2.7.egg-info

所以我的第一个问题:这些命令在 easy-install.pth 中做了什么,easy-install.pth 如何处理以更改我的 sys.path

接下来是当我尝试使用 easy_install(来自他们的源代码)安装新的 python 包时,它破坏了我的 easy-install.pth -- 从某种意义上说,所有包仍然是那里但没有第一行和最后一行(那些 python 命令)。

更具体地说:我正在尝试安装 idna-2.5 它确实安装成功但搞砸了 easy-install.pth

[root@sandbox idna-2.5]# easy_install .
Processing .
Writing /opt/proj/sbs-installs/COSpkgs/idna-2.5/setup.cfg
Running setup.py -q bdist_egg --dist-dir /opt/proj/sbs-installs/COSpkgs/idna-2.5/egg-dist-tmp-sLNbiE
warning: no previously-included files matching '*.pyc' found under directory 'tools'
warning: no previously-included files matching '*.pyc' found under directory 'tests'
zip_safe flag not set; analyzing archive contents...
Removing /usr/lib/python2.7/site-packages/idna-2.5-py2.7.egg
Moving idna-2.5-py2.7.egg to /usr/lib/python2.7/site-packages
Removing idna 2.6 from easy-install.pth file
Adding idna 2.5 to easy-install.pth file

Installed /usr/lib/python2.7/site-packages/idna-2.5-py2.7.egg
Processing dependencies for idna==2.5
Finished processing dependencies for idna==2.5

请注意它已卸载 idnz 2.6。在此之后我的 easy-install.pth 看起来像这样:

./setuptools-27.2.0-py2.7.egg
./jmespath-0.9.3-py2.7.egg
./chardet-3.0.4-py2.7.egg
./certifi-2018.01.18-py2.7.egg
./urllib3-1.22-py2.7.egg
./requests-2.18.4-py2.7.egg
./docutils-0.14-py2.7.egg
./python_dateutil-2.7.5-py2.7.egg
./enum34-1.1.6-py2.7.egg
./six-1.10.0-py2.7.egg
./ipaddress-1.0.18-py2.7.egg
./asn1crypto-0.22.0-py2.7.egg
./pyOpenSSL-17.2.0-py2.7.egg
/usr/lib64/python2.7/site-packages/cryptography-2.0.3-py2.7-linux-x86_64.egg
/usr/lib64/python2.7/site-packages/cffi-1.10.0-py2.7-linux-x86_64.egg
./idna-2.5-py2.7.egg

现在,当我检查 pyOpenSSL 版本时,它是旧版本(如上所述):

[root@sandbox ~]# python -c "import OpenSSL; print OpenSSL.__version__"
0.13.1

提前感谢您的帮助。

需要一些挖掘才能找到答案。为了完整性和可搜索性,我在这里发布答案:)。

easy-install.pth开头的python命令调用prelude and the one at the last line is called postlude (in setuptools代码。

正如预期的那样,easy_install 重写 PathDistributions,即 easy-install.pth,如果您的安装导致删除旧包并安装新包。

现在,如果你想包含 preludepostlude,代码需要将 SETUPTOOLS_SYS_PATH_TECHNIQUE 环境 属性 设置为 rewrite(默认值为raw)。更具体地说,以下命令解决了问题:

[root@sandbox idna-2.5]# export SETUPTOOLS_SYS_PATH_TECHNIQUE=rewrite
[root@sandbox idna-2.5]# easy_install .
Processing .
Writing /opt/proj/sbs-installs/COSpkgs/idna-2.5/setup.cfg
Running setup.py -q bdist_egg --dist-dir /opt/proj/sbs-installs/COSpkgs/idna-2.5/egg-dist-tmp-gE1V2U
warning: no previously-included files matching '*.pyc' found under directory 'tools'
warning: no previously-included files matching '*.pyc' found under directory 'tests'
zip_safe flag not set; analyzing archive contents...
Removing /usr/lib/python2.7/site-packages/idna-2.5-py2.7.egg
Moving idna-2.5-py2.7.egg to /usr/lib/python2.7/site-packages
Removing idna 2.6 from easy-install.pth file
Adding idna 2.5 to easy-install.pth file

Installed /usr/lib/python2.7/site-packages/idna-2.5-py2.7.egg
Processing dependencies for idna==2.5
Finished processing dependencies for idna==2.5
[root@sandbox idna-2.5]#