[英]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
? 除了将所有代码包装在
try
except
,是否有任何方法可以实现与运行脚本相同的功能,如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. 如果您不想修改源代码,那么yOu可以从ipython运行它 - 一个增强的交互式python shell。
eg run ipython then execute %pdb on
to enable post-mortem debugging. 例如,运行ipython然后执行
%pdb on
以启用事后调试。 %run scriptname
will then run the script and automatically enter the debugger on any uncaught exceptions. 然后,
%run scriptname
将运行该脚本并自动在任何未捕获的异常上输入调试器。
Alternatively %run -d scriptname
will start the script in the debugger. 或者
%run -d scriptname
将在调试器中启动脚本。
python -i script
will leave you in the interactive shell when an exception gets raised; 当异常被引发时,你会留在交互式shell中; 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. 只要您的脚本不调用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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.