2017-09-14 25 views
0

JupiterノートブックのPythonコードでトレースバックを隠したいので、エラーの種類とメッセージだけが表示されます。Jupyterでトレースバックを抑制するにはどうすればよいですか?

This answersys.tracebacklimit = 0ことを示唆しているが、それは次与えしよう:また、カスタム関数でsys.excepthookを置き換える提案したが、トレースバックはまだ表示されていた答え

 
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 

トレースバックを非表示にするにはどうすればよいですか?

答えて

0

私はIPythonをmonkeypatchingすることを含む、これを行うためのいくつかの方法を見つけました。

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

ipython.showtraceback = hide_traceback 
関連する問題