繁体   English   中英

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*
>>>

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

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

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

[home]#

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

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

理想情况下,我想在我离开的时候返回到调试模式,这样我就可以继续单步调试我的代码。

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.5ipdbpdb

对于那些在 jupyter notebook 中寻找解决方案的人(并且还不想学习 emacs)。 我找到了一个对我有用的(从这里)。

在 Linux 外壳中:

echo ^D | xclip -selection clipboard 

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

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

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

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

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

如果您使用 Emacs 并通过Mx shell访问pdb交互模式,我能找到的最好方法是调用comint-quit-subjob ( Cc C-\\ )。 这会终止整个调试会话并将您返回到 shell 会话,而不是像comint-send-eof ( Cc Cd ) 那样comint-send-eof整个 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>

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

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

(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 的交互模式下不起作用)

在相关说明中,如果您想完全退出调试器,只需按q然后回车。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM