我可以在同一台 Windows 计算机上安装 Python 3.x 和2.x 吗?

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

我希望能够在同一台机器上运行2.x 脚本的同时使用 Python 3。

252761 次浏览

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

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

你可以把两个都装上。

你应该在你的剧本前写下这些:

#!/bin/env python2.7

或者,最终..。

#!/bin/env python3.6

更新

我的解决方案在 Unix 上运行良好,在 谷歌上快速搜索一下,下面是 Windows 的解决方案:

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

同样的事情: 在你的剧本前。

我认为有一个设置 Windows 文件关联的选项。安装程序中的 py 文件。取消检查,你会没事的。

如果没有,你可以很容易地重新联想。旧版本的 py 文件。最简单的方法是右键单击。Py 文件,选择“ open with”/“ select program”。在出现的对话框中,选择或浏览到您想要默认使用的 python 版本,并选中“总是使用此程序打开此类文件”复选框。

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

我使用的是来自 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 文件。

您应该确保 PATH 环境变量不包含两个 python.exe 文件(添加当前用于每天运行脚本的文件) ,或者按奈特的建议处理批处理文件。 除此之外,没什么不可以的。

PS: 我安装了2.6作为我的 “初级” python,3.0作为我的 “玩” python。路径包含了2.6版本。一切正常。

Python 安装通常将 .py.pyw.pyc文件与 Python 解释器相关联。因此,您可以通过在 Explorer 中双击 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

巨蟒 l 射器

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

给你。

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。如果它不工作为您后期修复这里。

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

为了使用新的启动程序(不需要手动设置您自己的关联) ,保持“ Register Extended”选项处于启用状态。我不太确定为什么,但是在我的机器上,它将 Py2.7作为(启动程序的)“默认值”。

通过从命令行直接调用脚本来运行脚本,这样就可以将脚本路由到启动程序并解析 shebang (如果存在的话)。您还可以显式调用启动程序并使用开关: py -3 mypy2script.py

各种各样的方法似乎都奏效了

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

以及滥用职权

  • #! notepad.exe

这是我的设置:

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

现在在命令行中,您可以对2.7使用 python,对3.4使用 python3

下面是如何在同一台机器上运行 Python2和3

  1. 安装 Python2.x
  2. 安装 Python3.x
  3. 启动 Powershell
  4. 键入 巨蟒 -2启动 Python2.x
  5. 键入 巨蟒 -3启动 Python3.x

用于 Windows 的 Python 启动程序从3.3版本开始就嵌入到 Python 中,正如2011年独立版首次亮相时所承诺的那样:

用于 Windows 的 Python 启动程序

从3.3版本开始,Python 为 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启动 Python2.x
  5. 键入 Py-3. x启动 Python3.x

我现在才刚刚开始学蟒蛇。我正在读 Zed Shaw 的书《艰难学习 Python 》 ,这本书需要 Python 2.x 版本,但我也在上一门需要 Python 3的课。X

我是这么做的。

  1. 下载 python 2.7
  2. 运行 PowerShell (应该已经安装在 Windows 上)
  3. 在 POWERSHELL 中运行 python (如果它不能识别,那么转到步骤4)
  4. 只有在 powershell 不能识别 python 2.7 类型的情况下:

“[环境] : 环境变量(“路径”,“ $ENV: 路径; C: PYTHON27”,“用户”) (没有引号)

  1. 现在输入 python,应该会看到 python 2.7等等

现在对于 python3.x

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

开放 Python for Windows for 3. x

为 python2.x 打开 Powershell

希望这个能帮上忙!

在我勇敢地同时安装这两个软件之前,我有很多问题。如果我给 python,当我想要 py2的时候,它会转到 py3吗?将在 py2/3下发生 pip/viralenv?

现在似乎很简单了。

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

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

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

然后去两个文件夹。

导航到

python3.6/Scripts/

并将 pip 重命名为 pip3。

如果 pip3已经存在,则删除 pip。这将确保只有 pip 将在 蟒蛇2下运行。你可透过以下途径核实:

pip --version

如果你想在 蟒蛇3中使用 pip,那么只需使用

pip3 install

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

干杯!

试试用蟒蛇。

使用 Anaconda 环境的概念,假设您需要 Python 3来学习编程,但是您不希望通过更新 Python 来清除 Python 2.7环境。您可以创建并激活一个名为“ Snake”(或任何您想要的)的新环境,并按照以下方式安装 Python 3的最新版本:

conda create --name snakes python=3

它比听起来要简单,看看这里的介绍页面: 从水蟒开始

然后,为了解决版本2. x 和版本3. x 并行运行的具体问题,请参阅:

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

只要重命名其中之一。在我的情况下,我将其重命名为 python3.exe

所以当我运行 python的时候它会执行 python.exe,也就是2.7 当我运行 python3的时候,它会执行 python3.exe,也就是3.6

enter image description here

嗯。.我现在只是在 https://www.python.org/downloads/release/python-365/上下载了用于 Windows 的 Python 3.6.5,并确保安装了启动程序。然后,我遵循了使用 python2和 python3的说明。重新启动命令提示符,然后使用 py -2.7来使用 Python2,使用 pypy -3.6来使用 Python3。还可以将 pip2用于 Python 2的 pip,将 pip用于 Python 3的 pip

这里有一个简洁的方法来在 Windows 上安装 Python 2和 Python 3。

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

我的情况是: 我不得不安装 Cassandra。我已经在 开车中安装了 Python 3。由于有大量的开发工作在进行中,我不想搞砸我的 Python 3安装。而且我需要 Python 2只是为了 Cassandra。

所以我采取了以下步骤:

  1. 下载并安装 Python 2。
  2. 向类路径(C:\Python27;C:\Python27\Scripts)添加了 Python2条目
  3. Python exe修改为 Python2.exe(如下图所示)

enter image description here

  1. 现在我可以同时运行这两个程序了,对于 Python 2(python2 --version)和 Python 3(python --version)。 enter image description here

因此,我的 Python 3安装仍然完好无损。

我遇到了同样的问题,我希望在大多数工作中使用 python3,但是 IDA pro 需要 python2。我是这么做的。

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

  1. PYTHON _ Aactive: 这最初是空的
  2. HOME _ PYTHON27: 有一个到安装 Python 2的文件夹的路径
  3. HOME _ PYTHON38: 类似于 python2,这个变量包含到 python3文件夹的路径。

现在我加上

% PYTHON _ Aactive%

到 PATH 变量。所以,基本上就是说,不管这个“ PYTHON _ Aactive”包含什么,它都是活动的 PYTHON。我们以编程方式更改“ PYTHON _ Aactive”的包含以切换 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 _ Aactive。因此改变了全局 Python 版本。