Python Debugging Tips

Coding a breakpoint, which activates the REPL debugger:

import pdb
pdb.set_trace()


Printing out the stack trace of an exception:

import traceback
try:
    print(variable_that_does_not_exist)
except Exception as e:
    print(traceback.format_exc())


import traceback
try:
    print(variable_that_does_not_exist)
except Exception as e:
    print(traceback.format_exc())
import traceback
try:
    print(variable_that_does_not_exist)
except Exception as e:
    print(traceback.format_exc())
import traceback
try:
    print(variable_that_does_not_exist)
except Exception as e:
    print(traceback.format_exc())

Comments