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

Question:

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.

Asked By: Ray

||

Answers:

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
...
Answered By: Lukas Graf

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

Answered By: alpha_989

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

Answered By: nocibambi

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.

Answered By: nonremovable

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:projectsmy-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:projectsmy-project>

Answered By: Lorem Ipsum

On a related note, if you want to exit the debugger entirely you just press q then enter.

Answered By: Nic Scozzaro

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.

Answered By: Synergist

On Windows 10, press Ctrl + Z and give Enter.

Answered By: Claudio Ciccarone

In Python 3, 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)

Answered By: Apostolos
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.