python - 自 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
,当在虚拟环境中执行时,命令会得到不同的结果:
- 在 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 上的构建的检查,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 的问题的结果。
总而言之,在我看来:
- 在我的 MacOS 和 Ubuntu 系统上,
pip freeze
在自 8 月 17 日创建的虚拟环境中不起作用(使用python3 -m venv
) requirements.txt
在存在使用早于 8 月 17 日的虚拟环境构建的文件的情况下,Readthedocs 文档构建成功。- 如果存在在任何虚拟环境之外构建的文件,Readthedocs 文档构建会失败
requirements.txt
- 始终带有上面的消息,它指的是
但是,有些事情并没有加起来:
- 我相当肯定我会
requirements.txt
在虚拟环境中创建 8 月 17 日的文件,因为我从不使用虚拟环境之外的项目。但除非有效,否则怎么可能pip freeze
呢?
有谁能解释一下吗?这是python3 -m venv
,flit
还是其他问题?我是否至少正确地怀疑 RTD 构建的失败与失败有关pip freeze
?
解决方案
的失败pip freeze
确实是 Pip 2.2 中的一个错误。这个错误已经为即将到来的 Pip 2.3 修复了。
推荐阅读
- c - C99/C11 自引用子对象初始化可能吗?
- c# - 不同时间间隔的定时器方法经常同时运行
- javascript - JavaScript 复制对象结构(不是属性值)
- python - python win32com:在 Outlook 中删除多封电子邮件
- java - Java - 为什么我会收到 UnknownFormatConversionException?
- kotlin - 如何从 Kotlin 中的 Int 创建枚举?
- r - 从R中的cforest中提取终端节点
- python - 检查列表中的对象是否具有相同的 VALUE
- c# - LINQ 查询以返回具有转换属性的原始对象的集合
- .net - 如何获取错误引用的程序集'xxxxxxxx'没有强名称