在 Python 调试器 pdb 中,如何在不终止调试会话的情况下退出交互模式

In the Python debugger pdb, how do you exit interactive mode without terminating the debugging session

使用 python 3.5.1

当我 运行 使用 python 调试器模块的脚本时:

  [home]# python -m pdb myscript.py

这将启动调试会话:

  > /somepath/to/myscript.py(1)<module>()
  -> import os
  (Pdb) 

如果我想从调试会话中进入交互式终端,我可以发出 interact 命令:

(Pdb) interact
*interactive*
>>>

现在我可以像在 运行ning python 交互模式中一样与代码交互,可以访问脚本范围内的任何函数或变量 运行ning在我进入 interact 模式时的调试器中。

当我发出退出交互模式(继续调试)的命令时,它终止了整个调试会话:

>>> exit()
The program exited via sys.exit(). Exit status: None
....long nasty stack trace here....

[home]#

我也试过 quit() 它也终止了调试器。

如何在不终止整个调试会话的情况下退出 interact 模式?这可能吗?

理想情况下,我想 return 在我离开的地方进入调试模式,这样我就可以继续单步执行我的代码。

通过按 Ctrl + D 发送 EOF 应该有效:

$ python -m pdb myscript.py
> .../myscript.py(1)<module>()
-> import os
(Pdb) import code
(Pdb) code.interact()
Python 2.7.11 (default, Dec 27 2015, 01:48:39)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> <CTRL-D>
(Pdb) c
...

如果您正在使用 ipdb,并且在 Windows/Windows10,您应该使用 Cntrl-Z>Return 退出互动 shell。

ipython/python 3.5ipdb 以及 pdb

中测试

对于那些在 jupyter notebook 中寻找解决方案(但还不想学习 emacs)的人。我找到了一个对我有用的(来自 here)。

在linux shell:

echo ^D | xclip -selection clipboard 

但是,您输入的不是 ^D 作为字符,而是 ctrl-v ctrl-d...

在我的 Spyder 版本中(在 Gnome 上),我无法键入 Ctrl+DCtrl+Shift+U。因此,为了退出交互模式,我打开一个文本编辑器,键入 Ctrl+Shift+U,然后在不松开 Ctrl+Shift 的情况下,我按下 Ctrl+Shift+4。这会在文本编辑器中放置一个我可以突出显示和复制的字符。然后我将它粘贴到 Spyder 的交互模式中,我可以退出交互模式并返回调试器。

如果您正在使用 Emacs 并通过 M-x shell 访问 pdb 交互模式,我能找到的最好方法是调用 comint-quit-subjob (C-c C-\)。这会终止整个调试会话,returns 你会进入 shell 会话,而不是像 comint-send-eof (C-c C-d) 那样终止整个 shell 进程。

(venv) c:\projects\my-project> python my-entry-point.py

    550         import ipdb; ipdb.set_trace(context=10)
--> 551         print("First line to start debugging at")

ipdb> interact
*interactive*
In : # call M-x comint-quit-subjob (C-c C-\)
^C
(venv) c:\projects\my-project>

相关说明,如果您想完全退出调试器,只需按 q 然后输入即可。

https://github.com/jupyter/notebook/issues/3603#issuecomment-747392494

from pandas.io.clipboard import copy; copy("\x04")

将 Ctrl-D 复制到剪贴板,您可以粘贴并输入。

在 Windows 10 上,按 Ctrl + Z 并输入 Enter。

在Python3中,使用交互式解释器:

(Pdb) code.interact()
>>> (Enter your commands)
>>> ...
>>> exit() # Exit interactive mode
(Pdb) c

您也可以在主代码中导入“代码”,然后在“Pdb”模式下使用code.interact()

回复:https://docs.python.org/3/library/code.html

(注意:exit() 在 Python 2 中的交互模式下不起作用)