简体   繁体   中英

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 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.

eg 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()

资料来源: http//docs.python.org/library/pdb.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM