gpt4 book ai didi

python - 将 "Edit with IDLE"与 Python 2.6.5 脚本一起使用

转载 作者:行者123 更新时间:2023-11-28 22:00:11 25 4
gpt4 key购买 nike

我已经在 Windows 上安装了 Python 2.7.1 和 2.6.5 版本。我在环境变量中只添加了 Python 2.6.5 的路径。现在,我想使用 Python 2.6.5 运行 Python 脚本 x.py。我知道如何使用 cmd 执行此操作,但使用 IDLE 打开它并使用 Run Module 选项在其中运行会更方便。据推测,这是通过右键单击脚本,然后转到 Edit with IDLE 选项来完成的,但这会使用 Python 2.7.1 打开并运行脚本。有没有办法用 Python 2.6.5 打开和运行它?

最佳答案

Edit with IDLE 注册表中的标准命令如下:

"C:\Program Files\Python33\pythonw.exe" "C:\Program Files\Python33\Lib\idlelib\idle.pyw" -e "%1"

现在,如您所见,路径被硬编码到其中,所以它不能像那样使用不同的 Python 解释器——至少在默认情况下是这样。

然而,PEP 397为 Python 引入了一个新的 Python 启动器,可以启动基于 shebang 行的不同版本的 Python。因此,以 #!/usr/bin/env python2 开头的文件将启动当前的 Python 2 解释器,而 #!/usr/bin/env python3 将使用 Python 3 .

使用该信息,您可以为给定的 Python 版本动态启动 IDLE。例如,这将使用启动器的默认 Python 版本编辑文件:

 C:\Windows\pyw.exe -m idlelib.idle -e "%1"

这将强制使用 Python 3

 C:\Windows\pyw.exe -3 -m idlelib.idle -e "%1"

这将强制使用 Python 2:

 C:\Windows\pyw.exe -2 -m idlelib.idle -e "%1"

所以你需要做的是编写一个脚本,基本上检查 哪个 文件想要执行的 Python 版本,即通过手动解析 shebang 行(遗憾的是启动器没有给你这个信息没有实际启动脚本——还没有。我可能会启动一个 pull request 来将这样的功能纳入核心)。然后您将使用上面的命令运行 Python 2 或 Python 3 IDLE 并完成。

您只需要更改 Edit with IDLE 命令即可执行您的脚本,然后它就可以工作了。

一个非常简单的替代方法是添加另一个启动 Python 2 IDLE 的注册表项。所以你会有 Edit with IDLEEdit with IDLE (Py2) 之类的东西。

为此,只需将以下内容放入 .reg 文件并执行它:

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (Py2)]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (Py2)\command]
@="C:\\Windows\\pyw.exe -2 -m idlelib.idle -e \"%1\""

编辑:我刚刚注意到我写这篇文章主要是关于 Python 2 与 Python 3;我认为启动器随 Python 2.7 一起提供,所以它的工作原理是一样的。您只需将启动器的版本指定器调整为 -2.6-2.7 或您想要的任何版本。

关于python - 将 "Edit with IDLE"与 Python 2.6.5 脚本一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15343138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com