簡體   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