---
title: "7 Need to Know Python pdb Debugger Commands"
description: "Here are the seven top python pdb commands. Helpful to use these commands at the pdb command line. Each one is explained with the result."
url: https://srinimf.com/2022/03/07/7-need-to-know-python-pdb-debugger-commands/
date: 2022-03-07
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2022/03/pexels-photo-7014925.jpeg?fit=1880%2C1253&ssl=1
categories: ["Python"]
tags: ["pdb debugger commands"]
type: post
lang: en
---

# 7 Need to Know Python pdb Debugger Commands

Here are seven helpful python pdb commands. Those are next, quit, print line numbers, display variable, set break points and clear break points.

Advertisements

## Here’s Python source code to use for debugging

```
#Python source code is present # in main.py
import pdb; 
pdb.set_trace() 

#try finally 
try: 
    print("Hello World") 
    # No luck, an exception 
    number = int("abc") 
finally: 
    print("==> Let's start our journey to the moon!") 

print("TO THE MOON…")
```

Before you [start debugging](https://pymotw.com/2/pdb/), firstly import pdb. Then set pdb to set_trace(). The set_trace() allows you [to debug your](https://srinimf.com/2021/07/30/ibm-debugger-here-are-the-most-useful-commands/) code interactively.

## List of python pdb commands

n –> to go to next stmt

q —> to quit from debug mode

l —> to print line numbers

p –> display variable

b—> b x set b point

cl –> clear

c –> continue

## Result of each command of pdb debugger

### Python pdb Command n

Here, you will see the result of ‘n’ command. It goes to next statement and execute it.

```
> /home/repl/b7e783ae-efe2-4425-ac87-ed04cfce52d6/main.py(5)<module>() 
-> try: 
(Pdb) 
n 
> /home/repl/b7e783ae-efe2-4425-ac87-ed04cfce52d6/main.py(6)<module>() 
-> print("Hello World") 
(Pdb)
```

### Python pdb Command q

You will see here the result of ‘q’ command. The control comes out of the debugger.

```
> /home/repl/ef97b988-d2f3-44b5-97f5-c83110b99b76/main.py(5)<module>() 
-> try: 
(Pdb) 
q 
Traceback (most recent call last): 
  File "main.py", line 5, in <module> 
    try: 
  File "main.py", line 5, in <module> 
    try: 
  File "/usr/lib/python3.8/bdb.py", line 88, in trace_dispatch 
    return self.dispatch_line(frame) 
  File "/usr/lib/python3.8/bdb.py", line 113, in dispatch_line 
    if self.quitting: raise BdbQuit 
bdb.BdbQuit 


** Process exited - Return Code: 1 **
```

### Python pdb Commands l (print line numbers) and c (continue)

The command l displays line numbers for source code.

```
> /home/repl/6b3c467d-30ac-477f-ab1e-fe07b185a710/main.py(5)<module>() 
-> try: 
(Pdb) 
l 
  1  	import pdb; 
  2  	pdb.set_trace() 
  3  	 
  4  	#try finally 
  5  ->	try: 
  6  	    print("Hello World") 
  7  	    # No luck, an exception 
  8  	    number = int(123) 
  9  	finally: 
 10  	    print("==> Let's start our journey to the moon!") 
 11  	 
(Pdb) 
c 
Hello World 
==> Let's start our journey to the moon! 
TO THE MOON… 


** Process exited - Return Code: 0 **
```

### Python Command p

The command p and variable name if you give, it displays the value present in the variable.

```
> /home/repl/46fca557-3b61-4079-9d94-cec3d6789312/main.py(5)<module>() 
-> try: 
(Pdb) 
n 
> /home/repl/46fca557-3b61-4079-9d94-cec3d6789312/main.py(6)<module>() 
-> print("Hello World") 
(Pdb) 
n 
Hello World 
> /home/repl/46fca557-3b61-4079-9d94-cec3d6789312/main.py(8)<module>() 
-> number = int(123) 
(Pdb) 
n 
> /home/repl/46fca557-3b61-4079-9d94-cec3d6789312/main.py(10)<module>() 
-> print("==> Let's start our journey to the moon!") 
(Pdb) 
p number 
123 
(Pdb) 
c 
==> Let's start our journey to the moon! 
TO THE MOON… 


** Process exited - Return Code: 0 **
```

### Command b (break point) and Command cl (clears all break points)

Here, when you set b and line number, it sets break point at that line. The cl command clears all break points. I set break point at line 8. When I give c (continue), it will then stops at line 8.

```
> /home/repl/cb23d974-ca23-4158-bef1-c16f7f9b0183/main.py(5)<module>() 
-> try: 
(Pdb) 
l 
  1  	import pdb; 
  2  	pdb.set_trace() 
  3  	 
  4  	#try finally 
  5  ->	try: 
  6  	    print("Hello World") 
  7  	    # No luck, an exception 
  8  	    number = int(123) 
  9  	finally: 
 10  	    print("==> Let's start our journey to the moon!") 
 11  	 
(Pdb) 
b 8 
Breakpoint 1 at /home/repl/cb23d974-ca23-4158-bef1-c16f7f9b0183/main.py:8 
(Pdb) 
c 
Hello World 
> /home/repl/cb23d974-ca23-4158-bef1-c16f7f9b0183/main.py(8)<module>() 
-> number = int(123) 
(Pdb)
```

### Python Clear cl command

```
> /home/repl/5124e49f-df43-456e-a356-1509a51a127e/main.py(5)<module>() 
-> try: 
(Pdb) 
b 8 
Breakpoint 1 at /home/repl/5124e49f-df43-456e-a356-1509a51a127e/main.py:8 
(Pdb) 
c 
Hello World 
> /home/repl/5124e49f-df43-456e-a356-1509a51a127e/main.py(8)<module>() 
-> number = int(123) 
(Pdb) 
cl 
Clear all breaks? 
y 
Deleted breakpoint 1 at /home/repl/5124e49f-df43-456e-a356-1509a51a127e/main.py:8 
(Pdb) 
c 
==> Let's start our journey to the moon! 
TO THE MOON… 


** Process exited - Return Code: 0 ** 
Press Enter to exit terminal
```

To sum up, these seven commands are useful to work with python pdb debugger.

References

- [How To Use the Python Debugger](https://www.digitalocean.com/community/tutorials/how-to-use-the-python-debugger)
- [The python debugger-pdb](https://docs.python.org/3/library/pdb.html)

Related posts

- [How to Debug Using ‘pdb’ in Python](https://srinimf.com/2020/08/02/how-to-debug-py-module-using-pdb/)DateAugust 2, 2020
- [Python Series Vs. Dataframe Top Differences](https://srinimf.com/2020/01/18/python-series-vs-dataframe-explained-top-differences/)DateJanuary 18, 2020
- [12 Tricky Python Coding Interview Questions](https://srinimf.com/2020/02/23/12-frequently-asked-python-interview-questions/)DateFebruary 23, 2020

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2022%2F03%2F07%2F7-need-to-know-python-pdb-debugger-commands%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2022%2F03%2Fpexels-photo-7014925.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=7%20Need%20to%20Know%20Python%20pdb%20Debugger%20Commands)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2022/03/07/7-need-to-know-python-pdb-debugger-commands/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2022/03/07/7-need-to-know-python-pdb-debugger-commands/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2022/03/07/7-need-to-know-python-pdb-debugger-commands/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2022/03/07/7-need-to-know-python-pdb-debugger-commands/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%207%20Need%20to%20Know%20Python%20pdb%20Debugger%20Commands&body=https%3A%2F%2Fsrinimf.com%2F2022%2F03%2F07%2F7-need-to-know-python-pdb-debugger-commands%2F&share=email)
-

### Like this:

Like Loading…
