Launch an IPython shell on exception

前端 未结 10 2354
长情又很酷
长情又很酷 2020-12-04 09:30

Is there a way to launch an IPython shell or prompt when my program runs a line that raises an exception?

I\'m mostly interested in the context, variables, in the sc

10条回答
  •  -上瘾入骨i
    2020-12-04 09:53

    @Adam's works like a charm except that IPython loads a bit slowly(800ms on my machine). Here I have a trick to make the load lazy.

    class ExceptionHook:
        instance = None
    
        def __call__(self, *args, **kwargs):
            if self.instance is None:
                from IPython.core import ultratb
                self.instance = ultratb.FormattedTB(mode='Verbose',
                     color_scheme='Linux', call_pdb=1)
            return self.instance(*args, **kwargs)
    sys.excepthook = ExceptionHook()
    

    Now we don't need to wait at the very beginning. Only when the program crashes will cause IPython to be imported.

提交回复
热议问题