How to format IPython starck trace. (IPythonのスタックトレースを見やすくする)

Problem

IPython is absolutely great tool to create Python code interactively, but when I use complicated libraries like pandas or scipy, its stack trace gets very very long and it's hard to find where was the original mistake, thanks to the verbose rich texts explaining errors.

Moreover, although there are several libraries which can format the stack traces, they don't work with IPython, because IPython skip standard sys.excepthook.
https://stackoverflow.com/questions/1261668/cannot-override-sys-excepthook

Here is how to make it work, using one of those formatting tool TBVaccine (https://github.com/skorokithakis/tbvaccine).
I wrote following at the python initial code(script set for $PYTHONSTARTUP).
try:
  import tbvaccine
  kargs = {"code_dir":os.getenv('PWD'), "isolate":True, "show_vars":False}
  tbvaccine.add_hook(**kargs)

  if get_ipython(): # IPYTHON
    tbv = tbvaccine.TBVaccine(**kargs)
    def f(*args, **argv): tbv.print_exception(*sys.exc_info())
    get_ipython().showtraceback = f
except Exception as e:
  pass
this makes

into this


Comments

Popular posts from this blog

Subclassing and Signal connect on a same widget crashes PySide application on exit.

Calling OpenCV functions via Cython from Python 3.X.

Showing CPU/Memory usage on tmux status bar(tmuxのステータスバーにCPUとMemoryの使用状況を表示する)