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



我的sys.path遇到了一个奇怪的问题;由于我要清楚地描述这个问题,所以这篇文章会有点长。

我的/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

则CCD_ 7版本是较旧的版本:

[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,最后一行的命令称为postlude(在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]# 

最新更新