102

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?

4

15 回答 15

113

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

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

您将看到系统属性。从这里导航到Advanced Tab -> 点击Environment Variables

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

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

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

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

于 2018-10-21T07:30:51.373 回答
82

Python 安装程序会安装适用于 Windows 的 Python Launcher。该程序 ( py.exe) 与 Python 文件扩展名相关联,并查找“shebang”注释以指定要运行的 Python 版本。这允许 Python 的多个版本共存,并允许 Python 脚本在需要时明确指定要使用的版本。如果未指定,则默认使用当前架构的最新 Python 版本(x86 或 x64)。可以通过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;.pyoPATHEXT环境变量,然后命令行可以script没有扩展名。

于 2011-02-23T08:31:58.293 回答
68

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

py -2

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

py -3

如果您希望将 Python 3.x 作为默认版本,则需要创建环境变量“PY_PYTHON”并将其值设置为 3。

于 2017-01-14T13:19:55.733 回答
22

如果您知道Environment variables系统变量名为path,请考虑任何较早出现的二进制版本的任何版本,都将被用作默认值。

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

在此处输入图像描述

于 2020-01-23T07:49:53.513 回答
18

如果您是 Windows 用户并且您拥有 Python 3.3 或更高版本,您应该在您的计算机上安装适用于 Windows 的 Python Launcher,这是用于启动所有 Python 脚本的推荐方式(无论脚本需要何种 Python 版本) )。

作为用户

  • 从命令行运行脚本时始终键入py而不是。python

  • 设置您的“打开方式...”资源管理器默认程序关联C:\Windows\py.exe

  • 设置命令行文件扩展名关联以使用适用于 Windows 的 Python Launcher(这将使输入成为py可选):

    ftype Python.File="C:\windows\py.exe" "%L" %*

    ftype Python.NoConFile="C:\Windows\pyw.exe" "%L" %*

  • 通过设置PY_PYTHON环境变量设置您首选的默认版本(例如PY_PYTHON=3.7)。您可以通过键入查看默认的 python 版本py。您还可以设置PY_PYTHON3PY_PYTHON2指定默认的 python 3 和 python 2 版本(如果有多个)。

  • 如果需要运行特定版本的python,可以使用py -M.m(哪里M是主要版本,哪里是m次要版本)。例如,py -3将运行任何已安装的 python 3 版本。

  • 列出已安装的 python 版本py -0

作为编剧

  • 在脚本顶部包含一个 shebang 行,指示所需的 python 的主要版本号。如果脚本与任何其他次要版本不兼容,请同时包含次要版本号。例如:

    #!/usr/bin/env python3

  • 您也可以使用 shebang 线来指示虚拟环境(请参阅下面的 PEP 486)。


也可以看看

于 2019-08-02T23:02:38.390 回答
15

看到这里的原始帖子

;
; 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 所在的位置创建了一个py.ini文件,其内容如下:c:\windows\

[defaults]
python=3

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

于 2013-05-11T21:48:05.703 回答
9
  1. 编辑注册表项HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\python.exe\default
  2. .py设置打开文件的默认程序python.exe
于 2012-08-02T03:14:22.667 回答
5

这对我有用。

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

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

步骤1

在此处输入图像描述

第2步

在此处输入图像描述

第 3 步

在此处输入图像描述

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

蟒蛇--版本

>>> Python 3.6
于 2018-12-22T01:07:01.613 回答
5

这对我有用:

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

于 2017-11-14T00:24:36.177 回答
3

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

assoc .py=<Python 3.1 directory>
于 2021-07-23T08:08:54.790 回答
2

现在 Python 3.3 已经发布,使用此处描述的 py.exe 实用程序是最简单的:http: //www.python.org/dev/peps/pep-0397/

它允许您使用 UNIX 样式指令在脚本文件中指定 Python 版本。还有用于控制运行哪个版本的 Python 的命令行和环境变量选项。

获取此实用程序的最简单方法是安装 Python 3.3 或更高版本。

于 2013-01-03T05:57:49.863 回答
2

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

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

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

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

于 2016-11-30T06:30:44.950 回答
1

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

python --version

将主文件夹位置(例如 C/ProgramFiles)和脚本位置(C/ProgramFiles/Scripts)添加到系统的环境变量中。添加 3.x 版本和 2.x 版本

路径位置在环境变量中排名。如果要使用 Python 2.x,只需将 python 2.x 的路径放在首位,如果要使用 Python 3.x,只需将 3.x 放在首位

这使用python 2

于 2018-04-06T09:06:11.750 回答
0

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

SET PATH=C:\Program Files\Python 3.5
于 2016-10-22T14:31:06.457 回答
-4

尝试修改 Windows 注册表中的路径 (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment)。

警告:不要破坏注册表:)

于 2011-02-23T06:54:39.867 回答