使用“-m venv --upgrade”在虚拟环境中升级 python
我有多个由 pyenv 管理的 python 版本。我想使用“-upgrade”选项将我的一个虚拟环境从 3.7.13 升级到 3.10.3:
>deactivate
>pyenv local 3.10.3
>python3 -m venv --upgrade .venv
>. .venv/bin/activate
> python -V
Python 3.7.13
我希望“-upgrade”会将 python 版本更改为 3.10.3,但它并没有保留为 3.7 .13
我知道放弃并重新创建虚拟环境可能会更容易,但我真的想了解“升级”应该如何工作
I have multiple python versions managed by pyenv. I want to upgrade one of my virtual environments from 3.7.13 to 3.10.3 with the ‘—upgrade’ option as:
>deactivate
>pyenv local 3.10.3
>python3 -m venv --upgrade .venv
>. .venv/bin/activate
> python -V
Python 3.7.13
I expect the '—upgrade' would change the python version to 3.10.3 but it did not it stayed with 3.7.13
I understand it may be easier just discard and recreate the virtual environment, but I really want to learn how '—upgrade' should work
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您阅读
venv
模块的官方文档,那么 --upgrade 选项的描述非常具体:“...假设 Python 已就地升级。”我认为这意味着它必须与您最初创建虚拟环境时使用的 Python 安装相同,才能使--upgrade
标志发挥作用。 pyenv 安装的每个版本的 Python 都是单独安装的,因此我不希望--upgrade
标志在这种情况下起作用。话虽这么说,据我所知,
venv
只是安装几个基本脚本和配置文件,以及一些符号链接。venv
模块的源代码看起来相当简单,--upgrade
开关所做的就是跳过安装脚本。我认为您可以通过更改一些符号链接并更改一些目录名称来手动“破解”此方法。然而,这不是venv
应该如何使用。所以,是的,为了避免痛苦,放弃旧的虚拟环境并构建一个新的环境。
If you read the official documentation of the
venv
module, then the description of the --upgrade option is very specific: "... assuming Python has been upgraded in-place." I think this implies that it has to be the same Python installation that you originally created the virtual environment with, for the--upgrade
flag to work. Each version of Python installed by pyenv is installed separately, so I wouldn't expect the--upgrade
flag to work in this case.That being said, as far as I know,
venv
does little more than installing a couple of basic scripts and configuration files, and some bunch of symbolic links. The source code of thevenv
module seems fairly straightforward, and all that the--upgrade
switch does is skip the setup scripts. I think you could manually "hack" your way through this by changing some symbolic links and changing some directory names here and there. However, it's not howvenv
should be used.So, yeah, save yourself the misery, and discard the old virtual environment and just build a new one.