How To Debug In Python

1 mainAs with all programming related tasks, debugging is an important part. If you are using an IDE, then you’ll have some kind debugging feature available. But most Python programmers like to use vim or any text editor to write code and then execute it from the terminal. So how can you debug in this situation? Whenever you are working on a project, it’s nice to have a set of tools that can be used to understand what’s happening during execution. Python has an inbuilt debugger called pdb, which stands for “Python DeBugger”. Big surprise, right? It’s a great tool to analyze your code. In this blog post, we’ll see how to use some of its most popular features. Let’s get started, shall we?  

How to use it?

Let’s consider the following piece of code:

first_line = "This is the first line"
print first_line
text = "sentence"
new_line = first_line[:-4] + text
print new_line
words = new_line.split()
print words[1:-1]

Now, let’s say we want to stop the execution after the fifth line and debug the code. We need to import the pdb package and add the line “pdb.set_trace()”. The code should look like this:

import pdb
first_line = "This is the first line"
print first_line
text = "sentence"
new_line = first_line[:-4] + text
print new_line
pdb.set_trace()
words = new_line.split()
print words[1:-1]

Run the code and you should see the following on your terminal:

This is the first line
 This is the first sentence
 > /path/to/mycode.py(8)<module>()
 -> words = new_line.split()
 (Pdb)

If you observe this on your terminal, you have successfully launched pdb to debug your code.

Printing the values of variables

One of the main reasons we want to debug the code is to inspect the values of various variables. In order to print the variables, you can just use the “print” command:

(Pdb) print text
sentence
(Pdb) print first_line
This is the first line

Continuing the execution

During the debugging process, you can execute the current line by pressing the ‘n’ key (which stands for ‘next’) followed by the Enter key. If you want it to continue executing all the lines, you can press ‘c’ (which stands for ‘continue’). This command will execute all the lines and quit the debugger. You can also use ‘q’ (which stands for ‘quit’) to quit the debugger, but it won’t be graceful. You will see a crash message before it quits.

Locating yourself

If you are dealing with a big code file, then you may have to insert multiple debug statements. So it would be useful to know where you are in the file. This can be achieved by entering the ‘l’ key (which stands for ‘list’). If will show you a few lines of code where the current line will be highlighted using an arrow mark. This will give you some context during the debugging process.

What else can we do?

You can pretty much do anything in the debugger! It’s just acts like a python shell where you can define new variables, modify existing variables, print stuff, and so on. Happy debugging!

——————————————————————————————————————

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s