【问题标题】:Python update - how to update `py` to point to new version?Python更新-如何更新`py`以指向新版本?
【发布时间】:2019-10-25 16:02:47
【问题描述】:

我在 v3.7 上开始学习 Python,现在 v3.8 已经发布了。

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

如何更新py

如果我打开一个新的 Command WindowGit Bash 窗口,python --version 已更新,但 py --version 未更新,我如何将 py 更新为指向最新版本?

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

他们应该继续指向他们构建时使用的 python 版本 - 当 venv 处于活动状态时,pythonpy 都是如此吗?


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

【问题讨论】:

  • 什么是py?这是别名吗?
  • py 是 Python3 中包含的一个命令,允许您调用“默认”python 安装。但是我很难找到有关它的任何文档。在另一个问答中提到它指向默认值,但没有指向文档的链接或提及如何更新默认安装。

标签: python upgrade python-venv python-py


【解决方案1】:

搜索了一段时间后,我偶然发现了py更新部分的答案,至少。

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

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

我能够通过将其更新为 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 目录。

注意:更新环境变量后,您将需要重新启动一些程序,而其他程序可能需要注销或完全重新启动(Windows 上的 VS Code 也是如此)。 em>


更多关于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,等等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多