Python 更新 - 如何更新 `py` 以指向新版本?

Python update - how to update `py` to point to new version?

我从 v3.7 开始学习 Python,现在 v3.8 已经出来了。

我已经下载了 3.8.0 并将其安装到 python38 文件夹旁边的新 python38 目录中。路径已更新,现在包括 python v3.7 和 v3.8。

如何更新py

如果我打开一个新的命令WindowGitBashwindow,python --version 已更新,但未更新 py --version,如何更新 py 以指向最新版本?

关于虚拟环境还有其他注意事项吗?

它们应该保持指向 python 的版本 - 当 venv 处于活动状态时,pythonpy 都是如此吗?


注意: py 是命令行 python 启动器工具,包含在 Python 中,适用于 Windows v3 .3

搜索了一段时间后,我至少找到了 py 更新部分的答案。

py 命令是 Windows Python 发行版中包含的可执行文件,从 Python v3.3 开始。它被称为 "Python Launcher for Windows" in the official Python documentation.

关于 Python 启动器的文档非常完整,但为了回答我最初的问题,我将环境变量 PY_PYTHON 设置为 3.7 以声明默认值 Python 供 py 使用的版本。

我可以通过将此更新为 3.8 并重新启动任何终端和使用终端访问 py/python 的程序来解决问题。还有其他 Python 环境变量,所以我全部更新了它们:

更新: 我能够完全删除 PYTHONHOMEPYTHONPATH 变量,只使用 PY_PYTHON 来控制默认版本。您可以阅读有关这些变量的更多信息 in the Environment Variables section of the Python Documentation

  • PY_PYTHON设置为3.8
  • PYTHONHOME设置为我的新Python38目录
  • PYTHONPATH 添加了新的 python38python38/scripts 目录。

注意:更新环境变量后,您将需要重新启动一些程序,而其他程序可能需要注销或完全重新启动(适用于 VS Windows).

上的代码

有关 py 命令和版本的更多信息

您可以使用 py --help:

获取基本 py 命令的列表
$ py --help
Python Launcher for Windows Version 3.8.150.1013

usage:
C:\WINDOWS\py.exe [launcher-args] [python-args] script [script-args]

Launcher arguments:

-2     : Launch the latest Python 2.x version
-3     : Launch the latest Python 3.x version
-X.Y   : Launch the specified Python version
     The above all default to 64 bit if a matching 64 bit python is present.
-X.Y-32: Launch the specified 32bit Python version
-X-32  : Launch the latest 32bit Python X version
-X.Y-64: Launch the specified 64bit Python version
-X-64  : Launch the latest 64bit Python X version
-0  --list       : List the available pythons
-0p --list-paths : List with paths

上面用于启动特定版本 python 的版本字符串可以放入 PY_PYTHON 环境变量中。例如,如果您只想要最新的 Python3,则使用 3,对于特定的 Python3,使用 3.8,或者对于 32 位版本,使用3.8-32,等等