How to debug in Python using PDB
Debugging is a very important aspect of writing programs that every developer should master. The built-in module PDB is an interactive source code debugger for Python programs.
This module is provided by the standard library and therefore needs no installation. In this article, you’ll learn how to debug in Python by using different methods.
The PDB module allows us to set breakpoints within our code. It also allows us to interrogate our code line by line, pause and resume the execution of our code as we inspect variable execution.
By doing so we are able to determine how each line of code affects the flow of the program.
Prior to version 3.7 of Python we needed to import the PDB module at the top of the program that we needed to import.
To set up a breakpoint we would then do pdb.set_trace() as shown in the code below.
Inversions later than Python 3.6 would still work, however, a more precise way is the use of the built-in function breakpoint() without defaults.
This built-in function does not only import the module and call pdb.set_trace() by default but also provides us with the ability to control the debugging behaviour using its API.
Some of the most common commands provided by the module that can be used in the debugger include:
- s(tep)– The command, in this case, is reduced to s which executes the current line and stops in the next line of the current function or when a function is called.
- n(next) – Unlike the step command which stays and stops inside the current function this command executes called functions.
- The p expression – Executes and prints the values of the expression in the current context.
- c(continues) – Executes code and only stops when a breakpoint is encountered.
How to debug in Python – step by step
Below is a simple function that accepts two numbers as arguments, does some calculations and gives us a number at the end.
Besides the function definition, we have also initialized x and y with values 20 and 50 respectively. Furthermore, we have also set a breakpoint which when hit will open a debugging environment in the terminal.
Upon running this program we will get into an interactive PDB shell in the terminal where we have the breakpoint.
Using the commands enlisted earlier we can perform a few executions. For instance, using the p command, we can inspect various variables in the programs by typing the letter p followed by a single space and the name of the variable.
This command returns the value of the variable as shown in the code below.
Similarly, we can inspect the value of the variable y by typing the command p followed by the name of the variable as shown below.
Now using the command n we want to execute the expressions inside the current function and still stay in this function.
Using the command p out we would then print out the results of the expressions as shown below.
How to debug in Python – methods
But what we can do now is we can actually run it again and we’re going to go into the function. We can do this using various commands as shown here.
So now if we inspect the value of the variable y in the next line we’ll see that we get the value of y as 50.
Inspecting the values of y once more last time we get that the value of y is 500 in this case. In this process, we have stepped out of the function and returned the value of y.
We have then stepped into the function again updated the value of y through the expression y = y * 10.
Since 500 is the value that we expected that means that there is no bug and everything is as it should be.
We now wish to also update the value of x, we can therefore do that by running the next line which is num = x + y by using the command n.
Inspecting the value of the variable x shows that the value of x is now 23 as shown here.
Let’s now run the next line which is the expression num = x + y. To do that we need to run the commands n and p num consecutively. This executes the expression and returns the value of num as shown here.
Now that we are done executing the programs we are going to use the command c to execute everything all the way up to the breakpoint.
Most beginners tend to use print statements to trace bugs in programs, however, in large programs it is only convenient that we adopt proper debugging tools such as the PDB.
Such tools allow us to execute programs step by step and also insert all variables in the code so that we can see what is really happening. Knowing how to debug in Python is an important skill that every developer should learn.
Summary
This is how to debug in Python. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
