首页 > 解决方案 > 决定是否在 pip-tools 中允许不安全的包版本的正确方法是什么?

问题描述

我正在尝试使用 pip-tools 来管理 venv(如python -m venv .venv)环境。新激活的环境最初只有 pip-tools:

> pip list
Package   Version
--------- -------
Click     7.0
pip       19.3.1
pip-tools 4.2.0
six       1.13.0

我创建了一个requirements/main.in文件:

numpy
matplotlib

运行pip-compile --upgrade --build-isolation --generate-hashes --output-file requirements/main.txt requirements/main.in给了我这个警告:

# WARNING: The following packages were not pinned, but pip requires them to be
# pinned when the requirements file includes hashes. Consider using the --allow-unsafe flag.
# setuptools==41.6.0        # via kiwisolver
The generated requirements file may be rejected by pip install. See # WARNING lines for details.

如警告,pip install --upgrade -r requirements/main.txt拒绝操作:

ERROR: In --require-hashes mode, all requirements must have their versions pinned with ==. These do not:
    setuptools from https://files.pythonhosted.org/packages/d9/de/554b6310ac87c5b921bc45634b07b11394fe63bc4cb5176f5240addf18ab/setuptools-41.6.0-py2.py3-none-any.whl#sha256=3e8e8505e563631e7cb110d9ad82d135ee866b8146d5efe06e42be07a72db20a (from kiwisolver==1.1.0->-r requirements/main.txt (line 11)) 

所以现在我的困境是:我应该使用--allow-unsafe吗?这样做有什么影响?我尝试了一下,发现生成的需求文件已将其固定到该特定版本,kiwisolver(我猜是 numpy/matplotlib 的传递依赖)需要对吗?但为什么这是不安全的?

如果我理解正确,我可以尽可能长时间地保留生成的需求,但是——每当我决定更新——我可以重新运行 pip-compile 并创建一个新的需求文件,它可能会有一个没有这个限制的新kiwisolver,对吧?

pip-tools 要求我做出这个决定的原因是什么?为什么这可能不安全?有哪些示例不想使用 --allow-unsafe?

一个相关的问题是:我可以只为 setuptools 指定“--allow-unsafe”吗?它似乎是 pip-compile 的一个参数,这是一种全有或全无的方法。我可以将特定的标记为“可以固定”吗?如果出现其他情况,我想再次收到警告,以便我评估是否可以?

标签: pythonpippip-tools

解决方案


所以现在我的困境是:我应该使用--allow-unsafe吗?这样做有什么影响?

是的你应该。此选项允许您固定requirements.txt以下包:distributepipsetuptools. 如果你不介意,那就去吧!

但为什么这是不安全的?

AFAIK,这些包裹可能被认为是不安全的,原因如下:

  • 更改setuptools可能会导致与pip( distribute的旧包装器发生冲突,setuptools自 2013 年起已弃用)。
  • 更改pip可能会破坏pip-tools自身或您的系统pip

--allow-unsafe选项很可能在不久的将来被弃用,请参阅pip-toolspip问题跟踪器中的讨论。


推荐阅读