I've recently discovered the very useful '-i' flag to Python
-i : inspect interactively after running script, (also PYTHONINSPECT=x)
and force prompts, even if stdin does not appear to be a terminal
this is great for inspecting objects in the global scope, but what happens if the exception was raised in a function call, and I'd like to inspect the local variables of the function? Naturally, I'm interested in the scope of where the exception was first raised, is there any way to get to it?
At the interactive prompt, immediately type
>>> import pdb
>>> pdb.pm()
pdb.pm() is the "post-mortem" debugger. It will put you at the scope where the exception was raised, and then you can use the usual pdb commands.
I use this all the time. It's part of the standard library (no ipython necessary) and doesn't require editing debugging commands into your source code.
The only trick is to remember to do it right away; if you type any other commands first, you'll lose the scope where the exception occurred.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With