繁体   English   中英

如何在 Jupyter 中抑制回溯?

[英]How do I suppress tracebacks in Jupyter?

我想在 Jupyter 笔记本中的 Python 代码中隐藏回溯,因此只显示错误类型和消息。

这个答案表明sys.tracebacklimit = 0但尝试给出以下结果:

ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.

Traceback (most recent call last): 
AssertionError 
Traceback (most recent call last): 
AssertionError

该答案还建议用自定义函数替换sys.excepthook ,但仍显示回溯。

如何隐藏回溯?

我找到了几种方法来做到这一点,都涉及到monkeypatching IPython。

#1. 这将仅输出异常类型和消息,但在输出区域中以红色突出显示:

from __future__ import print_function  # for python 2 compatibility
import sys
ipython = get_ipython()

def exception_handler(exception_type, exception, traceback):
    print("%s: %s" % (exception_type.__name__, exception), file=sys.stderr)

ipython._showtraceback = exception_handler

#2. 这将输出异常和异常类型的颜色代码(就像 Jupyter 通常所做的那样,但没有回溯):

import sys
ipython = get_ipython()

def hide_traceback(exc_tuple=None, filename=None, tb_offset=None,
                   exception_only=False, running_compiled_code=False):
    etype, value, tb = sys.exc_info()
    value.__cause__ = None  # suppress chained exceptions
    return ipython._showtraceback(etype, value, ipython.InteractiveTB.get_exception_only(etype, value))

ipython.showtraceback = hide_traceback

关于什么

import functools
ipython = get_ipython()
method_name = "showtraceback"
setattr(
    ipython,
    method_name,
    functools.partial(
        getattr(ipython, method_name),
        exception_only=True
    )
)

我认为xmode魔法正是你在这里寻找的。 只需在单元格中键入它。 有四种模式:Context、Minimal、Verbose 和 Plain(我认为是默认值)。 您可以使用xmode <mode>或不带参数切换到下一个模式。

In [1]: %xmode
Exception reporting mode: Minimal

In [2]: %xmode
Exception reporting mode: Plain

这是一个简单错误的区别。 通过更详细的错误消息可以更容易地查看差异。

xmode Minimal

x = 6 / 0

返回

ZeroDivisionError: division by zero

xmode plain

x = 6 / 0


Traceback (most recent call last):                                                                            
File "<ipython-input-187-28f66aec0cca>", line 2, in <module>
x = 6/0

ZeroDivisionError: division by zero

xmode Context

---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-188-8224aeca2117> in <module>
      1 get_ipython().run_line_magic('xmode', 'Context')
----> 2 x = 6/0

ZeroDivisionError: division by zero

xmode vErboSe

---------------------------------------------------------------------------
    ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-189-8fc1397d2e40> in <module>
      1 get_ipython().run_line_magic('xmode', 'Verbose')
----> 2 x = 6/0
        global x = ['a', 'b', 'c', 'd']

ZeroDivisionError: division by zero

我意识到很久以前就有人问过这个问题,但我希望其他人可能会在这里登陆,并且在永远搜索堆栈交换并且从未发布之后,我想我会回馈。 我期待一些礼仪之火。 温柔点,下次我会知道的更好!

暂无
暂无

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

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