When I run pip freeze I see (among other expected packages) pkg-resources==0.0.0. I have seen a few posts mentioning this package (including this one), but none explaining what it is, or why it is included in the output of pip freeze. The main reason I am wondering is out of curiosity, but also, it seems to break things in some cases when trying to install packages with a requirements.txt file generated with pip freeze that includes the pkg-resources==0.0.0 line (for example when Travis CI tries to install dependencies through pip and finds this line).

什么是pkg-resources,是否可以从requirements.txt中删除这一行?

更新:

我发现这一行似乎只存在于输出pip冻结时,我在一个virtualenv。我仍然不确定它是什么或它做什么,但我将进一步调查,知道它可能与virtualenv有关。


当前回答

在这个链接中找到答案:https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1635463

作者:路易斯·布沙尔(Louis Bouchard)于2019年11月16日写道:

这对我很管用。但是Iḿ不是专家,所以,如果有人更好地理解它,如果能解释它就太好了。

你好,

不管怎样,问题来自于virtualenv的去编码器版本,它使用了pkg_resource的去编码器版本,在创建时添加到virtualenv中:

$ virtualenv .
Running virtualenv with interpreter /usr/bin/python2
New python executable in /home/caribou/git/quividi/test/bin/python2
Also creating executable in /home/caribou/git/quividi/test/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
$ pip freeze
pkg-resources==0.0.0

使用pip安装版本的virtualenv可以是一个可行的解决方案:

$ sudo apt -y purge python3-virtualenv virtualenv tox
$ pip install virtualenv
$ virtualenv .
pip install virtualenv
Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/c5/97/00dd42a0fc41e9016b23f07ec7f657f636cb672fad9cf72b80f8f65c6a46/virtualenv-16.7.7-py2.py3-none-any.whl (3.4MB)
    100% |████████████████████████████████| 3.4MB 351kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.7
$ virtualenv .
New python executable in /home/caribou/git/quividi/test/bin/python
Installing setuptools, pip, wheel...
done.
$ source bin/activate
$ pip freeze
$

其他回答

至于你的问题“是否可以删除这条线?”:

我在ubuntu 16.04上开发时遇到了同样的问题。当在debian 8.5上运行“pip install -r requirements.txt”进行部署时,pip会抱怨pkg-resources“未找到”,但有一个全局包安装了“python-pkg-resources”,因此依赖关系应该得到满足。在ubuntu上也是一样:这个包也存在于那里。

正如这里所说的,它似乎是一些“隐式安装包”。

所以:如果你在安装了python- ppg -resources的Debian/Ubuntu上,删除这一行应该是安全的。我这样做了,一切都很好。然而,由于我不是这方面的专家,您应该记住,在另一台机器上部署时,这可能会导致并发症。

根据https://github.com/pypa/pip/issues/4022的说法,这是由于Ubuntu向pip提供了错误的元数据而导致的错误。所以,这种行为似乎没有一个很好的理由。我提交了Ubuntu的后续bug。https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1635463

为了备份之前的答案,从requirements.txt中删除这一行应该是安全的。下面是一个Make文件节的例子,它可以安全地冻结你的包列表(放入你的Makefile并运行Make freeze):

freeze:
    pip freeze | grep -v "pkg-resources" > requirements.txt

编辑2022年7月6日

我被告知包的名称因所使用的系统而异(pkg-resources vs pkg_resources)。关于不同版本的Debian/Ubuntu在使用上的差异,请参阅附在这个答案后面的评论。由于pkg-resources是在本文发布时(大约6年前)所讨论的系统的历史上正确的包名,因此在本答案中它将保持不变。

在这个链接中找到答案:https://bugs.launchpad.net/ubuntu/+source/python-pip/+bug/1635463

作者:路易斯·布沙尔(Louis Bouchard)于2019年11月16日写道:

这对我很管用。但是Iḿ不是专家,所以,如果有人更好地理解它,如果能解释它就太好了。

你好,

不管怎样,问题来自于virtualenv的去编码器版本,它使用了pkg_resource的去编码器版本,在创建时添加到virtualenv中:

$ virtualenv .
Running virtualenv with interpreter /usr/bin/python2
New python executable in /home/caribou/git/quividi/test/bin/python2
Also creating executable in /home/caribou/git/quividi/test/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
$ pip freeze
pkg-resources==0.0.0

使用pip安装版本的virtualenv可以是一个可行的解决方案:

$ sudo apt -y purge python3-virtualenv virtualenv tox
$ pip install virtualenv
$ virtualenv .
pip install virtualenv
Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/c5/97/00dd42a0fc41e9016b23f07ec7f657f636cb672fad9cf72b80f8f65c6a46/virtualenv-16.7.7-py2.py3-none-any.whl (3.4MB)
    100% |████████████████████████████████| 3.4MB 351kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.7
$ virtualenv .
New python executable in /home/caribou/git/quividi/test/bin/python
Installing setuptools, pip, wheel...
done.
$ source bin/activate
$ pip freeze
$