paint-brush
Introduction to Python Debugging with Pdbby@terieyenike
2,575 reads
2,575 reads

Introduction to Python Debugging with Pdb

by TeriAugust 24th, 2022
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

The Python Debugger (pdb) is an interactive source code debugger for Python programs. 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. The built-in pdb is part of the standard library that Python comes with during installation on your work tool. It gives you several other commands you can use when testing your program.
featured image - Introduction to Python Debugging with Pdb
Teri HackerNoon profile picture

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, pdbIs used to inspect and analyze your code to make it compliant with industry-standard during testing and before shipping to users.


Prerequisites

To understand the process of debugging, you need to have:

  • Python installed on your local machine
  • Knowledge of Python


Ways to Improve Your Python Programs

To  debug your Python code correctly, you need to be aware of the following tips to help improve productivity and error checking:

  • Linting: It detects issues with your code before running the code. A way to make this work is by installing software that helps color-coding your programs so that you make fewer errors and quickly resolve them when the IDE (VS Code) points the error out. One such tool is Pylint, and it gives suggestions when you code.
  • An integrated development environment (IDE) / editor: Python-specific IDEs like PyCharm or VS code have built-in tools and features that help with auto-formatting code based on PEP8 and highlight your code when there is an error.
  • Read errors: Learning to read code errors in your Python programs would significantly solve half of the problem because you can understand what they mean in the console.


name = 'teri

print(name)


Console

Python Debugging with pdb

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 pdbmodule 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.pyor 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:

  • Import the library pdb
  • Define a function, multiply_number with two parameters, num1 and num2
  • Call the library with the 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
  • Get the result of the arguments with the return keyword
  • Call the function and pass in the number arguments


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 Debugger Commands documentation to find the complete list of commands you can use.


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 pdbmodule is for testing during development, not production. Remove it before deploying.

Conclusion

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.


Learn More