42

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.

Ray
  • 40,256
  • 21
  • 101
  • 138

9 Answers9

48

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
...
Lukas Graf
  • 30,317
  • 8
  • 77
  • 92
  • 4
    Bonus tip: `CTRL-D` is also incredibly handy for closing dozens of open terminal / SSH sessions at the end of a long day of work: It will (usually) immediately close that tab in your terminal emulator, **except** if there's still a half-typed command on the command line or a running program in the foreground. – Lukas Graf Apr 29 '16 at 18:44
  • I think CTRL-D just killed my entire Spyder instance. – Josiah Yoder Jun 08 '18 at 19:52
  • 1
    In jupyter notebook (in firefox) it does not work. Ctrl+D is for creating a bookmark. Also discussed here https://stackoverflow.com/questions/47522316/exit-pdb-interactive-mode-from-jupyter-notebook?noredirect=1&lq=1 – nocibambi May 04 '19 at 16:49
9

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

alpha_989
  • 4,882
  • 2
  • 37
  • 48
5

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.

Synergist
  • 517
  • 4
  • 20
4

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

nocibambi
  • 2,065
  • 1
  • 16
  • 22
1

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>

Lorem Ipsum
  • 4,020
  • 4
  • 41
  • 67
0

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.

nonremovable
  • 798
  • 1
  • 6
  • 19
0

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

0

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)

Apostolos
  • 3,115
  • 25
  • 28
  • Mac, Python3, `exit()` will exit the whole debugging session and it's not possible to `c` after that. – M3RS Oct 13 '22 at 05:31
  • Well, I don't know anymore what this `code.interact()` is and where it is used. If I use it in PDB in my PY3, I get a nice "name 'code' is not defined"! And it I use `exit()` the script is just terminated. – Apostolos Oct 14 '22 at 17:12
-2

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

Nic Scozzaro
  • 6,651
  • 3
  • 42
  • 46