简体   繁体   English

py2exe错误处理重定向和弹出窗口

[英]py2exe error handling redirection and popup

Been trying to figure out how to get py2exe to handle errors more gracefully. 一直试图弄清楚如何让py2exe更优雅地处理错误。 There are basically 2 weird things happening: 基本上发生了两件奇怪的事情:

1) Popup message after shutting down the program => want to suppress (not show up) this popup 1)关闭程序后弹出消息=>想要抑制(不显示)此弹出窗口

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 2)在c:\\ Program Files \\ AppName \\ AppName.exe.log中创建日志文件(有时写入此文件夹有权限错误)=>将日志重定向到c:\\ ProgramData

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. 我想我可能只是将代码放在错误的位置并且py2exe引导程序代码在我设置它们之后触发但我不确定。 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) 我已经尝试在生成错误日志之前正确放置此代码,但它仍然会转到py2exe引导它们到的地方(StdErr对象)


The structure of my program is as follows 我的程序结构如下

src/
  python/
    gui/
      __main__.py

main .py .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()

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包中的源代码:

py2exe\boot_common.py

I had problems where one of the import s was going wrong right at the top of my file. 我有一个问题,其中一个import错误就在我的文件顶部。 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. 我必须将stdout / stderr重定向放在文件的顶部,以确保日志没有像py2exe那样创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM