在 Window OS 中双击时如何 运行 空闲 shell 中的脚本?

How to run a script in Idle shell when double-clicked in Window OS?

总结:

Windows 用户在 his/her 机器上安装了 python,双击 (.py) 或 (.bat) 文件并启动 myapp.pyPython 空闲 Shell.

或者,myapp.py 可以在 Python 空闲 Shell 双击后自行启动,但这可能不可行.

比较散漫(防止误会):

如果有人想通过 Python 空闲 Shell 运行 一个 (.py) 文件,他们会右键单击文件 > 'Edit with IDLE' > 'Run Module (F5)'。如何通过 Python 空闲 Shell 运行 (.py) 文件,只需双击 (.py) 文件或 (.py) bat)文件?

附加信息

OS: Windows 7 (x64)

Python: 2.7

如果 Python 在您的 windows 路径中,一种通过批处理脚本 (.bat) 在 Python 中解释/“执行”myapp.py 的方法可能是:

Starter.bat :

@echo off
title “MyApp Launcher”
python2 myapp.py
pause
exit

不过它并没有专门使用IDLE应用程序! 此外,.bat 必须位于与 myapp.py .

相同的文件夹中(或者两个元素之间的路径应固定)

注意:根据您安装的 Python,如果它不起作用,请尝试将 python2 替换为 pythonpy

正如我在评论中所说,来自 Command line usage section of the Python 2 Idle documentation it sounded like you could do it in a batch file by starting idle and passing it the -r file option to get it to run your script. (Note for Python 3, here's the Command line usage section。)

今天我有机会自己进一步探索这个问题,并且可以报告说它确实有效。所以现在这就是我更具体的意思。

以下批处理文件是我在安装了 32 位版本 Python 2.7.14 的 Windows 7 (x64) 系统上使用的.您可能需要调整其中使用的文件路径以适合您自己的配置。

idletest.bat

@echo on
python "C:\Python\Lib\idlelib\idle.py" -r "idletest.py"
pause