[英]How Should I Set Default Python Version In Windows?
我在 Windows 7 上安装了Python 2.6
和Python 3.1
并设置了环境变量: path = d:\python2.6
。
当我在cmd
中运行python
时,它显示 python 版本 2.6,这就是我想要的!
但是,当我在bat文件中编写脚本并运行它时,显示的python版本是3.1。
import sys
print (sys.version)
这里发生了什么?
如果您同时安装了这两个版本。
转到This PC -> Right-click -> Click on Properties -> Advanced System Settings.
您将看到System Properties
。 从这里导航到"Advanced"
选项卡 -> 单击Environment Variables
。
您将看到用户变量的上半部分和System variables
的下半部分。
检查System Variables
并双击Path
(以编辑路径)。
检查 Python 的路径(您希望运行的是 Python 2.x 或 3.x)并将其移动到路径列表的顶部。
重新启动命令提示符,现在当您检查 Python 版本时,它应该正确显示所需的版本。
Python 安装程序会安装适用于 Windows 的 Python Launcher 。 该程序 ( py.exe
) 与 Python 文件扩展名相关联,并查找“shebang”注释以指定要运行的 Python 版本。 这允许许多版本的 Python 共存,并允许 Python 脚本在需要时明确指定要使用的版本。 如果未指定,则默认使用当前架构(x86 或 x64)的最新 Python 版本。 可以通过py.ini
文件或PY_PYTHON
环境变量自定义此默认值。 有关更多详细信息,请参阅文档。
较新版本的 Python 更新启动器。 最新版本有一个py -0
选项来列出已安装的 Python 并指示当前的默认值。
以下是如何从控制台检查启动器是否已正确注册:
C:\>assoc .py
.py=Python.File
C:\>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*
上面, .py
文件与Python.File
类型相关联。 Python.File
的命令行是 Python Launcher,它安装在 Windows 目录中,因为它总是在 PATH 中。
要使关联正常工作,请从命令行使用script.py
运行脚本,而不是“python script.py”,否则将运行python
而不是py
。 事实上,最好从 PATH 中删除 Python 目录,这样“python”就不会运行任何东西并强制使用py
。
py.exe
也可以使用开关运行以强制使用 Python 版本:
py -3 script.py # select latest Python 3.X version to be used.
py -3.6 script.py # select version 3.6 specifically.
py -3.9-32 script.py # select version 3.9 32-bit specifically.
py -0 # list installed Python versions (latest PyLauncher).
此外,将.py;.pyw;.pyc;.pyo
到PATHEXT
环境变量,然后命令行可以只是没有扩展名的script
。
运行 'py' 命令将告诉您运行的版本。 如果您当前运行的是 3.x 并且需要切换到 2.x,则需要使用开关“-2”
py -2
如果您需要从 python 2.x 切换到 python 3.x,则必须使用“-3”开关
py -3
如果您希望将 Python 3.x 作为默认版本,则需要创建环境变量“PY_PYTHON”并将其值设置为 3。
;
; This is an example of how a Python Launcher .ini file is structured.
; If you want to use it, copy it to py.ini and make your changes there,
; after removing this header comment.
; This file will be removed on launcher uninstallation and overwritten
; when the launcher is installed or upgraded, so don't edit this file
; as your changes will be lost.
;
[defaults]
; Uncomment out the following line to have Python 3 be the default.
;python=3
[commands]
; Put in any customised commands you want here, in the format
; that's shown in the example line. You only need quotes around the
; executable if the path has spaces in it.
;
; You can then use e.g. #!myprog as your shebang line in scripts, and
; the launcher would invoke e.g.
;
; "c:\Program Files\MyCustom.exe" -a -b -c myscript.py
;
;myprog="c:\Program Files\MyCustom.exe" -a -b -c
因此,在我的系统上,我在c:\\windows\\
下创建了一个py.ini
文件,其中 py.exe 存在,内容如下:
[defaults]
python=3
现在,当您双击 .py 文件时,它将以新的默认版本运行。 现在我只使用 Shebang #! python2
#! python2
在我的旧脚本上。
如果你是 Windows 用户并且你有 Python 3.3 或更高版本,你应该在你的机器上安装Python Launcher for Windows ,这是用于启动所有 python 脚本的推荐方式(无论脚本需要什么 python 版本) )。
作为用户
从命令行运行脚本时,始终键入py
而不是python
。
使用C:\\Windows\\py.exe
设置“打开方式...”资源管理器默认程序关联
将命令行文件扩展名关联设置为使用适用于 Windows 的 Python 启动器(这将使键入py
可选):
ftype Python.File="C:\\windows\\py.exe" "%L" %*
ftype Python.NoConFile="C:\\Windows\\pyw.exe" "%L" %*
通过设置PY_PYTHON
环境变量(例如PY_PYTHON=3.7)
设置您的首选默认版本。 您可以通过键入py
来查看默认的 Python 版本。 您还可以设置PY_PYTHON3
或PY_PYTHON2
来指定默认的 python 3 和 python 2 版本(如果有多个)。
如果需要运行特定版本的python,可以使用py -Mm
(其中M
是主要版本, m
是次要版本)。 例如, py -3
将运行任何已安装的 python 3 版本。
使用py -0
列出已安装的 python 版本。
作为编剧
在脚本顶部包含一个 shebang 行,指示所需的 python 的主要版本号。 如果脚本与任何其他次要版本不兼容,请包括次要版本号。 例如:
#!/usr/bin/env python3
您也可以使用 shebang 行来表示虚拟环境(请参阅下面的 PEP 486)。
也可以看看
HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\python.exe\\default
.py
文件的默认程序设置为python.exe
这对我有用:
去
Control Panel\System and Security\System
选择
Advanced system settings from the left panel
from Advanced tab click on Environment Variables
在系统变量部分搜索(如果不存在则创建)
PYTHONPATH
并设置
C:\Python27\;C:\Python27\Scripts;
或您想要的版本
您需要重新启动 CMD。
如果它仍然不起作用,您可能只想在 PATH 变量中保留您想要的版本。
现在 Python 3.3 已发布,最容易使用此处描述的 py.exe 实用程序: http ://www.python.org/dev/peps/pep-0397/
它允许您使用 UNIX 样式指令在脚本文件中指定 Python 版本。 还有用于控制运行哪个版本的 Python 的命令行和环境变量选项。
获取此实用程序的最简单方法是安装 Python 3.3 或更高版本。
上面没有任何效果,这对我有用:
ftype Python.File=C:\Path\to\python.exe "%1" %*
此命令应在以管理员身份启动的命令提示符下运行
警告:即使此命令中的路径设置为 python35,如果您安装了 python36,它也会将默认设置为 python36。 为防止出现这种情况,您可以暂时将文件夹名称从Python36
为xxPython36
,运行该命令,然后删除对 Python 36 文件夹的更改。
如果您使用的是 Windows,请使用 ASSOC 命令更改 Python 程序的默认 Python 版本。
assoc .py=<Python 3.1 directory>
检查系统当前使用的是哪一种:
python --version
将主文件夹位置(例如 C/ProgramFiles)和脚本位置(C/ProgramFiles/Scripts)添加到系统的环境变量中。 添加 3.x 版本和 2.x 版本
路径位置在环境变量中排名。 如果您想使用 Python 2.x,只需将 python 2.x 的路径放在第一位,如果您想使用 Python 3.x,只需将 3.x 放在第一位
在 Windows CMD 中使用SET
命令临时设置当前会话的默认 python。
SET PATH=C:\Program Files\Python 3.5
在我的Windows 11 OS上安装 Python 版本2.7
、 3.7
、 3.9
后,上述解决方案对我不起作用。
命令py --help
给出一些设置 python 版本的提示,例如:
用法:
[...]
如果没有给出确切的版本,使用最新版本可以被以下任何一种覆盖,(按优先顺序):
来自 %LOCALAPPDATA%\py.ini 中 py.ini 中的 [defaults]
我发现设置特定默认 Python 版本的最简单方法是在%LOCALAPPDATA%\py.ini
下创建/编辑py.ini
文件。
py.ini
的内容[defaults]
python=3.7
C:\Users\bob>py --version
Python 3.7.9
由于我的问题略有不同,以上都不适用于我,我将添加对我有用的内容。 我今天为 python 3.10 安装了新的 python 启动器,通过它安装了版本,但是命令 window 无法识别版本。 相反,它列出了我计算机上的较旧的 python3 版本。
最后,在 windows 程序列表中,我看到我有两个版本的 python 启动器。 我卸载了旧版本,现在 python 3.10 在运行py -0
时正确显示,并且是运行时选择的版本py
。
抱歉,如果这是一个菜鸟的答案,我对这一切都很陌生。
我有同样的问题并通过再次执行安装文件解决了它。 当您这样做时,python 会自动知道您之前已经安装过它,因此它会向您推荐 3 个选项。 select modify 和 select all packages you want to modify 然后在下一页中,您可以检查 python 的新版本是否已添加到您的环境变量中。 检查它然后执行修改。 我做到了,它解决了。
尝试修改 Windows 注册表中的路径 (HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment)。
警告:不要破坏注册表:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.