我正在我们的开发商店中运行 Python 应用程序。它是从另一个 GUI 应用程序调用的。当它失败时,我希望在 pdb 事后调试中它停止的地方出现一个控制台,这样我就可以走过去看看当我们的用户遇到问题时发生了什么。
我尝试在程序顶部设置异常钩子:
def pcs_debugger(type, value, tb):
traceback.print_exception(type, value, tb)
pdb.pm()
sys.excepthook = pcs_debugger
这很好用,除非我还没有控制台,比如当我用 pythonw 启动它或从这个不是用 Python 编写的其他 GUI 应用程序调用它时。
有没有办法做到这一点?谢谢
更新:我忘了提到这一切都在 Windows 7 上
更新:添加最小代码示例。请注意,如果我使用 python.exe 而不是 pythonw.exe 启动它,这将按照我想要的方式工作,并且 pythonw 更类似于我在我的环境中所做的事情,我实际上有一个 C# GUI 加载这个来自 dll 的 Python 代码。
import pdb, sys, traceback, wx
def my_debugger(type, value, tb):
traceback.print_exception(type, value, tb)
pdb.pm()
sys.excepthook = my_debugger
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame("Hello World", (50, 60), (450, 340))
frame.Show()
self.SetTopWindow(frame)
return True
class MyFrame(wx.Frame):
def __init__(self, title, pos, size):
wx.Frame.__init__(self, None, -1, title, pos, size)
menuFile = wx.Menu()
menuFile.Append(1, "&About...")
menuFile.AppendSeparator()
menuFile.Append(2, "E&xit")
menuFile.Append(3, "&Fail")
menuBar = wx.MenuBar()
menuBar.Append(menuFile, "&File")
self.SetMenuBar(menuBar)
self.CreateStatusBar()
self.SetStatusText("Welcome to wxPython!")
self.Bind(wx.EVT_MENU, self.OnAbout, id=1)
self.Bind(wx.EVT_MENU, self.OnQuit, id=2)
self.Bind(wx.EVT_MENU, self.OnFail, id=3)
def OnQuit(self, event):
self.Close()
def OnAbout(self, event):
wx.MessageBox("This is a wxPython Hello World Sample",
"About Hello World", wx.OK | wx.ICON_INFORMATION, self)
def OnFail(self, event):
a = 1
b = 0
self.SetStatusText('about to divide by 0')
c = a / b
print 'here it is {}'.format(c)
return
if __name__ == '__main__':
app = MyApp(False)
app.MainLoop()