Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

py2exe error handling redirection and popup

Tags:

python

py2exe

Been trying to figure out how to get py2exe to handle errors more gracefully. There are basically 2 weird things happening:

1) Popup message after shutting down the program => want to suppress (not show up) this popup

  • Use try/except => doesn't work
    • http://osdir.com/ml/python.py2exe/2006-09/msg00016.html
    • Not sure where to put this code

2) Log file getting created in c:\Program Files\AppName\AppName.exe.log (sometimes has permission errors writing to this folder) => redirect log to c:\ProgramData

  • Use sys.stdout and sys.stderr => doesn't work
    • http://www.dreamincode.net/forums/topic/234318-py2exe-do-not-show-errors-occurred-prompt-to-user/
    • Not sure where to put this code

I'm thinking that I may just be putting the code in the wrong spot and the py2exe bootstrap code is firing AFTER I've set these up but I'm not sure. I've tried putting this code right before the error log is generate but it still goes to where py2exe is bootstrapping them to (the StdErr objects)


The structure of my program is as follows

src/
  python/
    gui/
      __main__.py

main.py

if __name__ == "__main__":
    # Redirect py2exe log to somewhere else if windows
    if hasattr(sys,"frozen") and sys.frozen in ("windows_exe", "console_exe"):
        stdout_file = "c:\ProgramData\AppName\out.log"
        stderr_file = "c:\ProgramData\AppName\err.log"
        sys.stdout = open(stdout_file, "w")
        sys.stderr = open(stderr_file, "w")
    try:
        gui = AppNameGui()
        gui.main()
    except:
        traceback.print_exc()
like image 605
Chris Go Avatar asked Sep 16 '13 19:09

Chris Go


2 Answers

It is old post but still, someone could find it handy. You can disable this annoying popup window by set logger propagation

logger.propagate = False

The reason is that logger is not propagate output to console. For more details check source code in py2exe package:

py2exe\boot_common.py
like image 145
Michal Gonda Avatar answered Nov 14 '22 12:11

Michal Gonda


I had problems where one of the imports was going wrong right at the top of my file. I had to put the stdout/stderr redirection right at the top of the file to ensure the logs didn't get created the way py2exe wanted to.

like image 1
Martin Thompson Avatar answered Nov 14 '22 12:11

Martin Thompson