简体   繁体   中英

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

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.

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

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

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

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

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

Copies Ctrl-D to your clipboard, and the you can paste it and enter.

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.

If you're using Emacs and accessing the pdb interact mode through Mx shell , the best I could find was to call comint-quit-subjob ( Cc 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 ( Cc Cd ) 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>

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

, use the Interactive Interpreter:,使用交互式解释器:

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

You can also import "code" in your main code and just use code.interact() in "Pdb" mode.

Re: https://docs.python.org/3/library/code.html

(Note: exit() doesn't work in interactive mode in Python 2)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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