有什么方法可以从脚本内部实现与 python -mpdb 相同的功能吗?

发布于 2024-08-23 04:10:36 字数 137 浏览 3 评论 0原文

除了将所有代码包装在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对风讲故事 2024-08-30 04:10:36

如果您不想修改源代码,那么您可以从 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.

追风人 2024-08-30 04:10:36
python -i script

当出现异常时,会让您留在交互式 shell 中;然后

import pdb
pdb.pm()

会将您带入事后调试器,以便您可以执行所有常见的调试操作。

只要您的脚本不调用 sys.exit,这应该就可以工作。 (哪些脚本不应该这样做,因为它破坏了这个非常有用的技术!并且使它们更难编写测试。)

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

伊面 2024-08-30 04:10:36

<代码>导入pdb; pdb.set_trace()

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

import pdb; pdb.set_trace()

Source: http://docs.python.org/library/pdb.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文