繁体   English   中英

自 8 月中旬以来创建的 Python 虚拟环境为 `python3 -m pip freeze` 引发 `AssertionError`

[英]Python virtual environments created since mid-August raise `AssertionError` for `python3 -m pip freeze`

在 MacOS 10.15.7 (Python 3.8.6) 和 Ubuntu 20.04 (Python 3.8.5) 上,我为我正在编写的各种 Python 脚本安装了各种虚拟环境(全部在 Git 和 Github 上)。 这些项目使用pyproject.tomlflit pyproject.toml使用。 我使用脚本中使用的模块创建并填充了环境,如下所示(其中 $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在虚拟环境中执行时得到不同的结果:

  • 在 8 月 17 日之前创建的环境中,它会显示已安装的模块(应该如此)。
  • 在较新的环境中,我得到:
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 上的构建检查,每当推送 repo 时,它都会重建文档。

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构建失败,同样的错误。

当我使用 7 月创建的虚拟环境时, pip freeze >requirements.txt确实有效。 但是,当我在 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 freezeAssertionErroreditable project location等方面的搜索没有结果。 Stackoverflow 搜索“pip freeze assertionerror”只产生了 12 个结果,除了一个来自 2015 年或之前的结果,还有一个来自 2019 年的关于 Docker 的问题。

总而言之,在我看来:

  • 在我的 MacOS 和 Ubuntu 系统上, pip freeze在自 8 月 17 日以来创建的虚拟环境中不起作用(使用python3 -m venv
  • 如果存在使用 8 月 17 日之前的虚拟环境构建的requirements.txt文件,Readthedocs 文档构建成功。
  • 如果存在在任何虚拟环境之外构建的requirements.txt文件,Readthedocs 文档构建失败——总是带有上面的消息,它指的是

然而,有些事情并没有加起来:

  • 我相当确定我会在虚拟环境中创建 8 月 17 日的requirements.txt文件,因为我从不使用虚拟环境之外的项目。 但是,除非pip freeze正常工作,否则怎么可能呢?

有大佬能解释一下吗? 这是python3 -m venvflit还是其他问题? 我是否至少正确地怀疑 RTD 构建的失败与pip freeze的失败有某种关系?

pip freeze失败确实是Pip 2.2 的一个bug。 即将推出的 Pip 2.3已修复此错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM