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

半世苍凉 提交于 2019-12-21 06:57:13

问题


Using python 3.5.1

When I run a script using the python debugger module:

  [home]# python -m pdb myscript.py

This starts a debug session:

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

If I want to enter an interactive terminal from within the debug session I can issue the interact command:

(Pdb) interact
*interactive*
>>>

Now I can interact with th code as if I was in a running python interactive mode, with access to any functions or variable in scope of the script running in the debugger at the time I entered interact mode.

When I issue the command to exit the interactive mode (to continue debugging) it kills the entire debug session:

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

[home]#

I've also tried quit() and it also terminates the debugger.

How can you exit out of interact mode without terminating the entire debug session? Is this even possible?

Ideally, I'd like to return to into debug mode at the point where I left off so I could continue stepping through my code.


回答1:


Sending an EOF by pressing Ctrl + D should work:

$ 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
...



回答2:


If you are using ipdb, and are on Windows/Windows10, you should use Cntrl-Z>Return to get out of the interactive shell.

Tested in ipython/python 3.5 and ipdb and pdb




回答3:


For those who look for a solution in jupyter notebook (and just yet do not want to learn emacs). I found one which worked for (me from here).

In linux shell:

echo ^D | xclip -selection clipboard 

But, you do not type ^D as characters but as ctrl-v ctrl-d...




回答4:


In my version of Spyder (on Gnome), I cannot type Ctrl+D or Ctrl+Shift+U. So to escape interactive mode, I open a text editor, type Ctrl+Shift+U then, without letting go of Ctrl+Shift, I press Ctrl+Shift+4. This places a character in the text editor that I can highlight and copy. I then paste it into the interactive mode of Spyder and I can get out of interactive mode and back into the debugger.




回答5:


If you're using Emacs and accessing the pdb interact mode through M-x shell, the best I could find was to call comint-quit-subjob (C-c C-\). This kills the entire debug session and returns you to the shell session rather than killing the entire shell process as comint-send-eof (C-c C-d) would do.

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



来源:https://stackoverflow.com/questions/36945059/in-the-python-debugger-pdb-how-do-you-exit-interactive-mode-without-terminating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!