![](/img/trans.png)
[英]Virtual Environments: python -m venv VS echo layout python3
[英]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.toml
与flit
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
在虚拟环境中执行时得到不同的结果:
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 freeze
、 AssertionError
、 editable project location
等方面的搜索没有结果。 Stackoverflow 搜索“pip freeze assertionerror”只产生了 12 个结果,除了一个来自 2015 年或之前的结果,还有一个来自 2019 年的关于 Docker 的问题。
总而言之,在我看来:
pip freeze
在自 8 月 17 日以来创建的虚拟环境中不起作用(使用python3 -m venv
)requirements.txt
文件,Readthedocs 文档构建成功。requirements.txt
文件,Readthedocs 文档构建失败——总是带有上面的消息,它指的是然而,有些事情并没有加起来:
requirements.txt
文件,因为我从不使用虚拟环境之外的项目。 但是,除非pip freeze
正常工作,否则怎么可能呢? 有大佬能解释一下吗? 这是python3 -m venv
、 flit
还是其他问题? 我是否至少正确地怀疑 RTD 构建的失败与pip freeze
的失败有某种关系?
pip freeze
失败确实是Pip 2.2 的一个bug。 即将推出的 Pip 2.3已修复此错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.