【问题标题】:easy_install clobbers my easy-install.pth and drops the python-commands in the beginning and end of the fileeasy_install 破坏了我的 easy-install.pth 并在文件的开头和结尾删除了 python-commands
【发布时间】:2018-12-01 22:19:17
【问题描述】:

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

那么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 会从easy-install.pth 中删除那些python 命令?

标签: python-2.7 installation setuptools easy-install


【解决方案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]# 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-30
    • 2012-12-18
    • 2012-12-18
    • 1970-01-01
    • 2023-03-03
    • 2015-11-27
    • 1970-01-01
    相关资源
    最近更新 更多