Mistakes in your code are known as “bugs”. You will make mistakes. You will make many mistakes, and that’s totally fine. Most of the time, they will be simple mistakes, such as typos. But since computers are very literal, even typos prevent your code from working as intended. So they need to be fixed. The process of fixing your mistakes in programming is known as debugging.
The Python programming language comes with its own built-in debugger called pdb
. You can use pdb
on the command line or import it as a module. The name, pdb
, is short for “Python debugger”.
Here is a link to the full documentation for pdb
:
In this article, you will familiarize yourself with the basics of using pdb
. Specifically, you will learn the following:
pdb
in the REPLpdb
on the Command Linepdb
set_trace()
breakpoint()
FunctionWhile pdb
is handy, most Python editors have debuggers with more features. You will find the debugger in PyCharm or WingIDE to have many more features, such as auto-complete, syntax highlighting, and a graphical call stack.
A call stack is what your debugger will use to keep track of function and method calls. When possible, you should use the debugger that is included with your Python IDE as it tends to be a little easier to understand.
However, there are times where you may not have your Python IDE, for example when you are debugging remotely on a server. It is those times when you will find pdb
to be especially helpful.
Let’s get started!
pdb
in the REPLThe best way to start is to have some code that you want to run pdb
on. Feel free to use your own code or a code example from another article on this blog.
Or you can create the following code in a file named debug_code.py
:
Python
# debug_code.py
2
def log(number):
3
print(f'Processing {number}')
4
print(f'Adding 2 to number: {number + 2}')
5
6
def looper(number):
7
for i in range(number):
8
log(i)
9
10
11
if __name__ == '__main__':
12
looper(5)
There are several ways to start pdb
and use it with your code. For this example, you will need to open up a terminal (or cmd.exe
if you’re a Windows user). Then navigate to the folder that you saved your code to.
Now start Python in your terminal. This will give you the Python REPL where you can import your code and run the debugger, pdb
. Here’s how:
Python
>>> import debug_code
2
>>> import pdb
3
>>> pdb.run('debug_code.looper(5)')
4
> <string>(1)<module>()
5
(Pdb) continue
6
Processing 0
7
Adding 2 to number: 2
8
Processing 1
9
Adding 2 to number: 3
10
Processing 2
11
Adding 2 to number: 4
12
Processing 3
13
Adding 2 to number: 5
14
Processing 4
15
Adding 2 to number: 6
The first two lines of code import your code and pdb
. To run pdb
against your code, you need to use pdb.run()
and tell it what to do. In this case, you pass in debug_code.looper(5)
as a string. When you do this, the pdb
module will transform the string into an actual function call of debug_code.looper(5)
.
The next line is prefixed with (Pdb)
. That means you are now in the debugger. Success!
To run your code in the debugger, type continue
or c
for short. This will run your code until one of the following happens:
In this case, there were no exceptions or breakpoints set, so the code worked perfectly and finished execution!
#tutorial #python #debugging #pdb