SoFunction
Updated on 2024-10-29

Python tips for debugging code with pdb

What is pdb

I don't know how you debug your code in Python when you get an error, but how do you go back and check each line step by step from the error message? If you don't have an IDE or command line to write code, how to debug quickly? If you use pdb for debugging, it will be very convenient.

Pdb is Python debugger, which is the debugger that comes with python. With pdb we caninteractivepdb can be used to view the values of variables in the running process, set breakpoints, execute code line by line, view the call stack of the code, and so on. And if the environment does not have a GUI, then pdb can help you debug code faster.

First we prepare a problematic piece of code for the example

def test(p):
  p = p + 1
  
  return p

def fastdebug():
  print('start')
  temp = 'ready' 
  print('ok')
  res = test(temp) 
  print('end')
  
fastdebug()

This code is very simple, when we execute fastdebug (), will execute the fastdebug function and in the function call another function test (), test function to receive a parameter p and calculate the value of p + 1 passed out. Obviously, the parameter temp passed in the highlighted line of code is str, so when we execute the above piece of code will definitely report an error!

Now let's pretend we don't know what the problem is and use pdb to debug this code.

Using pdb

Since pdb is a standard python library, we can just import it and use it.

import pdb

Then we set a breakpoint in front of the code segment we need to debug.

pdb.set_trace()

Applied to our code:point_down.

You can see that after running the code, pdb's debugger console will start waiting for our command from the location where we put set_trace() and the arrow markers point to the lines that pdb will run next. So let's go over some of the common commands used in pdb.

utilizationnto execute the next line

You can see that the debugger is pointing to the

print('start')

Type n in the pop-up text box to execute the line, and the arrow will point to the next line of code to be executed

Repeat the previous command using ENTER

Now that we haven't gotten to the point where the error is reported, we repeat the previous command using n or a direct carriage return

Printing variable values using p

In Pdb we can use p + variable name to view the value of a variable, for example we view the value of the variable temp

Use l to view the code on the top and bottom lines

In Pdb we can use l to see how many lines of code are above and below the current line

Use s to enter subfunctions

Now, we come to the line where the error occurs, and the reason for the error is that there's a problem passing in the variable, so we can use s in the pdb interaction line to get to the subfunction test()

Use r to execute the function until the end

In pdb you can use r to execute the currently running function to its end

Use c to stop debugging and continue running

Now that we have modified the relevant code to complete the debugging, we can use the c command to continue executing commands

Of course, you can also type q to exit the debugging screen.

Above is the use of Python standard library to perform a debug process, you can see pdb debugging code at the command line is very good, of course, in some cases pdb will not be the best choice such as multi-threaded related to the use of pdb in the larger project also seems to be overwhelmed, so we need to be flexible in choosing debugging options, but at least we have another grasp of a code debugging tool, isn't it?