There are two main ways to do post-mortem debugging in Python with PDB:

  1. “Caught” exceptions: use a try/catch with pdb.pm()
  2. “Uncaught” exceptions: register sys.excepthook and use pdb.post_mortem

Option 1:

try:
    1 / 0
except ArithmeticError as exc:
    import pdb
    pdb.pm()

Option 2:

import sys
import pdb
 
def auto_postmortem(typ, value, tb):
    pdb.post_mortem()
 
sys.excepthook = auto_postmortem
 
1 / 0

See also

If neither of these are enough for you… see Catching all exceptions. Danger ahead.