Today I learned about postmortem debugging in Python.

3 ways to add postmortem debugging into python worflow are:

  • Running the python script using pdb directly. PDB will create a breakpoint when the problem will fail.
python -m pdb script.py
  • Using pdb postmortem inside an except statement. PDB will drop us a breakpoint in the line which raised the exception.
try:
    something()
except Exception:
    import pdb(); pdf.post_mortem()
    handle_exception()
  • Using pdb postmortem with a test runner (Pytest). if the test fails, PDB will start.
pytest test.py --pdb

More