首页 > 解决方案 > 自 8 月中旬以来创建的 Python 虚拟环境为 `python3 -m pip freeze` 引发了 `AssertionError`

问题描述

在 MacOS 10.15.7 (Python 3.8.6) 和 Ubuntu 20.04 (Python 3.8.5) 上,我为我正在编写的各种 Python 脚本安装了各种虚拟环境(全部在 Git 和 Github 上)。项目pyproject.toml用于与flit. 我使用脚本中使用的模块创建并填充了环境,如下所示(其中 $VENVS 是我保留虚拟环境的目录):

$ python3 -m venv $VENVS/csvenv
$ source $VENVS/csvenv/bin/activate
(csvenv) $ python3 -m pip install --upgrade flit
(csvenv) $ flit install -s      # to install script as "editable"

但是,在两个平台上python3 -m pip freeze,当在虚拟环境中执行时,命令会得到不同的结果:

ERROR: Exception:
Traceback (most recent call last):
  File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 228, in _main
    status = self.run(options, args)
  File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/commands/freeze.py", line 101, in run
    for line in freeze(**freeze_kwargs):
  File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/operations/freeze.py", line 67, in freeze
    req = FrozenRequirement.from_dist(dist)
  File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/operations/freeze.py", line 257, in from_dist
    req = direct_url_as_pep440_direct_reference(
  File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/utils/direct_url_helpers.py", line 49, in direct_url_as_pep440_direct_reference
    assert not direct_url.info.editable
AssertionError

我的猜测是,这取决于创建环境的时间,这既基于目录时间戳,也基于对 Readthedocs 上的构建的检查,Readthedocs 会在推送存储库时重新构建文档。

在 8 月 17 日之前创建环境的项目的文档继续不间断地重建,而具有较新环境的项目的文档自 8 月以来一直未能构建。在 8 月 17 日的提交中,requirements.txt添加了一个文件。

如果我恢复它requirements.txt,RTD 构建将失败并显示以下消息:

ERROR: Could not find a version that satisfies the requirement csv2shex==0.2 (from -r docs/requirements.txt (line 16)) (from versions: none)
ERROR: No matching distribution found for csv2shex==0.2 (from -r docs/requirements.txt (line 16))

如果我退出虚拟环境deactivate,执行 python3 -m pip freeze >requirements.txt(在虚拟环境之外工作)并推送,Readthedocs 无法构建并出现相同的错误。

pip freeze >requirements.txt当我使用 7 月创建的虚拟环境时确实有效。但是,当我在 Ubuntu 上克隆该项目并重新创建虚拟环境(如上)时,pip freeze会引发异常。如果我在 MacOS 上创建并填充新的虚拟环境,它也会失败。但是,如果我从 7 月开始在虚拟环境中创建一个新requirements.txt文件并推送,则文档会构建。

在 pip 的direct_url_helpers.py的第 49 行引发了异常,其中包括以下注释:

    # pip should never reach this point for editables, since
    # pip freeze inspects the editable project location to produce
    # the requirement string
    assert not direct_url.info.editable

我已经研究了很多小时。对pip freeze, AssertionError,editable project location等的搜索一无所获。Stackoverflow 搜索“pip freeze assertionerror”仅产生 12 个结果,除了 2015 年或更早的一个结果,以及一个 2019 年关于涉及 Docker 的问题的结果。

总而言之,在我看来:

但是,有些事情并没有加起来:

有谁能解释一下吗?这是python3 -m venv,flit还是其他问题?我是否至少正确地怀疑 RTD 构建的失败与失败有关pip freeze

标签: pythonpipvirtual-environmentflit

解决方案


的失败pip freeze确实是 Pip 2.2 中的一个错误。这个错误已经为即将到来的 Pip 2.3 修复了。


推荐阅读