Do you want to be a real Python debugging expert? Debug your Python code using these amazing Python debugging tools and libraries we shall soon cover!
Python is a general-purpose, high-level, and object-oriented programming language used for many development purposes. Moreover, it is an effective development tool for different applications, from web application development to web scraping and more complex applications like Machine learning and Data Science.
During development, errors called bugs in programming are prone to occur. Developers take several steps to detect and eliminate the existing and potential bugs from the code. As a result, you prevent the program code from crushing. We call this process debugging.
This article will look at several available Python debuggers you can use. We will also see how different these debuggers are from each other and what makes their implementation effective.
cProfiler debugging library
cProfiler, is a popular library and a C extension that executes profiling of long-running code. In the long run, it identifies sections of the program code that take long to run. Although it identifies the precise time taken to run different sections of the code, it does not entirely identify nor fix bugs in the code.
Therefore, the need to use other debuggers arises.
Fortunately, you can use recommended libraries like ipdb, Django-debug-toolbar, pyelftools, viztracer, and py-spy as debugging tools for your Python code instead.
The ipdb debugging tool
The IPython-enabled Python Debugger in full is an interactive third-party debugger that contains pdb‘s functionality. Ipbd as well comes with interactive shell IPython support. Such support includes tab completion, color support, and magic functions, among other support features.
This debugger enables access to the IPython debugger by exporting relevant functions. It also offers a similar interface for better introspection, just like in the pdb module.
Debugging with Ipdb
The library requires installation using the pip command below.
pip install ipdb
An example of using ipdb would look like this:
alpha_list = ['a', 'b', 'c']
fruit_list = ['orange', 'mango', 'kiwi']
for x in alpha_list:
if __name__ == '__main__':
Run the python file using the command below where test.py is my file’s name:
python -m ipdb test.py
The ipdb import and running the
ipdb.set_trace() function allows the beginning of the program and runs the debugger through the execution.
ipdb.pm() function(post-mortem) acts similarly to the
%debug magic function.
Pass the context as an argument to
set_trace to show several lines of code defined. Additionally,
set_trace also accepts as an argument, accepts Boolean values, and starts the ipdb interface when you set
Using configuration file
Set the context argument with an idpdb file or the
setup.cfg file available in the home folder and project folder, respectively. You are welcome to check ipdb’s functionality further.
Django Debug toolbar
The Django debug toolbar is a popular debugging tool in Django: a Python framework.
This configurable set of panels displays a current request or response’s debug information. When you click the toolbar, more details about the panel’s contents are displayed.
This tool thoroughly inspects the Django development environment.
Follow the installation process and the configuration instructions here.
The pyelftools library is purely built on Python. It parses and analyzes ELF files and DWARF debugging information and it only requires Python to run.
Using Pyelftools is easy since it does not have external libraries. Additionally, using pyelftools without installation is pretty easy since it simply requires adjusting
PYTHONPATH in the environment variables.
You will install it using:
pip install pyelftools
Implementation of pyelftools simply requires importing it and invoking it in your program.
The icecream 🍦 debugging tool
This is another efficient debugging tool for Python developers.
Using icecream, also
ic(), comes with many benefits over print(), as outlined below:
- Typing it is relatively faster, literally.
- It prints Data structures pretty well.
- Print expressions or variable names and their values with
- It highlights the syntax of the output.
- Optionally, it includes the program context, including the filename, line number, and the parent function.
Before using this package, install it using the pip command below:
pip install icecream
The good thing is you can avail
ic() in all files without necessarily importing it in all files by installing it using
ic() to the builtins module. All files the interpreter imports will share
In your first root python file, which you can name x.py, add
from icecream import install
from y import mult
In y.py fil the x.py file is importing, call ic()
ic| z : 8
What makes the ic() more efficient is its ability to inspect variables passed to it, including itself, then print its arguments and the argument values as in the following example.
from icecream import ic
return x * 4
ic| mult(100): 400
Moreover, you can insert ic() into pre-existing code since it returns its arguments. The example below returns
ic| x: 12, then
ic| y: 48.
from icecream import ic
x = 12
y = mult(ic(x))
Debugging using the py-spy tool
You can use py-spy to profile samples for Python programs as a debugging tool. Without restarting the program or modifying its code, py-spy visualizes the Python program’s execution. Furthermore, because it is written in Rust, it has a low overhead.
Additionally, It is worth considering that using py-spy against production Python code is safe because it runs a different process from the profiled Python program.
Like every other preceding tool we have seen, you can use the py-spy tool after installing it.
pip install py-spy
Despite your Python program serving production traffic, you can still profile and debug this program using py-spy, making it a critical Python profiler tool.
Alternatively, you can use viztracer, a debugging tool, to trace and visualize your Python program execution. It is also a profiling tool with low-overhead logging.
What makes viztracer an effective debugging tool?
- Using it is pretty easy, and it does not depend on external packages to work.
- Viztracer operates on all Operating System platforms: Windows, Linux, or macOS.
- Its powerful front-end smoothly renders GB-level trace.
- It uses RegEx to log arbitrary functions and extra information like variables and attributes, Raised Exceptions, Garbage Collector operations, etc., without modifying any code section within your source code.
- What makes viztracer a low-overhead debugging tool is its ability to filter out data you do not require in your program. It then retains old information before dumping the log in JSON format.
- You can use it to insert custom events like the instant event, the variable event, and the Duration event as the program runs. This way, it works like a print debug, except viztracer lets you know when the print occurs as you trace data.
Python profiling and debugging just like software profiling is a key step in a development that calls for attention. This step helps to rule out any code sections with bugs so that the overall performance of the code is optimized.
The debugging tools we have covered above are efficient tools that make a Python developer’s work pretty easy.
Seeing that the other debugging tools work more effectively than the cProfiler, you can, like a true hero, debug your Python code and fix any potential bugs using these debuggers to prevent your code from running without crashing.