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>)
-
It's readonly, I believe.coldspeed95– coldspeed952017年06月23日 12:42:43 +00:00Commented Jun 23, 2017 at 12:42
-
Hmm, couldn't find anywhere in the docs anything indicating that... Is there no indirect way to change it?fakedrake– fakedrake2017年06月23日 12:46:49 +00:00Commented Jun 23, 2017 at 12:46
1 Answer 1
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