python - 当提到(未调用)内置退出函数时,为什么 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 运行可执行文件时,不会显示任何错误。
解决方案
推荐阅读
- java - Enum 类如何扩展另一个外部库类?
- arrays - 图像滑块中的数组问题
- c# - 为什么会出现 Exception FatalExecutionEngineError 这个代码
- python - 在我的 crud 操作中创建一个撤消按钮
- node.js - 有没有办法在 aws-sdk 或 aws-amplify 中不提供密码的情况下为用户生成 cognito auth 令牌?
- sql - 使用批量postgres将数据从表复制到表?
- node.js - 如何使用 server.js 获取应用程序的路径
- entity-framework-core - Entity Framework Core 查询总是返回缓存的数据
- ruby-on-rails - Ruby on Rails 不同页面上的多个更新表单
- javascript - 将 jest 与 tensorflow.js 一起使用会产生错误