简体   繁体   English

如何在 Jupyter Lab/Notebook 中隐藏错误回溯?

[英]How to hide the error traceback in Jupyter Lab/Notebook?

My goal is to have only a minimal output from pythons exception handler (without the traceback).我的目标是从 python 异常处理程序中获得最小的 output (没有回溯)。 In a plain python script I can use:在一个普通的 python 脚本中,我可以使用:

sys.tracebacklimit=0

However in a notebook, this does not seem to work the way I want and the output is actually worse (longer).然而,在笔记本中,这似乎并没有按照我想要的方式工作,而且 output 实际上更糟(更长)。 Is there any quick rescue to this issue?这个问题有什么快速救援吗?

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()
    return ipython._showtraceback(etype, value, ipython.InteractiveTB.get_exception_only(etype, value))

ipython.showtraceback = hide_traceback

Above code will print only exception and color code the exception type without trace back.上面的代码将只打印异常和颜色代码异常类型而没有回溯。 Is this what you are looking for.这是你想要的。 Paste it in the 1st cell of your notebook and try to run it将其粘贴到笔记本的第一个单元格中并尝试运行它

Or better yet:或者更好:

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

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

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