有什么方法可以从脚本内部实现与 python -mpdb 相同的功能吗?
除了将所有代码包装在 try
except
中之外,是否有任何方法可以实现与运行 python -mpdb 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不想修改源代码,那么您可以从 ipython 运行它 - 一个增强的交互式 python壳。
例如,运行 ipython 然后执行
%pdb on
以启用事后调试。然后,%run scriptname
将运行脚本并在出现任何未捕获的异常时自动进入调试器。或者
%run -d scriptname
将在调试器中启动脚本。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.当出现异常时,会让您留在交互式 shell 中;然后
会将您带入事后调试器,以便您可以执行所有常见的调试操作。
只要您的脚本不调用 sys.exit,这应该就可以工作。 (哪些脚本不应该这样做,因为它破坏了这个非常有用的技术!并且使它们更难编写测试。)
will leave you in the interactive shell when an exception gets raised; then
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.)
<代码>导入pdb; pdb.set_trace()
来源:http://docs.python.org/library /pdb.html
import pdb; pdb.set_trace()
Source: http://docs.python.org/library/pdb.html