使用“-m venv --upgrade”在虚拟环境中升级 python

发布于 2025-01-15 09:20:14 字数 344 浏览 1 评论 0原文

我有多个由 py​​env 管理的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若言繁花未落 2025-01-22 09:20:14

如果您阅读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 the venv 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 how venv should be used.

So, yeah, save yourself the misery, and discard the old virtual environment and just build a new one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文