Audvik Labs

Debugging in Python

Tricks for easier debugging in python

Bugs and debugging are part of the daily lives of programmers. Therefore, to learn some useful debugging techniques is an important task for every developer.

 

Why is debugging important?

There are bugs in every modest sized or larger application. Every developer has to learn how to debug code in order to write programs that work as correctly as time and budget allow.

In addition to fixing bugs, debugging is an important skill for improving the efficiency of an application by optimizing performance and improving the logic. Debugging is a complex skill that takes time and practice for a developer to gain as a capability.

 

Strategies 

The first and foremost step of effective debugging is identifying the actual error. Once we get the error details, we need to find the error location, analyze the condition of the error and the underlying cause of the issue and resolve it. You need to have the right strategy along with the right tools. 

There are a couple of strategies in debugging. One is forward analysis, and the other is backward analysis.

The forward analysis method involves re-running the code in debugger mode, setting up the breakpoint in the suspicious line, and debugging it. To do forward analysis, you can use pdb and IDE to debug efficiently.

The backward analysis method involves tracing the problem using the logs collected from the production environment when the error occurred. For doing backward analysis, tools like print functions, logger, snapshots, etc., work.

 

Debugging tools

There are many debugging tools, some of which are built into IDEs like PyCharm and others that are standalone applications. The following list contains mostly standalone tools that you can use in any development environment.

 

pdb is a debugger built into the Python standard library and is the one most developers come across first when trying to debug their programs.

 

Web-PDB provides a web-based user interface for pdb to make it easier to understand what is happening while inspecting your running code.

 

wdb uses WebSockets to allow you to debug running Python code from a web browser.

Pyflame (source code) is a profiling tool that generates flame graphs for executing Python program code.

objgraph (source code) uses graphviz to draw the connections between Python objects running in an application.

 

1. Print and Check

The simplest but powerful method is to print some particular variables and check their values are as expected or not.

2. Assert and Check

Wherever the print is used to assist debugging, assert can be used instead.

3. Using logging Module

Python has an important module named logging. Replacing print or assert to logging methods is more professional and powerful way to debug. Actually, it is a “heavy weapon” for a Python project. Because it gives us lots of flexibility and abilities to debug, record and monitor our programs.

4. pdb

The fourth way is to start the Python debugger pdb, let the program run in a single step mode, and you can check the running status at any time. The pdb is a Python’s build-in debugger tool. There are some other debugger tools such as web-pdb, pudb and so on. In most cases, the pdb is useful enough.

5. Integrated Development Environment

If you want to set breakpoints and single-step execution more comfortably, you need an IDE (Integrated development environment) that supports debugging.

The current good Python IDEs are:

Visual Studio Code: Python plug-in needs to be installed.

 

PyCharm: Great IDE designed for Python developers.

 

Eclipse plus the “pydev” plug-in can also debug Python programs.

 

6. Pen and Paper

It sounds like a really traditional method but it can’t be ignored. Sometimes your mind is not very clear, writing it down with paper and pen is a good way to straighten out your thinking.

 

Debugging Crashing Application

If your application crashes before you get a chance to see what is going on in it, you might find this trick quite useful.

Running the application with-i argument ( python3 -i app . py) causes it to start interactive shell as soon as the program exits. At that point you can inspect variables and functions.

 

Conclusion

 

Developers have a variety of options for debugging. It is up to him to decide which one they want to utilize based on their requirements. Debugging is a fundamental requirement for any programmer who wants to be a productive developer.

Leave a comment

Your email address will not be published. Required fields are marked *