在这类问题上有许多不同的说法。不过,我特别希望找到一种方法来防止Python中的控制台应用程序在没有从终端(或其他控制台,如在Windows中调用的那样)调用时关闭。一个可能发生这种情况的例子是从Windows资源管理器中双击一个.py文件。
通常,我使用类似于以下代码片段的内容,但即使从现有终端调用应用程序,它也会带来操作的不幸副作用:def press_any_key():
if os.name == "nt":
os.system("pause")
atexit.register(press_any_key)
它还假设所有Windows用户都从Windows“shell”调用应用程序,并且只有Windows用户可以从现有终端以外的位置执行程序。
是否有(最好是跨平台)方法检测我的应用程序是否已从终端调用,和/或是否有必要为当前运行的实例提供“按任意键…”功能?请注意,诉诸批处理、bash或任何其他“包装器进程”解决方案是非常不可取的。
更新0
使用下面的Alex Martelli's答案,我生成了此函数:def register_pause_before_closing_console():
import atexit, os
if os.name == 'nt':
from win32api import GetConsoleTitle
if not GetConsoleTitle().startswith(os.environ["COMSPEC"]):
atexit.register(lambda: os.system("pause"))
if __name__ == '__main__':
register_pause_before_closing_console()
如果出现其他合适的答案,我将为其他平台和桌面环境追加更多代码。
更新1
在使用pywin32的过程中,我生成了这个函数,它在上面的函数的基础上进行了改进,使用了公认的答案。注释掉的代码是源于Update0的另一个实现。如果使用pywin32不是一个选项,请遵循accepted answer中的链接。暂停或getch()品尝。def _current_process_owns_console():
#import os, win32api
#return not win32api.GetConsoleTitle().startswith(os.environ["COMSPEC"])
import win32console, win32process
conswnd = win32console.GetConsoleWindow()
wndpid = win32process.GetWindowThreadProcessId(conswnd)[1]
curpid = win32process.GetCurrentProcessId()
return curpid == wndpid
def register_pause_before_closing_console():
import atexit, os, pdb
if os.name == 'nt':
if _current_process_owns_console():
atexit.register(lambda: os.system("pause"))
if __name__ == '__main__':
register_pause_before_closing_console()