Windows下如何设置默认Python版本?
我在 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)
这是怎么回事?
I installed Python 2.6
and Python 3.1
on Windows 7 and set environment variable: path = d:\python2.6
.
When I run python
in cmd
, it displays the python version 2.6, which is what I want!
But, when I wrote a script in a bat file and ran it, the displayed python version was 3.1.
import sys
print (sys.version)
What's going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
由于我的问题略有不同,并且上述内容都不适合我,因此我将添加对我有用的内容。我今天安装了 python 3.10 的新 python 启动器,通过它安装了版本,但命令窗口无法识别该版本。相反,它列出了我计算机上的旧 python3 版本。
最后,在Windows程序列表中,我看到我有两个版本的python启动器。我卸载了旧版本,现在 python 3.10 在运行 py -0 时正确显示,并且是运行 py 时选择的版本。
如果这是一个菜鸟答案,我很抱歉,我对这一切都很陌生。
Since my problem was slightly different and none of the above worked for me, I'll add what worked for me. I had installed the new python launcher for python 3.10 today, installed the version through it, but the command window did not recognise the version. Instead, it listed older python3 versions I had on my computer.
Finally, in the windows programs list, I saw that I had two versions of the python launcher. I uninstalled the old one, and now python 3.10 shows up correctly when running
py -0
and is the chosen version when runningpy
.Apologies if this is a noob answer, I am new to all this.
我的系统安装了 Python 3.9 和 3.12。由于我无法解决的原因,
py
启动器更愿意运行 3.9。我创建了一个
c:\Windows\py.ini
,其中包含:然后当我运行
py
时,它将运行 3.12。但是,如果我运行的 Python 程序的第一行是:或者如果我运行 py -3,那么它仍然会运行 Python 3.9。
我找到的解决方案是在 py.ini 中添加另一行:
I have a system with Python 3.9 and 3.12 installed. For reasons I couldn't work out,
py
launcher would prefer to run 3.9.I created a
c:\Windows\py.ini
that contained:Then when I ran
py
, it would run 3.12. However, if I ran a Python program that had as its first line:or if I ran
py -3
, then it would still run Python 3.9.The solution I found is to add another line to
py.ini
:在Windows CMD中使用
SET
命令临时设置当前会话的默认Python。Use
SET
command in Windows CMD to temporarily set the default python for the current session.尝试修改 Windows 注册表中的路径 (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment)。
注意:不要破坏注册表:)
Try modifying the path in the windows registry (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment).
Caveat: Don't break the registry :)
这是如果您安装了两个版本的话。
转到此电脑→右键单击→单击属性→高级系统设置。
您将看到系统属性。从这里导航到高级选项卡 ->单击环境变量。
您将看到用户变量的上半部分和系统变量的下半部分。
检查系统变量并双击路径(编辑路径)。
检查Python 的路径(您希望运行Python 2.x 或3.x)并将其移至路径列表的顶部。
重新启动命令提示符,现在当您检查Python版本时,它应该正确显示所需的版本。
This is if you have both the versions installed.
Go to This PC → Right-click → Click on Properties → Advanced System Settings.
You will see the System Properties. From here navigate to the Advanced Tab -> Click on Environment Variables.
You will see a top half for the user variables and the bottom half for System variables.
Check the System Variables and double-click on the Path (to edit the Path).
Check for the path of Python(which you wish to run i.e. Python 2.x or 3.x) and move it to the top of the Path list.
Restart the Command Prompt, and now when you check the version of Python, it should correctly display the required version.
Python 安装程序会安装适用于 Windows 的 Python 启动器。该程序 (
py.exe
) 与 Python 文件扩展名相关联,并查找“shebang”注释来指定要运行的 Python 版本。这允许多个版本的 Python 共存,并允许 Python 脚本显式指定要使用的版本(如果需要)。如果未指定,则默认使用当前架构(x86 或 x64)的最新 Python 版本。可以通过 py.ini 文件或 PY_PYTHON 环境变量自定义此默认值。有关更多详细信息,请参阅文档。较新版本的 Python 更新了启动器。最新版本有一个 py -0 选项来列出已安装的 Python 并指示当前默认值。
以下是如何从控制台检查启动器是否正确注册:
上面,
.py
文件与Python.File
类型关联。 Python.File 的命令行是 Python Launcher,它安装在 Windows 目录中,因为它始终位于 PATH 中。为了使关联正常工作,请使用
script.py
从命令行运行脚本,而不是“python script.py”,否则将运行python
而不是py
。事实上,最好从 PATH 中删除 Python 目录,这样“python”就不会运行任何东西并强制使用py
。py.exe
也可以使用开关运行以强制使用 Python 版本:此外,将
.py;.pyw;.pyc;.pyo
添加到PATHEXT< /code> 环境变量,然后命令行可以只是不带扩展名的
script
。The Python installer installs Python Launcher for Windows. This program (
py.exe
) is associated with the Python file extensions and looks for a "shebang" comment to specify the python version to run. This allows many versions of Python to co-exist and allows Python scripts to explicitly specify which version to use, if desired. If it is not specified, the default is to use the latest Python version for the current architecture (x86 or x64). This default can be customized through apy.ini
file orPY_PYTHON
environment variable. See the docs for more details.Newer versions of Python update the launcher. The latest version has a
py -0
option to list the installed Pythons and indicate the current default.Here's how to check if the launcher is registered correctly from the console:
Above,
.py
files are associated with thePython.File
type. The command line forPython.File
is the Python Launcher, which is installed in the Windows directory since it is always in the PATH.For the association to work, run scripts from the command line with
script.py
, not "python script.py", otherwisepython
will be run instead ofpy
. If fact it's best to remove Python directories from the PATH, so "python" won't run anything and enforce usingpy
.py.exe
can also be run with switches to force a Python version:Additionally, add
.py;.pyw;.pyc;.pyo
to thePATHEXT
environment variable and then the command line can just bescript
with no extension.运行“py”命令将告诉您正在运行的版本。如果您当前运行 3.x 并且需要切换到 2.x,则需要使用开关“-2”
如果您需要从 python 2.x 切换到 python 3.x,则必须使用“-3” ' switch
如果您希望将 Python 3.x 作为默认版本,那么您需要创建环境变量“PY_PYTHON”并将其值设置为 3。
Running 'py' command will tell you what version you have running. If you currently running 3.x and you need to switch to 2.x, you will need to use switch '-2'
If you need to switch from python 2.x to python 3.x you will have to use '-3' switch
If you would like to have Python 3.x as a default version, then you will need to create environment variable 'PY_PYTHON' and set it's value to 3.
如果您了解
环境变量
和名为path
的系统变量,请考虑任何较早发布的二进制文件的任何版本都将用作默认值。看下图,我有 3 个不同的 python 版本,但 python
3.8
将用作默认版本,因为它比其他两个版本更早出现。 (在提到图像的情况下,越早意味着越高!)If you know about
Environment variables
and the system variable calledpath
, consider that any version of any binary which comes sooner, will be used as default.Look at the image below, I have 3 different python versions but python
3.8
will be used as default since it came sooner than the other two. (In case of mentioned image, sooner means higher!)如果您是 Windows 用户并且拥有 Python 3.3 或更高版本,则应该在您的计算机上安装Windows 的 Python Launcher,这是启动所有 python 脚本的推荐方法(无论脚本需要什么 python 版本)。
作为用户
从命令行运行脚本时,始终键入
py
而不是python
。设置“打开方式...”资源管理器默认程序与
C:\Windows\py.exe
设置命令行文件扩展名关联以使用适用于 Windows 的 Python 启动器(这将使输入 py 可选)。在管理
cmd
终端中,运行:ftype Python.File="C:\Windows\py.exe" "%L" %*
ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*
通过设置
PY_PYTHON
环境变量(例如PY_PYTHON=3.11 )
。您可以通过输入 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
注意:(参见这个问题)如果
python3
不适合您,请确保您已从 Windows 应用商店安装了 python(例如通过winget install --id 9NRWMJP3717K
,作为 winget 包Python.Python.3.11
似乎不包含python3.exe
)。您也可以使用 shebang 行来指示虚拟环境(请参阅下面的 PEP 486)。
另请参阅
If you are a Windows user and you have a version of Python 3.3 or greater, you should have the Python Launcher for Windows installed on your machine, which is the recommended way to use for launching all python scripts (regardless of python version the script requires).
As a user
Always type
py
instead ofpython
when running a script from the command line.Setup your "Open with..." explorer default program association with
C:\Windows\py.exe
Set the command line file extension association to use the Python Launcher for Windows (this will make typing
py
optional). In an Admincmd
terminal, run:ftype Python.File="C:\Windows\py.exe" "%L" %*
ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*
Set your preferred default version by setting the
PY_PYTHON
environment variable (e.g.PY_PYTHON=3.11)
. You can see what version of python is your default by typingpy
. You can also setPY_PYTHON3
orPY_PYTHON2
to specify default python 3 and python 2 versions (if you have multiple).If you need to run a specific version of python, you can use
py -M.m
(whereM
is the major version andm
is the minor version). For example,py -3
will run any installed version of python 3.List the installed versions of python with
py -0
.As a script writer
Include a shebang line at the top of your script that indicates the major version number of python required. If the script is not compatible with any other minor version, include the minor version number as well. For example:
#!/usr/bin/env python3
Note: (see this question) If
python3
does not work for you, ensure that you've installed python from the Windows Store (e.g. viawinget install --id 9NRWMJP3717K
, as the winget packagePython.Python.3.11
does not appear to include apython3.exe
).You can use the shebang line to indicate a virtual environment as well (see PEP 486 below).
See also
请参阅此处查看原始帖子
因此,在我的系统上我在 py.exe 所在的
c:\windows\
下创建了一个py.ini
文件,其中包含以下内容:现在,当您双击 .py 文件时,它将由新的默认版本运行。现在我只使用 Shebang
#! python2 在我的旧脚本上。
See here for original post
Thus, on my system I made a
py.ini
file underc:\windows\
where py.exe exists, with the following contents:Now when you Double-click on a .py file, it will be run by the new default version. Now I'm only using the Shebang
#! python2
on my old scripts.这对我有用。
如果您想使用 python 3.6,则必须将 python3.6 移至列表顶部。
python2.7同样适用
如果您想将 2.7 作为默认版本,请确保将 python2.7 移动到列表的最顶部。
然后关闭任何 cmd 命令提示符并再次打开,它应该按预期工作。
This work for me.
If you want to use the python 3.6 you must move the python3.6 on the top of the list.
The same applies to the python2.7
If you want to have the 2.7 as default then make sure you move the python2.7 on the very top on the list.
then close any cmd command prompt and opened again, it should work as expected.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default
.py
文件的默认程序设置为python.exe
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default
.py
files topython.exe
在我的 Windows 11 操作系统2.7、
3.7
、3.9
和3.11
strong>,我在使用之前建议的解决方案时遇到了问题。然而,我发现了一种对我有用的简单方法。首先,让我们了解如何使用 py 命令设置默认的 Python 版本。运行 py --help 会提供提示,包括对位于 %LOCALAPPDATA%\py.ini 中的 py.ini 文件的引用。该文件允许我们指定默认的 Python 版本。
要设置特定的默认 Python 版本:
打开文本编辑器或在
%LOCALAPPDATA%\py.ini
中创建文件。将以下内容添加到
py.ini
文件中:现在,当您在控制台中运行 py --version 时,它应该显示指定的默认 Python 版本。
请注意以下需要考虑的其他要点:
设置默认 Python 版本的目的:设置特定的默认 Python 版本有助于确保与某些依赖项的兼容性或保持项目之间的一致性。
%LOCALAPPDATA%
的位置:%LOCALAPPDATA%
环境变量通常指的是C:\Users\;\AppData\Local
。如果该位置不存在py.ini
文件,您可以手动创建它。With Python versions
2.7
,3.7
,3.9
, and3.11
installed on my Windows 11 OS, I encountered issues with the previously suggested solutions. However, I found a straightforward method that worked for me.First, let's understand how to set the default Python version using the
py
command. Runningpy --help
provides hints, including a reference to thepy.ini
file located in%LOCALAPPDATA%\py.ini
. This file allows us to specify the default Python version.To set a specific default Python version:
Open a text editor or create a file in
%LOCALAPPDATA%\py.ini
.Add the following content to the
py.ini
file:Now, when you run
py --version
in the console, it should display the specified default Python version.Please note the following additional points to consider:
Purpose of setting the default Python version: Setting a specific default Python version can be helpful for ensuring compatibility with certain dependencies or maintaining consistency across projects.
Location of
%LOCALAPPDATA%
: The%LOCALAPPDATA%
environment variable typically refers toC:\Users\<username>\AppData\Local
. If thepy.ini
file does not exist in that location, you can create it manually.这对我有用:
转到
选择
在系统变量部分搜索(如果不存在则创建)
并设置
或您所需的版本
您需要重新启动 CMD。
如果它仍然不起作用,您可能只想在 PATH 变量中保留您想要的版本。
This worked for me:
Go to
select
In the System variables section search for (create if doesn't exist)
and set
or your desired version
You need to restart CMD.
In case it still doesn't work you might want to leave in the PATH variable only your desired version.
上面没有任何效果,这对我有用:
此命令应该在以管理员身份启动的命令提示符中运行
警告:即使此命令中的路径设置为python35,如果您安装了python36,它也会将默认设置设置为python36。为了防止这种情况,您可以暂时将文件夹名称从
Python36
更改为xxPython36
,运行命令,然后删除对 Python 36 文件夹的更改。编辑:这就是我最终所做的:我使用Python Launcher。
https://stackoverflow.com/a/68139696/3154274
Nothing above worked, this is what worked for me:
This command should be run in Command prompt launched as administrator
Warning: even if the path in this command is set to python35, if you have python36 installed it's going to set the default to python36. To prevent this, you can temporarily change the folder name from
Python36
toxxPython36
, run the command and then remove the change to the Python 36 folder.Edit: This is what I ended up doing: I use Python Launcher.
https://stackoverflow.com/a/68139696/3154274
现在 Python 3.3 已发布,最简单的方法是使用此处描述的 py.exe 实用程序:
http://www.python.org/dev/peps/pep-0397/
它允许您使用 UNIX 样式指令在脚本文件中指定 Python 版本。还有命令行和环境变量选项用于控制运行哪个版本的 Python。
获取此实用程序的最简单方法是安装 Python 3.3 或更高版本。
Now that Python 3.3 is released it is easiest to use the py.exe utility described here:
http://www.python.org/dev/peps/pep-0397/
It allows you to specify a Python version in your script file using a UNIX style directive. There are also command line and environment variable options for controlling which version of Python is run.
The easiest way to get this utility is to install Python 3.3 or later.
如果您使用的是 Windows,请使用 ASSOC 命令更改 python 程序的默认 python 版本。
If you are on Windows, use the ASSOC command to change the default python version for python programs.
检查系统当前使用的是哪一个:
将主文件夹位置(例如C/ProgramFiles)和脚本位置(C/ProgramFiles/Scripts)添加到系统的环境变量中。添加 3.x 版本和 2.x 版本
路径位置在环境变量中排名。如果你想使用Python 2.x,只需将Python 2.x的路径放在前面,如果你想使用Python 3.x,只需将3.x放在前面
这使用 python 2
Check which one the system is currently using:
Add the main folder location (e.g. C/ProgramFiles) and Scripts location (C/ProgramFiles/Scripts) to Environment Variables of the system. Add both 3.x version and 2.x version
Path location is ranked inside environment variable. If you want to use Python 2.x simply put path of python 2.x first, if you want for Python 3.x simply put 3.x first
This uses python 2
我遇到了同样的问题并通过再次执行安装文件解决了它。当你这样做时,python 会自动知道你之前已经安装过它,所以它会推荐你 3 个选项!选择“修改”并选择所有要修改的包,然后在下一页中您可以检查新版本的 python 是否已添加到您的环境变量中。检查一下然后执行修改。我这样做了并且解决了。
I had same problem and solve it by executing the installation file again. when you do that python automatically knows you have installed it before so it recommends you 3 options! select modify and select all packages you want to modify then in the next page you can check if new version of python is added to your environment variables or not. check it and then execute modification. I did and it solved.
将
PY_PYTHON
环境变量设置为2.6
(或您想要的任何版本)。重新启动终端或 cmd 并输入 py -0p 。2.6
旁边应该有一个*
表明这是现在默认的 python 版本。Set
PY_PYTHON
environment variable as2.6
(or any version you want). Restart the terminal or cmd and typepy -0p
. The2.6
should have a*
next to it indicating that's the default python version now.