Any way of achieving the same thing as python -mpdb from inside the script?
Besides wrapping all your code in try
except
, is there any way of achieving the same thing as running开发者_如何学JAVA your script like python -mpdb script
? I'd like to be able to see what went wrong when an exception gets raised.
If you do not want to modify the source then yOu could run it from ipython - an enhanced interactive python shell.
e.g. run ipython then execute %pdb on
to enable post-mortem debugging. %run scriptname
will then run the script and automatically enter the debugger on any uncaught exceptions.
Alternatively %run -d scriptname
will start the script in the debugger.
python -i script
will leave you in the interactive shell when an exception gets raised; then
import pdb
pdb.pm()
will put you into the post-mortem debugger so you can do all the usual debugging things.
This should work as long as your script does not call sys.exit. (Which scripts should never do, because it breaks this very useful technique! as well as making them harder to write tests for.)
import pdb; pdb.set_trace()
Source: http://docs.python.org/library/pdb.html
精彩评论