Starting python debugger automatically on error
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When a Python script crashes in the middle of a long run, the most useful information is the state of the program at the moment of failure. Instead of re-running the script and trying to guess where to add breakpoints, you can drop straight into the debugger as soon as an unhandled exception appears.
Use Post-Mortem Debugging with pdb
The built-in pdb module supports post-mortem debugging. That means you let the exception happen, capture its traceback, and then open an interactive debugger at the failing frame.
For one script, the clearest pattern is to wrap main() in a try block:
When the division-by-zero error occurs, Python prints the traceback and opens a pdb prompt. From there you can inspect variables with commands such as p total, p count, where, and up.
This approach is often better than adding pdb.set_trace() manually because it preserves the real failing path rather than a guessed checkpoint.
Install a Global Exception Hook for Scripts
If you want the same behavior across a script without wrapping every entry point, use sys.excepthook. Python calls it for uncaught exceptions on the main thread.
This is useful for command-line utilities because you can enable it once near startup. It also keeps the rest of the application code clean.
For ad hoc investigation, Python already ships a zero-code option:
That starts the program under the debugger immediately. It is helpful when you want step-by-step execution from the beginning, but it is slower than post-mortem debugging when the bug appears late in the run.
Make the Behavior Optional
Automatically opening a debugger is excellent in local development and annoying in production. A simple pattern is to guard it with an environment variable so you can opt in only when needed.
Run it like this:
That keeps the debugger out of normal execution while still making the feature easy to turn on during troubleshooting.
If you use IPython, there is a similar workflow with %pdb on, which enables automatic post-mortem debugging in that interactive environment.
Common Pitfalls
The biggest mistake is enabling automatic debugging in environments where no one can interact with the process. A stuck production worker waiting at a pdb prompt is worse than a normal crash because it can hold resources open and look like a hang.
Another issue is swallowing exceptions before they reach the hook. If your code has a broad except Exception block that logs and continues, sys.excepthook never runs. In those cases, call pdb.post_mortem(tb) explicitly inside the handler while you are investigating.
Threading and asynchronous execution can also change the behavior you expect. sys.excepthook is mainly for uncaught exceptions on the main thread. If a worker thread fails, you may need thread-specific error handling or framework-specific hooks.
Finally, remember that pdb is interactive text I/O. If your program runs under a service manager, inside a non-interactive container, or through a background job system, the debugger may open but be unusable.
Summary
- '
pdb.post_mortem()is the simplest way to jump into the debugger after a crash.' - Wrapping
main()intryandexceptis a clear pattern for single scripts. - '
sys.excepthooklets you enable automatic debugging for uncaught exceptions globally.' - Guard debugger activation behind an environment variable outside local development.
- Avoid relying on automatic
pdbsessions in non-interactive or production environments.
Related reading
- Static methods in Python?
- staticmethod and abc.abstractmethod Will it blend?
- ''staticmethod'' object is not callable
- Step-by-step debugging with IPython
- starting rmiregistry wont work
- Static web hosting on AWS S3 giving me 403 permission denied
- Stop pip from failing on single package when installing with requirements.txt
- Stop Tensorflow from printing to the console
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.