Debugging should not be an afterthought after deploying an application to production. It should be carried out as frequently as possible during development as this makes for easy tracking of code bugs and gives developers a fix to the issue that causes their code not to run. Most developers don’t know that Python’s print function doesn’t provide the exact picture of the errors that occur in development. So, what can you use to identify and fix these errors? The answer is debugging tools! They improve productivity and help make the coding process time effective.
The Python Debugger (pdb) is an interactive source code debugger for Python programs. Beyond debugging, it provides added functionality like setting conditional breakpoints, stepping through the source code line by line, and checking the variables at a particular line and its call stack.
This article will explain how the debugger tool, pdb
Is used to inspect and analyze your code to make it compliant with industry-standard during testing and before shipping to users.
To understand the process of debugging, you need to have:
To debug your Python code correctly, you need to be aware of the following tips to help improve productivity and error checking:
name = 'teri
print(name)
Console
In this section, you will test out a Python code by using the Python Debugger, pdb
. To run through the code and resolve errors as it happens in real-time in an interactive environment.
The built-in pdb
module is part of the standard library that Python comes with during installation on your work tool. This tool gives you several other commands you can use when testing your program. Let’s write a function using pdb. Create a new file app.py
or any other name you desire in your code editor, ending with the .py
extension.
import pdb
def multiply_number(num1, num2):
pdb.set_trace()
return num1 * num2
print(multiply_number(5, 10))
The code above does the following:
pdb
multiply_number
with two parameters, num1
and num2
set_trace
method, which is helpful in the pdb
object as it pauses your program and enters the debugger mode that allows you to type and test your code in the console
Running this program with the command python3 <name-of-file.py>
enters the debugger, where we can pass several debugger commands to run through the code. Check the
Next, let’s try out some commands in the console:
Typing a or args lists all the arguments used in the current function, multiply_number.
Before trying another command, update the code in the app.py file to include the string as part of the argument, so the programs output errors during execution:
# app.py
import pdb
def multiply_number(num1, num2):
pdb.set_trace()
return num1 * num2
print(multiply_number(5, 'execute'))
Rerun the program with the command python3 app.py:
Type num1
and num2
in the console, it outputs the result of the arguments passed in the multiply_number function.
Other commands you can try include the next step, continue, and so on, which are in the documentation.
Finally, once pdb
has identified the error in the code and the line number it occurred, you can go back to your code and clean it up by correcting the error and using the right argument so the program runs.
Note: The pdb
module is for testing during development, not production. Remove it before deploying.
This article guides you to adopt the best practices when trying to fix code and identify errors. Using the method is better than the print function as it has many more features to help you improve the quality of your code.