2

My largest issue is the fancy IPython exceptions. I want them look like normal python exceptions, but when I try to reset sys.excepthook it does not work:

In [31]: import sys; sys.excepthook = sys.__excepthook__; (sys.excepthook, sys.__excepthook__)
Out[31]: 
(<bound method TerminalInteractiveShell.excepthook of <IPython.terminal.interactiveshell.TerminalInteractiveShell object at 0x27c8e10>>,
 <function sys.excepthook>)
fakedrake
  • 6,528
  • 8
  • 41
  • 64

1 Answers1

6

IPython replaces sys.excepthook each time you execute a line of code, rendering it pointless to change it each time. In addition to this, IPython catches all exceptions and handles them itself, without having to invoke sys.excepthook.

This answer to a related question provides a way of overriding this behaviour: Basically, you've to override Ipython's showtraceback function with one that will format and display your exception the way you want it.

def showtraceback(self):
    traceback_lines = traceback.format_exception(*sys.exc_info())
    del traceback_lines[1]
    message = ''.join(traceback_lines)
    sys.stderr.write(message)

import sys
import traceback
import IPython
IPython.core.interactiveshell.InteractiveShell.showtraceback = showtraceback
cs95
  • 379,657
  • 97
  • 704
  • 746