【问题标题】:How to abbreviate traceback in Jupyter Notebook?如何在 Jupyter Notebook 中缩写回溯?
【发布时间】:2021-06-25 08:06:38
【问题描述】:

我使用 Jupyter Notebook 记录了一个 XML-API,因此文档和规范不能分开。

这很好用。

由于 API 还必须处理无效输入,Jupyter Notebook 会正确显示回溯。

回溯非常冗长 - 我想缩写/缩短它 - 理想情况下,应该只显示最后一行。

请求

server.get_licenses("not-existing-id")

Jupyter Notebook 中的当前打印输出

---------------------------------------------------------------------------
Fault                                     Traceback (most recent call last)
<ipython-input-5-366cceb6869e> in <module>
----> 1 server.get_licenses("not-existing-id")

/usr/lib/python3.9/xmlrpc/client.py in __call__(self, *args)
   1114         return _Method(self.__send, "%s.%s" % (self.__name, name))
   1115     def __call__(self, *args):
-> 1116         return self.__send(self.__name, args)
   1117 
   1118 ##

/usr/lib/python3.9/xmlrpc/client.py in __request(self, methodname, params)
   1456                         allow_none=self.__allow_none).encode(self.__encoding, 'xmlcharrefreplace')
   1457 
-> 1458         response = self.__transport.request(
   1459             self.__host,
   1460             self.__handler,

/usr/lib/python3.9/xmlrpc/client.py in request(self, host, handler, request_body, verbose)
   1158         for i in (0, 1):
   1159             try:
-> 1160                 return self.single_request(host, handler, request_body, verbose)
   1161             except http.client.RemoteDisconnected:
   1162                 if i:

/usr/lib/python3.9/xmlrpc/client.py in single_request(self, host, handler, request_body, verbose)
   1174             if resp.status == 200:
   1175                 self.verbose = verbose
-> 1176                 return self.parse_response(resp)
   1177 
   1178         except Fault:

/usr/lib/python3.9/xmlrpc/client.py in parse_response(self, response)
   1346         p.close()
   1347 
-> 1348         return u.close()
   1349 
   1350 ##

/usr/lib/python3.9/xmlrpc/client.py in close(self)
    660             raise ResponseError()
    661         if self._type == "fault":
--> 662             raise Fault(**self._stack[0])
    663         return tuple(self._stack)
    664 

Fault: <Fault 1: 'company id is not valid'>

我的愿望输出

Fault: <Fault 1: 'company id is not valid'>

【问题讨论】:

  • 我认为this 可能会有所帮助。他们建议使用 try/except 模式,如果出现错误,请获取回溯以查看它是什么并做出相应的响应。
  • 嗨@MarcosViníciusPetri - 感谢您的回答 - 尝试/例外模式肯定会成功。我想知道为什么我没有考虑过:-)你能写一个答案让我投票吗?无论如何,我会等几天,也许会有另一个答案。我敢打赌 Python 的异常处理有一个钩子/自定义。

标签: python jupyter-notebook traceback


【解决方案1】:

事实证明,这是 iPython 内置的,因此您无需安装或更新任何东西。

只需在笔记本顶部放置一个单元格,然后运行 ​​%xmode Minimal 作为唯一输入。您还可以查看%xmode? 的文档或%quickref 的许多其他“魔术方法”文档。

【讨论】:

【解决方案2】:

以下解决方案,使用 sys.excepthook 在 REPL 中工作...

代码

import sys

def my_exc_handler(type, value, traceback):
    print(repr(value), file=sys.stderr)
    
sys.excepthook = my_exc_handler

1 / 0

重击

❯ python3.9 main.py 
ZeroDivisionError('division by zero')

...但不幸的是,不在 Jupyter Notebook 中 - 我仍然可以获得完整的回溯。

当我查看 Python 的文档时...

当异常被引发但未被捕获时

...也许“未捕获”是问题所在。当我不得不猜测时,我认为 Jupyter Notebook 会捕获所有异常,并自行进行格式化和打印。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 2017-12-26
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 2019-07-09
    • 2018-07-17
    • 1970-01-01
    相关资源
    最近更新 更多