cgitb is a valuable debugging tool in the standard library. It was originally designed for showing errors and debugging information in web applications and was later updated to include plain-text output as well, but unfortunately was never renamed. This has led to obscurity, and the module is not used as often as it could be.

Standard Traceback Dumps


In [1]:
def func2(a, divisor):
    return a/divisor

def func1(a, b):
    c = b -5
    return func2(a, c)
func1(1,5)


---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-1-e2832237a4e0> in <module>()
      5     c = b -5
      6     return func2(a, c)
----> 7 func1(1,5)

<ipython-input-1-e2832237a4e0> in func1(a, b)
      4 def func1(a, b):
      5     c = b -5
----> 6     return func2(a, c)
      7 func1(1,5)

<ipython-input-1-e2832237a4e0> in func2(a, divisor)
      1 def func2(a, divisor):
----> 2     return a/divisor
      3 
      4 def func1(a, b):
      5     c = b -5

ZeroDivisionError: division by zero

Enabled Detailed Tracebacks


In [2]:
import cgitb
cgitb.enable(format='txt')
def func2(a, divisor):
    return a/divisor

def func1(a, b):
    c = b -5
    return func2(a, c)
func1(1,5)


---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-2-2ed08b453103> in <module>()
      7     c = b -5
      8     return func2(a, c)
----> 9 func1(1,5)

<ipython-input-2-2ed08b453103> in func1(a, b)
      6 def func1(a, b):
      7     c = b -5
----> 8     return func2(a, c)
      9 func1(1,5)

<ipython-input-2-2ed08b453103> in func2(a, divisor)
      2 cgitb.enable(format='txt')
      3 def func2(a, divisor):
----> 4     return a/divisor
      5 
      6 def func1(a, b):

ZeroDivisionError: division by zero

In [ ]: