如何在 post-mortem 调试中退出 ipdb?

How to quit ipdb while in post-mortem debugging?

我喜欢使用以下方法检查 Python 脚本中的错误:

$ python3 -m pdb my_script.py

这让我进入 pdb 提示符,我可以从那里 c 继续执行,当遇到错误时,我可以检查变量,然后 q 退出脚本执行以返回到我的 shell.

我对 iPython 调试器模块进行了同样的尝试,因为它更加丰富多彩:

$ python3 -m ipdb my_script.py

但是,我无法在检查完错误后退出调试器。使用 q 退出命令只会在重新执行脚本和 post-mortem 模式之间不断切换:

$ python3 -m ipdb my_script.py
ipdb> c
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
ipdb> Inspect some variables at this point
ipdb> q
Post mortem debugger finished. The my_script.py will be restarted
ipdb> q
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
ipdb> q
Post mortem debugger finished. The my_script.py will be restarted
ipdb> q
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
ipdb> q
Post mortem debugger finished. The my_script.py will be restarted
ipdb> q
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program

如何退出这个调试器?

正如用户@ffeast 评论的那样,有 an open ipdb issue,并提出了一些解决方法。对我来说,这些效果很好:

  • ctrl+zkill %1(或任何作业编号)
  • 执行ipdb> import os; os._exit(1)

这是 IPython 5.1 中的错误。它已在 this pull request 中修复,从 IPython 5.2 及更高版本开始不再是问题。您现在可以使用 qquit()Ctrl+d 退出调试器。

使用ctrl+z或者打开第二个终端,然后查找进程(ps -ax | grep python)和终止进程.

一步一步:

  1. 访问终端:

    • 选项A:按ctrl+z
    • 选项 B:如果 您可以访问 Ubuntu GUI,请打开第二个终端(ctrl+alt+t)
    • 选项 C:如果您只能访问命令行,请访问第二个 tty(ctrl+alt+F2)
    • 选项 D:如果您正在通过 ssh 访问服务器,请从另一个终端建立新连接ssh server (使用选项 B 或 C,这样你就可以打开第二个连接来执行命令)
  2. 寻找进程ps -ax | grep python对应的pythonPID。例如,我的进程 (python my_stucked_process.py) 的进程 ID 将是 112923:

   3085 tty1     Sl+   15:53 /usr/bin/python /usr/bin/x-terminal-emulator
   112923 pts/2    Tl     0:01 python my_stucked_process.py
   113118 pts/2    S+     0:00 grep --color=auto python
  1. 终止进程kill -9 112923

@tutuDajuju 建议使用 ctrl+z 但他们的建议只会将进程发送到后台(它仍然存在消耗内存).您需要执行上述操作才能真正终止进程