我可以在同一台 Windows 计算机上安装 Python 3.x 和 2.x 吗?[重复]
- 2025-03-18 08:54:00
- admin 原创
- 57
问题描述:
我运行的是 Windows,当您在命令行上运行程序时,shell/OS 会根据注册表设置自动运行 Python。如果我在同一台机器上安装 2.x 和 3.x 版本的 Python,这会中断吗?
我想使用 Python 3,同时仍然能够在同一台机器上运行 2.x 脚本。
解决方案 1:
共存的官方解决方案似乎是适用于Windows 的 Python 启动器,PEP 397,包含在Python 3.3.0中。将发布转储py.exe
和pyw.exe
启动器安装到%SYSTEMROOT%
( )中,然后分别与和脚本C:Windows
关联。py
`pyw`
为了使用新启动器(无需手动设置与它的关联),请保持“注册扩展”选项处于启用状态。我不太清楚为什么,但在我的计算机上,它将 Py 2.7 保留为(启动器的)“默认”设置。
通过直接从命令行调用脚本来运行脚本将通过启动器路由它们并解析 shebang(如果存在)。您还可以明确调用启动器并使用开关:py -3 mypy2script.py
。
各种方法似乎都有效
#!C:Python33python.exe
#!python3
#!/usr/bin/env python3
以及肆意滥用
#! notepad.exe
解决方案 2:
这是我的设置:
使用Windows 安装程序安装 Python 2.7 和 3.4 。
转到
C:Python34
(默认安装路径)并将 python.exe 更改为 python3.exe编辑 环境变量以包含
C:Python27;C:Python27Scripts;C:Python34;C:Python34Scripts;
现在您可以在命令行中使用python
2.7 和python3
3.4。
解决方案 3:
从 3.3 版本开始,Python 引入了适用于 Windows 实用程序的启动器https://docs.python.org/3/using/windows.html#python-launcher-for-windows。
为了能够使用多个版本的 Python:
安装 Python 2.x(x 是您需要的任何版本)
安装 Python 3.x(x 是您需要的任何版本,您还必须有一个版本 3.x >= 3.3)
打开命令提示符
输入py -2.x启动 Python 2.x
输入py -3.x启动 Python 3.x
解决方案 4:
您可以同时安装两者。
你应该在脚本前面写下这个:
#!/bin/env python2.7
或者,最终……
#!/bin/env python3.6
更新
我的解决方案在 Unix 上完美运行,在Google上快速搜索后,找到了 Windows 解决方案:
#!c:/Python/python3_6.exe -u
同样的事情:在你的剧本前面。
解决方案 5:
这是一个在 Windows 上安装 Python2 和 Python3 的简洁方法。
我的情况:我必须安装 Apache Cassandra。我的D: 驱动器中已安装了 Python3 。由于正在进行大量开发工作,我不想弄乱我的 Python3 安装。而且,我只需要 Apache Cassandra 的 Python2。
因此我采取了以下步骤:
下载并安装 Python2。
将 Python2 条目添加到类路径(
C:Python27;C:Python27Scripts
)将python.exe修改为python2.exe(如下图)
现在我可以同时运行两者了。对于 Python 2(
python2 --version
) 和 Python 3(python --version
)。
所以,我的 Python3 安装保持完整。
解决方案 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:programspythonX.Ypython.exe %*
命名它们pythonX.Y.bat
并将它们放在您的 PATH 中的某个位置。将首选次要版本(即最新版本)的文件复制到pythonX.bat
。(例如copy python2.6.bat python2.bat
。)然后您可以python2 file.py
从任何地方使用。
但是,这不会帮助甚至影响 Windows 文件关联情况。为此,您需要一个启动程序来读取该#!
行,然后将其与 .py 和 .pyw 文件关联。
解决方案 7:
当您将两者都添加到环境变量中时,就会发生冲突,因为两个可执行文件具有相同的名称:python.exe
。
只需重命名其中一个即可。就我而言,我将其重命名为python3.exe
。
因此,当我运行python
它时,它将执行python.exe
2.7,当我运行它时python3
,它将执行python3.exe
3.6
解决方案 8:
干得好...
启动器
#
# Looks for a directive in the form: #! C:Python30python.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:Python26python.exe
# # -*- coding: utf-8 -*-
#
# When run on Linux, Linux uses the /usr/bin/python. When run
# on Windows using winpylaunch.py it uses C:Python26python.exe.
#
# To set up the association add this to the registry...
#
# HKEY_CLASSES_ROOTPython.Fileshellopencommand
# (Default) REG_SZ = "C:Python30python.exe" S:/usrinwinpylaunch.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 上都有 Pythons 2.6.1 和 3.0.1。如果它不适合您,请在此处发布修复。
解决方案 9:
尝试使用 Anaconda。
使用 Anaconda 环境的概念,假设您需要 Python 3 来学习编程,但您不想通过更新 Python 来清除 Python 2.7 环境。您可以创建并激活一个名为“snakes”(或您想要的任何名称)的新环境,并安装最新版本的 Python 3,如下所示:
conda create --name snakes python=3
它比听起来简单,看看这里的介绍页面:Anaconda 入门
然后,要处理版本 2.x 和 3.x 并排运行的特定问题,请参阅:
使用 Anaconda 管理 Python 版本
在 Python 2 和 Python 3 环境之间切换
解决方案 10:
据我所知,Python 使用 PATH 变量而不是注册表设置通过命令行运行。
因此,如果您在 PATH 上指向正确的版本,您将使用该版本。请记住重新启动命令提示符以使用新的 PATH 设置。
解决方案 11:
Python 安装通常会将.py
、.pyw
和.pyc
文件与 Python 解释器关联起来。因此,您可以通过在资源管理器中双击它或在命令行窗口中键入其名称来运行 Python 脚本(因此无需键入python scriptname.py
,只需scriptname.py
键入 即可)。
如果您想手动更改此关联,您可以在 Windows 注册表中编辑这些键:
HKEY_CLASSES_ROOTPython.Fileshellopencommand
HKEY_CLASSES_ROOTPython.NoConFileshellopencommand
HKEY_CLASSES_ROOTPython.CompiledFileshellopencommand
Python启动器
人们一直在开发适用于 Windows 的 Python 启动器:一个与.py
和.pyw
文件关联的轻量级程序,它会在第一行查找“shebang”行(类似于 Linux 等),并根据需要启动 Python 2.x 或 3.x。有关详细信息,请参阅“适用于 Windows 的 Python 启动器”博客文章。
解决方案 12:
以下是如何在同一台机器上运行 Python 2 和 3
安装 Python 2.x
安装 Python 3.x
启动 Powershell
输入Python -2启动 Python 2.x
输入Python -3启动 Python 3.x
从 3.3 版开始,适用于 Windows 的 Python 启动器就嵌入到了 Python 中,正如 2011 年 Stand alone 首次亮相时所承诺的那样:
适用于 Windows 的 Python 启动器
解决方案 13:
非常简单,安装两个 python 版本后,将路径添加到环境变量中;参见。然后转到 python 2 和 python 3 文件夹并分别将它们重命名为 python2 和 python3,如图所示
。
现在在 cmd 中输入 python2 或 python3 以使用所需的版本,参见
。
解决方案 14:
我认为安装程序中有一个选项可以设置 .py 文件的 Windows 文件关联。取消选中它就可以了。
如果没有,您可以轻松地将 .py 文件与以前的版本重新关联。最简单的方法是右键单击 .py 文件,选择“打开方式”/“选择程序”。在出现的对话框中,选择或浏览到您要默认使用的 python 版本,然后选中“始终使用此程序打开此类文件”复选框。
解决方案 15:
您应该确保 PATH 环境变量不包含两个 python.exe 文件(添加您当前每天用来运行脚本的文件),或者按照 Kniht 对批处理文件的建议进行操作。除此之外,我不明白为什么不可以。
PS:我安装了 2.6 作为我的“主要” python,安装了 3.0 作为我的“播放” python。2.6 包含在PATH中。一切正常。
解决方案 16:
在我勇敢地同时安装两者之前,我有很多疑问。如果我提供 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
如果你想在python3中使用 pip ,那么只需使用
pip3 install
您可以对 python 文件和其他文件执行相同的操作。
干杯!
解决方案 17:
我遇到了同样的问题,我想在大多数工作中使用 python3,但 IDA pro 需要 python2。所以,这就是我所做的。
我首先在用户环境变量中创建了3个变量,如下所示:
PYTHON_ACTIVE :最初为空
HOME_PYTHON27 :具有安装 Python 2 的文件夹的路径。例如“;/scripts;”
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 版本。
解决方案 18:
我认为是这样,我在同一台计算机上并排安装了 Python 2.4、2.5 和 2.6。
解决方案 19:
我现在才刚开始学习 Python。我正在阅读 Zed Shaw 的书《Learn Python the Hard Way》,这本书需要 Python 2.x 版本,但我也在上一门需要 Python 3.x 的课程
所以我做了以下事情。
下载 python 2.7
运行 power shell (应该已经在 windows 上安装了)
在 POWERSHELL 中运行 python(如果无法识别则转到步骤 4)
仅当 powershell 无法识别以下 python 2.7 类型时:
“[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\PYTHON27", "USER")” (无外部引号)
现在输入 python 你应该看到它说 python 2.7 blah blah blah
现在适用于 Python 3.x
简单,python 3.x 下载附带适用于 Windows 的 python 应用程序。因此,只需将适用于 Windows 的 Python 应用程序固定到任务栏,或创建桌面快捷方式即可!
打开适用于 Windows 的 Python 3.x
打开适用于 Python 2.x 的 Powershell
我希望这有帮助!
解决方案 20:
嗯...我现在就这样做了,只需在https://www.python.org/downloads/release/python-365/下载适用于 Windows 的 Python 3.6.5 ,并确保启动器已安装。然后,我按照使用 Python 2 和 Python 3 的说明进行操作。重新启动命令提示符,然后使用py -2.7
使用 Python 2 和py
或py -3.6
使用 Python 3。您也可以使用pip2
Python 2pip
和pip
Python 3 pip
。
扫码咨询,免费领取项目管理大礼包!