我正在运行 Windows,当您在命令行上运行程序时,shell/OS 会根据注册表设置自动运行 Python。如果我在同一台机器上安装 Python 的 2.x 和 3.x 版本,这会中断吗?
我想玩 Python 3,同时仍然能够在同一台机器上运行 2.x 脚本。
我正在运行 Windows,当您在命令行上运行程序时,shell/OS 会根据注册表设置自动运行 Python。如果我在同一台机器上安装 Python 的 2.x 和 3.x 版本,这会中断吗?
我想玩 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。如果它对您不起作用,请在此处发布修复。
这是在 Windows 上安装 Python2 和 Python3 的一种简洁的方法。
我的情况:我必须安装 Apache cassandra。我的D: drive中已经安装了 Python3 。由于正在进行大量的开发工作,我不想弄乱我的 Python3 安装。而且,我只需要用于 Apache cassandra 的 Python2。
所以我采取了以下步骤:
C:\Python27;C:\Python27\Scripts
)所以,我的 Python3 安装保持不变。
尝试使用 Anaconda。
使用 Anaconda 环境的概念,假设您需要 Python 3 来学习编程,但您不想通过更新 Python 来消灭您的 Python 2.7 环境。您可以创建并激活一个名为“snakes”(或任何您想要的)的新环境,并安装最新版本的 Python 3,如下所示:
conda create --name snakes python=3
它比听起来简单,看看这里的介绍页面:Anaconda 入门
然后要处理同时运行版本 2.x 和 3.x 的具体问题,请参阅:
据我所知,Python 使用 PATH 变量而不是注册表设置从命令行运行。
因此,如果您指向 PATH 上的正确版本,您将使用它。请记住重新启动命令提示符以使用新的 PATH 设置。
Python 安装通常将.py
,.pyw
和.pyc
files 与 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 启动器”博客文章。
正如 2011 年 Standalone 首次亮相时所承诺的那样,Windows的Python 启动器自 3.3 版开始嵌入到 Python 中:
您应该确保 PATH 环境变量不包含两个 python.exe 文件(添加您当前用于日常运行脚本的那个),或者按照 Kniht 建议的批处理文件进行操作。除此之外,我不明白为什么不这样做。
PS:我安装了 2.6 作为我的“主要” python 和 3.0 作为我的“播放” python。2.6 包含在PATH中。一切正常。
我认为有一个选项可以在安装程序中为 .py 文件设置 Windows 文件关联。取消选中它,你应该没问题。
如果没有,您可以轻松地将 .py 文件与以前的版本重新关联。最简单的方法是右键单击 .py 文件,选择“打开方式”/“选择程序”。在出现的对话框中,选择或浏览到您要默认使用的 python 版本,并选中“始终使用此程序打开此类文件”复选框。
我有同样的问题,我想在大多数工作中使用 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,当我想要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 文件和其他文件执行相同的操作。
干杯!
我会假设是这样,我在同一台计算机上并排安装了 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 应用程序固定到您的任务栏,或创建桌面快捷方式,您就完成了!
为 3.x 打开适用于 Windows 的 Python
为 python 2.x 打开 Powershell
我希望这有帮助!
嗯..我现在通过在https://www.python.org/downloads/release/python-365/下载适用于 Windows 的 Python 3.6.5并确保安装了启动器来做到这一点。然后,我按照使用 python 2 和 python 3 的说明进行操作。重新启动命令提示符,然后使用py -2.7
to use Python 2 and py
or py -3.6
to use Python 3。您也可以使用pip2
for Python 2'spip
和pip
for Python 3's pip
。