[英]Can I install Python 3.x and 2.x on the same Windows computer?
我正在运行 Windows,当您在命令行上运行程序时,shell/OS 会根据注册表设置自动运行 Python。 如果我在同一台机器上安装 2.x 和 3.x 版本的 Python,这会中断吗?
我想在使用 Python 3 的同时仍然能够在同一台机器上运行 2.x 脚本。
共存的官方解决方案似乎是Python Launcher for Windows ,PEP 397,它包含在Python 3.3.0 中。 安装发布转储py.exe
和pyw.exe
启动器到%SYSTEMROOT%
( C:\\Windows
),然后分别与py
和pyw
脚本相关联。
为了使用新的启动器(无需手动设置您自己的关联),请启用“注册扩展”选项。 我不太确定为什么,但在我的机器上,它把 Py 2.7 作为(启动器的)“默认”。
通过直接从命令行调用它们来运行脚本将通过启动器路由它们并解析shebang(如果存在)。 您还可以显式调用启动器并使用开关: py -3 mypy2script.py
。
各种shebangs似乎都有效
#!C:\\Python33\\python.exe
#!python3
#!/usr/bin/env python3
以及肆意的辱骂
#! notepad.exe
这是我的设置:
C:\\Python34
(默认安装路径)并将 python.exe 更改为 python3.exeC:\\Python27\\;C:\\Python27\\Scripts\\;C:\\Python34\\;C:\\Python34\\Scripts\\;
现在在命令行中,您可以将python
用于 2.7,将python3
用于 3.4。
从 3.3 版开始,Python 引入了 Launcher for Windows 实用程序https://docs.python.org/3/using/windows.html#python-launcher-for-windows 。
所以为了能够使用多个版本的 Python:
您可以同时安装两者。
你应该在你的脚本前面写这个:
#!/bin/env python2.7
或者,最终……
#!/bin/env python3.6
我的解决方案与 Unix 完美配合,在Google上快速搜索后,这是 Windows 解决方案:
#!c:/Python/python3_6.exe -u
同样的事情:在你的脚本前面。
我正在使用 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 文件相关联。
干得好...
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。 如果它对您不起作用,请在此处发布修复程序。
尝试使用 Anaconda。
使用 Anaconda 环境的概念,假设您需要 Python 3 来学习编程,但您不想通过更新 Python 来消除您的 Python 2.7 环境。 您可以创建并激活一个名为“snakes”(或任何您想要的)的新环境,并安装最新版本的 Python 3,如下所示:
conda create --name snakes python=3
它比听起来更简单,请查看此处的介绍页面: Anaconda 入门
然后要处理让版本 2.x 和 3.x 并排运行的特定问题,请参阅:
这是在 Windows 上安装 Python2 和 Python3 的一种简洁明了的方法。
我的情况:我必须安装 Apache cassandra。 我已经在D: 驱动器中安装了 Python3。 由于正在进行大量的开发工作,我不想弄乱我的 Python3 安装。 而且,我只需要为 Apache cassandra 使用 Python2。
所以我采取了以下步骤:
C:\\Python27;C:\\Python27\\Scripts
)所以,我的 Python3 安装保持不变。
据我所知,Python 使用 PATH 变量而不是注册表设置从命令行运行。
因此,如果您指向 PATH 上的正确版本,您将使用它。 请记住重新启动命令提示符以使用新的 PATH 设置。
Python 安装通常将.py
、 .pyw
和.pyc
文件与 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
人们一直在开发适用于 Windows 的 Python 启动器:一个与.py
和.pyw
文件关联的轻量级程序,它会在第一行查找“shebang”行(类似于 Linux 等),然后启动 Python 2.x 或3.x 根据需要。 有关详细信息,请参阅“适用于 Windows 的 Python 启动器”博客文章。
was embedded into Python since Version 3.3, as promised in 2011 when the Stand alone first made its debut:正如 2011 年 Standalone 首次亮相时所承诺的那样,的从 3.3 版开始嵌入到 Python 中:
我认为有一个选项可以在安装程序中为 .py 文件设置 Windows 文件关联。 取消选中它,你应该没问题。
如果没有,您可以轻松地将 .py 文件与以前的版本重新关联。 最简单的方法是右键单击 .py 文件,选择“打开方式”/“选择程序”。 在出现的对话框中,选择或浏览到您要默认使用的python版本,并选中“始终使用此程序打开此类文件”复选框。
在我勇敢地同时安装两者之前,我有很多问题。 如果我给 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 文件和其他文件执行相同的操作。
干杯!
您应该确保 PATH 环境变量不包含这两个 python.exe 文件(添加当前用于每天运行脚本的文件),或者按照 Kniht 对批处理文件的建议进行操作。 除此之外,我不明白为什么不。
PS:我安装了 2.6 作为我的“主要” python 和 3.0 作为我的“播放” python 。 2.6 包含在PATH 中。 一切正常。
我遇到了同样的问题,我想在大多数工作中使用 python3,但 IDA pro 需要 python2。 所以,这就是我所做的。
我首先在用户环境变量中创建了 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 版本。
我认为是这样,我在同一台计算机上并排安装了 Python 2.4、2.5 和 2.6。
我现在才刚开始使用 python。 我正在阅读 Zed Shaw 的书“Learn Python the Hard Way”,该书需要 Python 2.x 版,但我也在学习需要 Python 3.x 的课程
所以这就是我所做的。
"[ENVIRONMENT]::SETENVIRONMENTVARIABLE("PATH", "$ENV:PATH;C:\\PYTHON27", "USER")"(无外引号)
现在适用于 python 3.x
简单,python 3.x 下载附带 python for windows 应用程序。 因此,只需将 Python for Windows 应用程序固定到您的任务栏,或创建桌面快捷方式即可!
打开 Python for Windows for 3.x
为 python 2.x 打开 Powershell
我希望这有帮助!
嗯..我现在通过在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 2的pip
和pip
对于 Python 3 的pip
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.