0

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.

  • `quit()` is a function intended for interactive use only - I guess it doesn't even exist in the compiled version of your program. (So it's not automatically calling this function, it's failing due to a `NameError`.) `sys.exit()` would be a suitable replacement that's always available (assuming the presence of `import sys` earlier). – jasonharper Jun 16 '21 at 14:37
  • I've updated the title in accordance with this. I also updated the question with a finding, about the lack of error display in PowerShell. sys.exit worked. – longvandyke Jun 16 '21 at 14:51
  • Run that exe file through cmd in order to know whether there is an error that got produced while converting the program to exe, or is the program really ending due to quit function. – Sam Varghese Jun 16 '21 at 15:22
  • Didn't get any cmd output when running via cmd. [`quit` is added by the site module and should not be used in programs](https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used), according to a user on that link and the docs. – longvandyke Jun 16 '21 at 15:53

0 Answers0