py2exe 错误处理重定向和弹出 [英] py2exe error handling redirection and popup

查看:47
本文介绍了py2exe 错误处理重定向和弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直在想办法让 py2exe 更优雅地处理错误.基本上有两件奇怪的事情发生:

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

1) 关闭程序后弹出消息 => 想抑制(不显示)这个弹出窗口

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

  • 使用 try/except => 不起作用

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

    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

    • 使用 sys.stdout 和 sys.stderr => 不起作用

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

      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)

      我的程序结构如下

      src/
        python/
          gui/
            __main__.py
      

      ma​​in.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
      

      原因是记录器没有将输出传播到控制台.更多细节请查看 py2exe 包中的源代码:

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

      py2exe\boot_common.py
      

      这篇关于py2exe 错误处理重定向和弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆