Debug code with IPython

Rui LA trick learned from the author of Zhihu.

Should have used IPython, right? Imagine being automatically taken to the IPython Shell when an exception is thrown? And unlike ordinary IPython, commands such as p (print), up (up stack), down (down stack) can be called at this time.

You can also create temporary variables and execute arbitrary functions. In fact this is possible and very simple.

First install ipython, then save the following code ascrash_on_ipy.py

import sys

class ExceptionHook:
    instance = None

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

sys.excepthook = ExceptionHook()

Then import crash_on_ipy somewhere in your project code.

Rendering:


Please indicate the author and source ( reposkeeper ) for reprinting, please do not use for any commercial purposes!

Follow the WeChat public account to get the push of new articles!
qrcode

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324824449&siteId=291194637