175

我正在运行 Windows,当您在命令行上运行程序时,shell/OS 会根据注册表设置自动运行 Python。如果我在同一台机器上安装 Python 的 2.x 和 3.x 版本,这会中断吗?

我想玩 Python 3,同时仍然能够在同一台机器上运行 2.x 脚本。

4

20 回答 20

72

官方的共存解决方案似乎是Python Launcher for Windows,PEP 397,它包含在Python 3.3.0中。将发布转储py.exepyw.exe启动器安装到%SYSTEMROOT%( C:\Windows) 中,然后分别pypyw脚本相关联。

为了使用新的启动器(无需手动设置您自己的关联),请启用“注册扩展”选项。我不太清楚为什么,但在我的机器上它把 Py 2.7 作为“默认”(启动器)。

通过直接从命令行调用它们来运行脚本将通过启动器将它们路由并解析shebang(如果存在)。您还可以显式调用启动器并使用开关:py -3 mypy2script.py.

各种shebangs似乎都有效

  • #!C:\Python33\python.exe
  • #!python3
  • #!/usr/bin/env python3

以及肆意谩骂

  • #! notepad.exe
于 2012-11-08T21:11:48.443 回答
55

这是我的设置:

  1. 使用windows 安装程序安装 Python 2.7 和 3.4 。
  2. 转到C:\Python34(默认安装路径)并将 python.exe 更改为 python3.exe
  3. 编辑 您的环境变量以包含C:\Python27\;C:\Python27\Scripts\;C:\Python34\;C:\Python34\Scripts\;

现在在命令行中,您可以使用python2.7 和python33.4。

于 2015-08-25T05:15:42.210 回答
42

从 3.3 版开始,Python 引入了 Launcher for Windows 实用程序https://docs.python.org/3/using/windows.html#python-launcher-for-windows

为了能够使用多个版本的 Python:

  1. 安装 Python 2.x(x 是您需要的任何版本)
  2. 安装 Python 3.x(x 是您需要的任何版本,也必须有一个版本 3.x >= 3.3)
  3. 打开命令提示符
  4. 键入py -2.x以启动 Python 2.x
  5. 键入py -3.x以启动 Python 3.x
于 2016-06-29T08:18:41.497 回答
37

你可以同时安装。

你应该在你的脚本前面写下这个:

#!/bin/env python2.7

或者,最终……

#!/bin/env python3.6

更新

我的解决方案与 Unix 完美配合,在Google上快速搜索后,这里是 Windows 解决方案:

#!c:/Python/python3_6.exe -u

同样的事情:在你的脚本前面。

于 2008-12-04T16:29:38.713 回答
9

我正在使用 shell 中的 2.5、2.6 和 3.0 以及以下形式的一行批处理脚本:

:: The @ symbol at the start turns off the prompt from displaying the command.
:: The % represents an argument, while the * means all of them.
@c:\programs\pythonX.Y\python.exe %*

命名它们pythonX.Y.bat并将它们放在您的 PATH 中的某个位置。将首选次要版本(即最新版本)的文件复制到pythonX.bat. (例如copy python2.6.bat python2.bat。)然后您可以python2 file.py在任何地方使用。

但是,这无助于甚至影响 Windows 文件关联情况。为此,您需要一个启动程序来读取该#!行,然后将其与 .py 和 .pyw 文件相关联。

于 2009-01-12T18:26:55.780 回答
8

当您将两者都添加到环境变量时,将会发生冲突,因为这两个可执行文件具有相同的名称:python.exe.

只需重命名其中一个。就我而言,我将其重命名为python3.exe.

因此,当我运行python时,它将执行python.exe2.7,当我运行python3时,它将执行python3.exe3.6

在此处输入图像描述

于 2017-10-19T08:24:24.150 回答
7

干得好...

winpylaunch.py

#
# Looks for a directive in the form: #! C:\Python30\python.exe
# The directive must start with #! and contain ".exe".
# This will be assumed to be the correct python interpreter to
# use to run the script ON WINDOWS. If no interpreter is
# found then the script will be run with 'python.exe'.
# ie: whatever one is found on the path.
# For example, in a script which is saved as utf-8 and which
# runs on Linux and Windows and uses the Python 2.6 interpreter...
#
#    #!/usr/bin/python
#    #!C:\Python26\python.exe
#    # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:\Python26\python.exe.
#
# To set up the association add this to the registry...
#
#    HKEY_CLASSES_ROOT\Python.File\shell\open\command
#    (Default) REG_SZ = "C:\Python30\python.exe" S:\usr\bin\winpylaunch.py "%1" %*
#
# NOTE: winpylaunch.py itself works with either 2.6 and 3.0. Once
# this entry has been added python files can be run on the
# commandline and the use of winpylaunch.py will be transparent.
#

import subprocess
import sys

USAGE = """
USAGE: winpylaunch.py <script.py> [arg1] [arg2...]
"""

if __name__ == "__main__":
  if len(sys.argv) > 1:
    script = sys.argv[1]
    args   = sys.argv[2:]
    if script.endswith(".py"):
      interpreter = "python.exe" # Default to wherever it is found on the path.
      lines = open(script).readlines()
      for line in lines:
        if line.startswith("#!") and line.find(".exe") != -1:
          interpreter = line[2:].strip()
          break
      process = subprocess.Popen([interpreter] + [script] + args)
      process.wait()
      sys.exit()
  print(USAGE)

我刚刚在阅读这个线程时敲了这个(因为这也是我需要的)。我在 Ubuntu 和 Windows 上都有 Python 2.6.1 和 3.0.1。如果它对您不起作用,请在此处发布修复。

于 2009-04-18T01:45:53.130 回答
5

这是在 Windows 上安装 Python2 和 Python3 的一种简洁的方法。

https://datascience.com.co/how-to-install-python-2-7-and-3-6-in-windows-10-add-python-path-281e7eae62a

我的情况:我必须安装 Apache cassandra。我的D: drive中已经安装了 Python3 。由于正在进行大量的开发工作,我不想弄乱我的 Python3 安装。而且,我只需要用于 Apache cassandra 的 Python2。

所以我采取了以下步骤:

  1. 已下载并安装 Python2。
  2. 将 Python2 条目添加到类路径 ( C:\Python27;C:\Python27\Scripts)
  3. 修改python.exepython2.exe(如下图所示)

在此处输入图像描述

  1. 现在我可以同时运行两者。对于 Python 2( python2 --version) 和 Python 3 ( python --version)。 在此处输入图像描述

所以,我的 Python3 安装保持不变。

于 2019-10-08T11:49:06.790 回答
5

尝试使用 Anaconda。

使用 Anaconda 环境的概念,假设您需要 Python 3 来学习编程,但您不想通过更新 Python 来消灭您的 Python 2.7 环境。您可以创建并激活一个名为“snakes”(或任何您想要的)的新环境,并安装最新版本的 Python 3,如下所示:

conda create --name snakes python=3

它比听起来简单,看看这里的介绍页面:Anaconda 入门

然后要处理同时运行版本 2.x 和 3.x 的具体问题,请参阅:

于 2017-05-16T19:23:11.197 回答
4

据我所知,Python 使用 PATH 变量而不是注册表设置从命令行运行。

因此,如果您指向 PATH 上的正确版本,您将使用它。请记住重新启动命令提示符以使用新的 PATH 设置。

于 2008-12-04T16:29:01.273 回答
3

Python 安装通常将.py,.pyw.pycfiles 与 Python 解释器相关联。因此,您可以通过在资源管理器中双击它或在命令行窗口中键入其名称来运行 Python 脚本(因此无需键入python scriptname.py,就可以了scriptname.py)。

如果要手动更改此关联,可以在 Windows 注册表中编辑这些键:

HKEY_CLASSES_ROOT\Python.File\shell\open\command
HKEY_CLASSES_ROOT\Python.NoConFile\shell\open\command
HKEY_CLASSES_ROOT\Python.CompiledFile\shell\open\command

Python 启动器

人们一直在为 Windows 开发 Python 启动器:一个.py.pyw文件关联的轻量级程序,它会在第一行查找“shebang”行(类似于 Linux 等),并启动 Python 2.x 或 3.x必需的。有关详细信息,请参阅“适用于 Windows 的 Python 启动器”博客文章。

于 2009-03-03T00:57:41.167 回答
2

Easy-peasy,在安装两个 python 版本后,添加环境变量的路径;请参阅环境变量设置。然后转到 python 2 和 python 3 文件夹,并分别将它们重命名为 python2 和 python3 ,如图所示这里是python2这里是python3。现在在 cmd 中键入 python2 或 python3 以使用您所需的版本,请参阅这里.

于 2019-06-20T03:11:11.350 回答
2

以下是如何在同一台机器上运行 Python 2 和 3

  1. 安装 Python 2.x
  2. 安装 Python 3.x
  3. 启动 Powershell
  4. 键入Python -2以启动 Python 2.x
  5. 键入Python -3以启动 Python 2.x

正如 2011 年 Standalone 首次亮相时所承诺的那样,Windows的Python 启动器自 3.3 版开始嵌入到 Python 中:

适用于 Windows 的 Python 启动器

于 2016-01-03T03:10:43.540 回答
1

您应该确保 PATH 环境变量不包含两个 python.exe 文件(添加您当前用于日常运行脚本的那个),或者按照 Kniht 建议的批处理文件进行操作。除此之外,我不明白为什么不这样做。

PS:我安装了 2.6 作为我的“主要” python 和 3.0 作为我的“播放” python。2.6 包含在PATH中。一切正常。

于 2009-01-17T16:53:19.377 回答
1

我认为有一个选项可以在安装程序中为 .py 文件设置 Windows 文件关联。取消选中它,你应该没问题。

如果没有,您可以轻松地将 .py 文件与以前的版本重新关联。最简单的方法是右键单击 .py 文件,选择“打开方式”/“选择程序”。在出现的对话框中,选择或浏览到您要默认使用的 python 版本,并选中“始终使用此程序打开此类文件”复选框。

于 2008-12-04T16:33:47.393 回答
1

我有同样的问题,我想在大多数工作中使用 python3,但 IDA pro 需要 python2。所以,这就是我所做的。

我首先在用户环境变量中创建了3个变量如下:

  1. PYTHON_ACTIVE :这最初是空的
  2. HOME_PYTHON27 :具有安装 Python 2 的文件夹的路径。例如。“;/脚本;”
  3. HOME_PYTHON38 :与 python 2 类似,此变量包含指向 python 3 文件夹的路径。

现在我添加了

%PYTHON_ACTIVE%

到 PATH 变量。所以,基本上说这个“PYTHON_ACTIVE”包含的任何东西都是活动的python。我们以编程方式更改“PYTHON_ACTIVE”的包含以切换 python 版本。

这是示例脚本:

:: This batch file is used to switch between python 2 and 3.
@ECHO OFF

set /p choice= "Please enter '27' for python 2.7 , '38' for python 3.8 : "

IF %choice%==27 (
setx PYTHON_ACTIVE %HOME_PYTHON27%
)

IF %choice%==38 (
setx PYTHON_ACTIVE %HOME_PYTHON38%
)


PAUSE

该脚本将 python 版本作为输入,并相应地将 HOME_PYTHON27 或 HOME_PYTHON38 复制到 PYTHON_ACTIVE。从而更改全局 Python 版本。

于 2020-05-17T09:25:03.937 回答
1

在我勇敢地同时安装两者之前,我有很多问题。如果我给python,当我想要py2时它会去py3吗?pip/virtualenv 会在 py2/3 下发生吗?

现在看起来很简单。

只是盲目地安装它们。确保你得到正确的类型(x64/x32)。安装时/安装后确保添加到环境变量的路径。

[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHONx", "USER")

替换上面命令中的 x 来设置路径。

然后转到这两个文件夹。

导航

python3.6/Scripts/

并将 pip 重命名为 pip3。

如果 pip3 已经存在,请删除 pip。这将确保 pip 将在python2下运行。您可以通过以下方式进行验证:

pip --version

如果您想将 pip 与python3一起使用,则只需使用

pip3 install 

您可以类似地对 python 文件和其他文件执行相同的操作。

干杯!

于 2017-03-12T09:32:51.443 回答
0

我会假设是这样,我在同一台计算机上并排安装了 Python 2.4、2.5 和 2.6。

于 2008-12-04T17:31:33.987 回答
0

我现在才刚开始使用python。我正在阅读 Zed Shaw 的书“Learn Python the Hard Way”,它需要 python 2.x 版本,但我也在学习需要 python 3.x 的课程

所以这就是我所做的。

  1. 下载python 2.7
  2. 运行power shell(应该已经安装在windows上)
  3. 在 POWERSHELL 中运行 python(如果无法识别,则转到第 4 步)
  4. 仅当 powershell 在以下情况下无法识别 python 2.7类型时:

"[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHON27", "USER")" (没有外部引号)

  1. 现在输入 python 你应该看到它说 python 2.7 blah blah blah

现在适用于 python 3.x

简单,python 3.x 下载附带 python for windows 应用程序。因此,只需将 Python for Windows 应用程序固定到您的任务栏,或创建桌面快捷方式,您就完成了!

为 3.x 打开适用于 Windows 的 Python

为 python 2.x 打开 Powershell

我希望这有帮助!

于 2017-02-07T00:46:31.597 回答
0

嗯..我现在通过在https://www.python.org/downloads/release/python-365/下载适用于 Windows 的 Python 3.6.5并确保安装了启动器来做到这一点。然后,我按照使用 python 2 和 python 3 的说明进行操作。重新启动命令提示符,然后使用py -2.7to use Python 2 and pyor py -3.6to use Python 3。您也可以使用pip2for Python 2'spippipfor Python 3's pip

于 2018-06-07T01:37:02.130 回答