Windows 上用 py 启动器运行 Python

使用系统自带的 py.exe 启动器运行 Python 脚本,并在多个已安装的 Python 版本之间切换。

WindowsPython命令提示符开发低风险
运行 py -3 script.py 用 Python 3 执行脚本,运行 py -0p 可以列出所有已安装版本及其路径。

01命令

命令
$ py -3 script.py

02作用说明

Python 官方安装包会在 C:\Windows 里装一个 py.exe 启动器,所以即使 python.exe 不在 PATH 里,cmd 和 PowerShell 中也能直接用 py。版本切换在装了多个 Python 的机器上特别实用:py -3.11 script.py 指定具体版本,py -0p 列出每个版本及其安装路径。在脚本开头写 shebang 风格注释(如 #! python3)还能让启动器自动选择解释器。

03风险

风险低风险
使用条件Python installed from python.org (the py.exe launcher is included in the installer)
!注意: 如果提示 py 不是可识别的命令,说明安装时没勾选启动器组件,或需要新开一个终端窗口让 PATH 生效。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • Using Python on Windows
    Python Software Foundation · vendor-doc · 2026-09-17
    依据: Documents the py.exe launcher: py -3 runs Python 3, py -0/-0p lists installed versions with paths, and shebang lines select interpreters.

最后验证: 2026-09-18 · 分类: 开发者

06相关方案