如何在事后调试时退出ipdb?

2024-10-02 06:27:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

$ python3 -m pdb my_script.py

这会将我放入pdb提示符中,在那里我可以c继续执行,当它遇到错误时,我可以检查变量,然后q退出脚本执行以返回shell。

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

$ python3 -m ipdb my_script.py

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

$ 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

如何退出此调试器?


Tags: orpymystepexceptionscriptpostwill
3条回答

使用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,但他们的建议只会将进程发送到后台(它仍然存在,消耗内存)。你需要做以上这些才能真正扼杀这个过程

正如用户@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退出调试器。

相关问题 更多 >

    热门问题