[Solved-4 Solutions] Starting python debugger automatically on error
Error Description:
Starting python debugger automatically on error
Solution 1:
- We can use traceback.print_exc to print the exceptions traceback.
- Then use sys.exc_info to extract the traceback and finally call pdb.post_mortem with that traceback
- If we want to start an interactive command line with code.interact using the locals of the frame where the exception originated we can do
Solution 2:
- When Python runs a script and an uncatched exception is raised, a traceback is printed and the script is terminated.
- Python2.1 has introduced sys.excepthook, which can be used to override the handling of uncaught exceptions.
- This allows to automatically start the debugger on an unexpected exception, even if python is not running in interactive mode.
- Code:
- The above code should be included in 'sitecustomize.py', which is automatically imported by python.
- The debugger is only started when python is run in non-interactive mode.
Solution 3:
- If we don't provide the
-c continue
flag then we'll need to enter 'c' (for Continue) when execution begins. Then it will run to the error point and give us control there.
Solution 4:
- We just checked python -?, and if we use the -i command we can interact where our script stopped.
- So given this script:
- We can get this output!
Learn python - python tutorial - python modes debugging code type - python examples - python programs