w3resource

How do you exit the Python debugger (pdb) and resume normal script execution?

Exiting Python debugger (pdb) and resuming script

To exit the Python debugger ('pdb') and resume normal script execution, you can use the following 'pdb' commands:

  • continue (c): The c command continues script execution without further debugging. When you enter this command at the (Pdb) prompt, the debugger will exit, and the script will remain running until it completes or reaches another breakpoint, if set.
  • quit (q): The q command quits the debugger and terminate the script immediately. If you enter this command at the (Pdb) prompt, the debugger will exit, and the script will stop running at its current state.

Here's how we can use these commands:

Using the continue command (c):

(Pdb) c

After typing 'c', the debugger will continue script execution without further interaction. The program will run until it reaches the next breakpoint (if any) or completes its execution.

Code:

# test.py
import pdb
def divide(a, b):
    result = a / b
    return result

x = 10
y = 0
# Place the pdb.set_trace() statement after the variables are defined
pdb.set_trace()
z = divide(x, y)
print(z)

Run in Anaconda open terminal

(base) C:\Users\ME>python test.py
> c:\users\me\test.py(11)<module>()
-> z = divide(x, y)
(Pdb) c
Traceback (most recent call last):
  File "C:\Users\ME\test.py", line 11, in <module>
    z = divide(x, y)
  File "C:\Users\ME\test.py", line 4, in divide
    result = a / b
ZeroDivisionError: division by zero

Using the quit command (q):

(Pdb) q

After typing 'q', the debugger quits immediately. The script will stop running, returning you to the command prompt.

Note: If you try to use the 'q' command while the program is still executing or while the debugger is in the middle of some operation, pdb may display a warning message to confirm your action. You can confirm the quit command by typing 'y' and pressing Enter.

Code:

# test.py
import pdb
def divide(a, b):
    result = a / b
    return result

x = 10
y = 0
# Place the pdb.set_trace() statement after the variables are defined
pdb.set_trace()
z = divide(x, y)
print(z)

Run in Anaconda open terminal

(base) C:\Users\ME>python test.py
> c:\users\me\test.py(11)<module>()
-> z = divide(x, y)
(Pdb) p x
10
(Pdb) p y
0
(Pdb) q
Traceback (most recent call last):
  File "C:\Users\ME\test.py", line 11, in <module>
    z = divide(x, y)
  File "C:\Users\ME\test.py", line 11, in <module>
    z = divide(x, y)
  File "h:\anaconda3\lib\bdb.py", line 88, in trace_dispatch
    return self.dispatch_line(frame)
  File "h:\anaconda3\lib\bdb.py", line 113, in dispatch_line
    if self.quitting: raise BdbQuit
bdb.BdbQuit

(base) C:\Users\ME>


Follow us on Facebook and Twitter for latest update.