How to exit pdb and allow program to continue?
Asked Answered
R

10

181

I'm using the pdb module to debug a program. I'd like to understand how I can exit pdb and allow the program to continue onward to completion. The program is computationally expensive to run, so I don't want to exit without the script attempting to complete. continue doesn't seems to work. How can I exit pdb and continue with my program?

Rento answered 23/7, 2013 at 20:40 Comment(5)
continue exits loops, return exits methods/functionsBottrop
@Stephan: He's talking about continue, the pdb command. Not the Python statement.Jasmine
@turtle: continue should "Continue execution, only stop when a breakpoint is encountered", according to the docs. Do you have a breakpoint set that is getting hit?Jasmine
ah. ok i see. yep, i have a break point set inside a loop. anyway to get around this? the loop is called thousands of times, so I can't keep manually hitting continue.Rento
@turtle: Do you have a breakpoint set from within Pdb, or do you have a pdb.set_trace() in your source code?Jasmine
J
287

continue should "Continue execution, only stop when a breakpoint is encountered", so you've got a breakpoint set somewhere. To remove the breakpoint (if you inserted it manually):

(Pdb) break
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /path/to/test.py:5
(Pdb) clear 1
Deleted breakpoint 1
(Pdb) continue

Or, if you're using pdb.set_trace(), you can try this (although if you're using pdb in more fancy ways, this may break things...)

(Pdb) pdb.set_trace = lambda: None  # This replaces the set_trace() function!
(Pdb) continue
# No more breaks!
Jasmine answered 23/7, 2013 at 20:55 Comment(8)
The above doesn't exit pdb.Urticaria
@SteveBarnes: Is it supposed to?Jasmine
About the 15th word of the question!Urticaria
@SteveBarnes: I think you're taking it too literally. The OP is just hitting a breakpoint over and over again. All they need is to continue their long-running process without hitting breakpoints. Whether or not Pdb is actually running is inconsequential.Jasmine
Nice tip on setting set_trace = lambda: None :)Enterectomy
For the extreme cases, nothing beats set_trace = lambda: None. Python org should add a command that just lets you get out of pdb.Mump
For those who want set_trace to work as expected again at a later point. reload(pdb) Obtained from here: #19099350Fernyak
How will it work if I use python 3.7 and breakpoint()Tamar
S
54

A simple Ctrl-D will break out of pdb. If you want to continue rather than breaking, just press c rather than the whole continue command

Schlessel answered 29/12, 2016 at 2:15 Comment(5)
What about when you're inside a function, inside a loop? It seems to just go to the next loop for me, and I can't actually close pdb, or even close python, without closing the entire terminal?Oxymoron
@LimokPalantaemon Weird, have you tried typing exitSchlessel
This doesn't work if you set a trace or if you have break points unless you continue to press c .Vo
embarassing, but this saved me from being stuck for too long -_-Kendry
this closes the python console too.Rubie
B
16

The answer from @voithos is correct, so I'll just add one alternative in the case where you are using set_trace. Yes, the pdb.set_trace = lambda: None hack works OK, but not if you have other breakpoints set and want to reenable it later on. To me this points to the fact that unfortunately pdb is missing a bunch of functionality (even basic stuff like display lists), and this is another case.

The good news is that pdb++ is a great drop-in replacement for pdb, and one of the things it solves is exactly the problem of disabling set_trace. So you can simply do:

pip install pdbpp

and then at the (Pdb++) prompt, type:

pdb.disable()

If you want to reenable later, unsurprisingly this works:

pdb.enable()

Easy! And you will get lots of other useful goodies on top of that.

Bitchy answered 15/3, 2018 at 20:56 Comment(0)
J
12

If you used breakpoint() in your code, you can disable all those breakpoints by environment variable.

import os
os.environ['PYTHONBREAKPOINT'] = '0'

This also work when you are in (Pdb) interactive state, this will disable all breakpoint() inserted in your code, but not breakpoints enabled by (Pdb).

So lets test this, code have breakpoint() in line 4:

> /tmp/py.py(5)<module>()
-> print(x,a)
(Pdb) ll
  1     
  2     for x in "abcdefg":
  3         a=1
  4         breakpoint()
  5  ->     print(x,a)
  6         b=2
  7         print(x,b)

Lets set new breakpoint in line 6.

(Pdb) break 6
Breakpoint 1 at /tmp/py.py:6

(Pdb) break
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /tmp/py.py:6

Now lets disable all breakpoint() in this case in line 4.

(Pdb) import os
(Pdb) os.environ['PYTHONBREAKPOINT'] = '0'

and continue

(Pdb) c
a 1
> /tmp/py.py(6)<module>()
-> b=2
(Pdb) ll
  1     
  2     for x in "abcdefg":
  3         a=1
  4         breakpoint()
  5         print(x,a)
  6 B->     b=2
  7         print(x,b)

now it stops in line 6 and skip line 4, lets continue

(Pdb) c
a 2
b 1
> /tmp/py.py(6)<module>()
-> b=2

now it stops again in line 6 and skip line 4

(Pdb) break
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /tmp/py.py:6
(Pdb) clear 1
Deleted breakpoint 1 at /tmp/py.py:6

now (Pdb) breakpoint in line 6 is deleted and program can continue working:

(Pdb) c
b 2
c 1
c 2
d 1
d 2
e 1
e 2
f 1
f 2
g 1
g 2
Junta answered 19/3, 2022 at 18:38 Comment(0)
U
5

If you really wish to exit the debugger then you need to run something like WinPdb which allows you to detach from the process and then exit the debugger, (N.B. It is multi-platform).

If you would like to continue debugging but no longer stop at a given breakpoint then you need to:

  1. Make a note of the breakpoint number, (or the file and line number),
  2. Either cl bp_number or clear file:line to permanently remove the breakpoint or disable pb_number to toggle it off but be able to toggle it back.
  3. Then continue and your program run until then next different breakpoint is hit.

For more detail on the above see the manual.

Urticaria answered 23/7, 2013 at 20:55 Comment(0)
B
1

I was stuck in a pdb shell, with Enter just producing ^M, so I couldn't escape it.

After some scientifically-crafted experimentation, turns out ctrl+Z haults the pdb shell. (Then stty sane to fix the Enter key.)

Then you need to kill or resume the pdb shell. To do so, run jobs, then either fg 1 or kill %1 (substituting the job number).

Figured I'd note it here for any others, and myself when I'm back here next week.

Borgeson answered 17/8, 2022 at 17:14 Comment(0)
S
1
  • hit c -> for continue (executes your program further)
  • hit q -> for quit (exit debugger and your program as well)
Sweetbread answered 17/3, 2023 at 8:58 Comment(0)
R
0

find new way to exit the pdb without install anything: - when the program starts to run, press ctrl+c, then switch the window to another(any window), then the original shell with pdb running should show something like: (pdb) ..... - switch back to pdb, then press Enter, now you are all set, pdb command shell reappear again

Rabinowitz answered 28/3, 2020 at 12:36 Comment(0)
H
0

Disable the breakpoint. from pdb, just type disable N, where N is the breakpoint number you are stuck on. If you don't know the number of your troubling breakpoint, enter tbreak. This will list your breakpoints by number under column "Num" and show whether (yes) or not (no) they are enabled or disabled under the column headed with "Enb."
Once you disbale your troubling breakpoint, running the command continue (or the abbreviation c) should finish your program.

Hygroscope answered 28/2, 2022 at 15:30 Comment(0)
A
0

which running pdb in docker shell/container, in order to exit

run pdb.disable() and then exit()

Amberambergris answered 27/3 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.