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

Ray picture Ray · Apr 29, 2016 · Viewed 11.9k times · Source

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.

Answer

Lukas Graf picture Lukas Graf · Apr 29, 2016

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