Windows下如何设置默认Python版本?

发布于 2024-10-18 14:28:49 字数 273 浏览 5 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

半衾梦 2024-10-25 14:28:50

由于我的问题略有不同,并且上述内容都不适合我,因此我将添加对我有用的内容。我今天安装了 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 running py.

Apologies if this is a noob answer, I am new to all this.

薄凉少年不暖心 2024-10-25 14:28:50

我的系统安装了 Python 3.9 和 3.12。由于我无法解决的原因,py 启动器更愿意运行 3.9。

我创建了一个 c:\Windows\py.ini ,其中包含:

[defaults]
python=3.12

然后当我运行 py 时,它将运行 3.12。但是,如果我运行的 Python 程序的第一行是:

#!/usr/bin/python3

或者如果我运行 py -3,那么它仍然会运行 Python 3.9。

我找到的解决方案是在 py.ini 中添加另一行:

python3=3.12

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:

[defaults]
python=3.12

Then when I ran py, it would run 3.12. However, if I ran a Python program that had as its first line:

#!/usr/bin/python3

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:

python3=3.12
有深☉意 2024-10-25 14:28:50

在Windows CMD中使用SET命令临时设置当前会话的默认Python。

SET PATH=C:\Program Files\Python 3.5

Use SET command in Windows CMD to temporarily set the default python for the current session.

SET PATH=C:\Program Files\Python 3.5
未央 2024-10-25 14:28:50

尝试修改 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 :)

巷子口的你 2024-10-25 14:28:49

这是如果您安装了两个版本的话。

转到此电脑右键单击单击属性高级系统设置

您将看到系统属性。从这里导航到高级选项卡 ->单击环境变量

您将看到用户变量的上半部分和系统变量下半部分

检查系统变量并双击路径(编辑路径)。

检查Python 的路径(您希望运行Python 2.x 或3.x)并将其移至路径列表的顶部

重新启动命令提示符,现在当您检查Python版本时,它应该正确显示所需的版本。

This is if you have both the versions installed.

Go to This PCRight-clickClick on PropertiesAdvanced 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.

晚风撩人 2024-10-25 14:28:49

Python 安装程序会安装适用于 Windows 的 Python 启动器。该程序 (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< /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 a py.ini file or PY_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:

C:\>assoc .py
.py=Python.File

C:\>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*

Above, .py files are associated with the Python.File type. The command line for Python.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", otherwise python will be run instead of py. If fact it's best to remove Python directories from the PATH, so "python" won't run anything and enforce using py.

py.exe can also be run with switches to force a Python version:

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).

Additionally, add .py;.pyw;.pyc;.pyo to the PATHEXT environment variable and then the command line can just be script with no extension.

ゃ人海孤独症 2024-10-25 14:28:49

运行“py”命令将告诉您正在运行的版本。如果您当前运行 3.x 并且需要切换到 2.x,则需要使用开关“-2”

py -2

如果您需要从 python 2.x 切换到 python 3.x,则必须使用“-3” ' switch

py -3

如果您希望将 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'

py -2

If you need to switch from python 2.x to python 3.x you will have to use '-3' switch

py -3

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.

左耳近心 2024-10-25 14:28:49

如果您了解环境变量和名为path的系统变量,请考虑任何较早发布的二进制文件的任何版本都将用作默认值。

看下图,我有 3 个不同的 python 版本,但 python 3.8 将用作默认版本,因为它比其他两个版本更早出现。 (在提到图像的情况下,越早意味着越高!)

在此处输入图像描述

If you know about Environment variables and the system variable called path, 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!)

enter image description here

寂寞美少年 2024-10-25 14:28:49

如果您是 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_PYTHON3PY_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 of python 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 Admin cmd 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 typing py. You can also set PY_PYTHON3 or PY_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 (where M is the major version and m 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. via winget install --id 9NRWMJP3717K, as the winget package Python.Python.3.11 does not appear to include a python3.exe).

  • You can use the shebang line to indicate a virtual environment as well (see PEP 486 below).


See also

素染倾城色 2024-10-25 14:28:49

请参阅此处查看原始帖子

;
; 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

因此,在我的系统上我在 py.exe 所在的 c:\windows\ 下创建了一个 py.ini 文件,其中包含以下内容:

[defaults]
python=3

现在,当您双击 .py 文件时,它将由新的默认版本运行。现在我只使用 Shebang #! python2 在我的旧脚本上。

See here for original post

;
; 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

Thus, on my system I made a py.ini file under c:\windows\ where py.exe exists, with the following contents:

[defaults]
python=3

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.

魂归处 2024-10-25 14:28:49

这对我有用。

如果您想使用 python 3.6,则必须将 python3.6 移至列表顶部。

python2.7同样适用
如果您想将 2.7 作为默认版本,请确保将 python2.7 移动到列表的最顶部。

第 1 步

enter图片描述在这里

第 2 步

enter图片描述在这里

第3步

enter image description here

然后关闭任何 cmd 命令提示符并再次打开,它应该按预期工作。

python --版本

>>> Python 3.6

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.

step 1

enter image description here

step 2

enter image description here

step 3

enter image description here

then close any cmd command prompt and opened again, it should work as expected.

python --version

>>> Python 3.6
宣告ˉ结束 2024-10-25 14:28:49
  1. 编辑注册表项 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default
  2. 将打开 .py 文件的默认程序设置为 python.exe
  1. Edit registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default
  2. Set default program to open .py files to python.exe

在我的 Windows 11 操作系统2.7、3.73.93.11 strong>,我在使用之前建议的解决方案时遇到了问题。然而,我发现了一种对我有用的简单方法。

首先,让我们了解如何使用 py 命令设置默认的 Python 版本。运行 py --help 会提供提示,包括对位于 %LOCALAPPDATA%\py.ini 中的 py.ini 文件的引用。该文件允许我们指定默认的 Python 版本。

要设置特定的默认 Python 版本:

  1. 打开文本编辑器或在 %LOCALAPPDATA%\py.ini 中创建文件。

  2. 将以下内容添加到py.ini文件中:

[defaults]
python=3.7
  1. 保存文件。

现在,当您在控制台中运行 py --version 时,它应该显示指定的默认 Python 版本。

请注意以下需要考虑的其他要点:

  • 设置默认 Python 版本的目的:设置特定的默认 Python 版本有助于确保与某些依赖项的兼容性或保持项目之间的一致性。

  • %LOCALAPPDATA% 的位置:%LOCALAPPDATA% 环境变量通常指的是 C:\Users\;\AppData\Local。如果该位置不存在 py.ini 文件,您可以手动创建它。

With Python versions 2.7, 3.7, 3.9, and 3.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. Running py --help provides hints, including a reference to the py.ini file located in %LOCALAPPDATA%\py.ini. This file allows us to specify the default Python version.

To set a specific default Python version:

  1. Open a text editor or create a file in %LOCALAPPDATA%\py.ini.

  2. Add the following content to the py.ini file:

[defaults]
python=3.7
  1. Save the 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 to C:\Users\<username>\AppData\Local. If the py.ini file does not exist in that location, you can create it manually.

请你别敷衍 2024-10-25 14:28:49

这对我有用:

转到

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 变量中保留您想要的版本。

This worked for me:

Go to

Control Panel\System and Security\System

select

Advanced system settings from the left panel
from Advanced tab click on Environment Variables

In the System variables section search for (create if doesn't exist)

PYTHONPATH

and set

C:\Python27\;C:\Python27\Scripts;

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.

—━☆沉默づ 2024-10-25 14:28:49

上面没有任何效果,这对我有用:

ftype Python.File=C:\Path\to\python.exe "%1" %*

此命令应该在以管理员身份启动的命令提示符中运行

警告:即使此命令中的路径设置为python35,如果您安装了python36,它也会将默认设置设置为python36。为了防止这种情况,您可以暂时将文件夹名称从 Python36 更改为 xxPython36,运行命令,然后删除对 Python 36 文件夹的更改。


编辑:这就是我最终所做的:我使用Python Launcher。
https://stackoverflow.com/a/68139696/3154274

Nothing above worked, this is what worked for me:

ftype Python.File=C:\Path\to\python.exe "%1" %*

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 to xxPython36, 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

碍人泪离人颜 2024-10-25 14:28:49

现在 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.

樱娆 2024-10-25 14:28:49

如果您使用的是 Windows,请使用 ASSOC 命令更改 python 程序的默认 python 版本。

assoc .py=<Python 3.1 directory>

If you are on Windows, use the ASSOC command to change the default python version for python programs.

assoc .py=<Python 3.1 directory>
微暖i 2024-10-25 14:28:49

检查系统当前使用的是哪一个:

python --version

将主文件夹位置(例如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:

python --version

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

绻影浮沉 2024-10-25 14:28:49

我遇到了同样的问题并通过再次执行安装文件解决了它。当你这样做时,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.

雪若未夕 2024-10-25 14:28:49

PY_PYTHON 环境变量设置为 2.6 (或您想要的任何版本)。重新启动终端或 cmd 并输入 py -0p 。 2.6 旁边应该有一个 * 表明这是现在默认的 python 版本。

Set PY_PYTHON environment variable as 2.6 (or any version you want). Restart the terminal or cmd and type py -0p. The 2.6 should have a * next to it indicating that's the default python version now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文