首页 > 解决方案 > 当提到(未调用)内置退出函数时,为什么 PyInstaller 可执行文件会终止?

问题描述

在下面的代码中,名为的字典列表options包含对quit内置函数的提及。在通过对用户输入的检查之前,不会调用该函数。它的名称只是存储在该列表中,以供以后需要时使用。

def my_function():
    input("my_function successfully executed, input anything to end program> ")

def choose_from_menu():

    options =   [
                {"description":". Execute my_function.",
            "method":my_function},
                {"description":". Terminate program.",
            "method":quit}
                ]

    for i in options:
        print(str(options.index(i) + 1) + i["description"])

    chosen = input("Choose an option. > ")

    for i in options:
        if chosen == str(options.index(i) + 1):
            # here it gets called
            i["method"]()

choose_from_menu()

使用 Python 解释器运行,脚本让用户从打印的列表中进行选择,然后从中选择相关方法options然后调用。

但是,当这个脚本使用 PyInstaller(在我的机器上,在 Windows 上)转换为可执行文件并运行时,它似乎在看到选项列表中提到退出函数的那一刻终止,因此从一开始就没有到达输入提示. (值得注意的是,它是通过退出来实现的,但不是通过 my_function 实现的。)

为什么?

我尝试定义自己的包含退出调用的方法,并将该名称存储在字典中。它确实绕过了这种情况。然而,这样做并没有让我明白解释器和编译器之间这种对比的原因。

使用 PowerShell 的 Start-Process commandlet 运行可执行文件时,不会显示任何错误。

标签: pythonpyinstallerbuilt-in

解决方案


推荐阅读