In the below code, the list of dictionaries named options
contains mention of the quit
built-in function. The function is not called until it passes a check against user input. Its name is just stored in that list for when it's needed later.
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()
Run with the Python interpreter, the script lets the user choose from the printed list, and the correlating method gets picked from options
and then called.
But when this script is turned into an executable with PyInstaller (on my machine, on Windows) and run, it seems to terminate the moment it sees mention of the quit function inside the options list, therefore never reaching the input prompt in the first place. (Notably, it did this with quit, but not with my_function.)
Why?
I tried defining my own method that contains a quit call, and storing that name in the dictionary instead. It does bypass this situation. However, doing that doesn't enlighten me as to the reason for this contrast between interpreter and compiler.
When the executable is run with PowerShell's Start-Process commandlet, no error is displayed.