How to debug in Python – with example

  • Reading time:18 mins read

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.

 

How to debug in Python

 

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.

 

How to debug in Python

 

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.

 

How to debug in Python

 

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

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.