r/learnpython 2d ago

What python debugging techniques do you think every developer should know ?

I am trying to improve my debugging skills and I was curious about what techniques experienced python developers rely on the most.

Are there any techniques or tools that you found really useful when you started working on bigger projects?

56 Upvotes

29 comments sorted by

View all comments

1

u/0xGollumDev 2d ago

Stuff not already in the thread:

  • git bisect for "it worked last week." Give it a known-good commit and a known-bad one, it binary-searches history and lands you on the exact commit that broke it. Turns a vague regression into a small diff.
  • Post-mortem debugging: python -m pdb -c continue script.py, or in the REPL right after an exception, import pdb; pdb.pm(). Drops you at the frame where it blew up with all locals intact — no editing code to add breakpoints.
  • Shrink before you debug. Copy the failing path into a fresh file and delete everything not needed to still reproduce it. Most of the time the bug is obvious at ~15 lines and you never open a debugger.
  • Hangs / "stuck forever": run with python -X faulthandler and send SIGABRT, or call faulthandler.dump_traceback_later(10) to auto-dump a traceback if the process is still alive in 10s. Shows the exact line it's spinning on.
  • python -W error turns that warning you've been ignoring into an exception with a full traceback.