py2exe error handling redirection and popup

笑着哭i 提交于 2019-12-07 03:40:01

问题


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()

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/18835641/py2exe-error-handling-redirection-and-popup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!