paint-brush
Top Debugging Methods for Quicker Deployment in Pythonby@danielcrouch
254 reads

Top Debugging Methods for Quicker Deployment in Python

by DanielJanuary 17th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this article, I will cover most debugging methods, basic and modern ones. Apart from that, I will also introduce some hands-on tools and show some basic configurations and instructions. Hopefully, developers can get some hits when doing developments.
featured image - Top Debugging Methods for Quicker Deployment in Python
Daniel HackerNoon profile picture

Debugging is one of the most crucial parts of software development. No matter where the unexpected result comes from, it is impossible to escape that step. As the complexity of our systems keeps increasing every day, more advanced methods of debugging are coming out to meet the demand. Most of the time, the debug should execute within a running status while online.


In this article, I will cover most debugging methods, basic and modern ones. Apart from that, I will also introduce some hands-on tools and show some basic configurations and instructions. Hopefully, developers can get some hits when doing developments.

Basic Methods of Debugging

Logging

Applying log-in programs is always compulsory. It helps record where an error roughly lies and logs the necessary info about what is happening in the running program. There are many basic and advanced use cases for it. Here, I would like to describe some basic ones.

Import and Set the Level

import logging
from pathlib import PurePosixPath

logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)


Different levels will print out different logging information. The tricky setting is about the level of the log. After you set the logging level, the module will print out all levels of logs that have higher-level values. The good practice is to set separate loggers for different programs and also put them in one general setting file like .yaml other than in the code itself.

Log in file

formatter = logging.Formatter('%(message)s')
# log into file
log_file = PurePosixPath(log_dir)/(datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S.log'))
handler = logging.FileHandler(log_file)
handler.setLevel(logging.INFO)
handler.setFormatter(formatter)
logger.addHandler(handler)

Log in terminal

If you want to see the log with a timestamp in your terminal console, the following code is the right way to do so:

# log into terminal
console = logging.StreamHandler()
console.setFormatter(formatter)
console.setLevel(logging.INFO)
logger.addHandler(console)
logger.info(datetime.datetime.now().strftime('%Y-%m-%d: %H %M %S'))

Breakpoint

Breakpoints are widely used when you want to analyze a function. Normally, you need to set at least two points. After running the program, the execution will stop at the first point. Then you need to step into the function to see the output for every step.

During the process, the variables and their values will show, and you can monitor the behavior to see how the function works.

Print Out

This method is tricky. Most of the time, it is applied when you can not figure out where the error or problem comes from in a similar black-box program. You can print out a sequence of numbers or other strings to notice the rough location where the error appears.

Top Modern Methods of Debugging

Remote Debugging

In general, debugging is a concept used more in offline cases. But for modern debugging, offline manipulations no longer satisfy the demand in running status because it is not possible to shut down the services to debug.


Remote debugging has become the best choice in this situation. The main difference between remote debug and debug is about the extra configuration. However, the bottom principles are totally the same. It saves a significant amount of headache and time, it is suitable when there are microservices, and it is suitable for distributed systems or services.


Here, I am going to give a relatively detailed introduction to this method, and I will show you some basic configurations in popular tools like Pycharm. The main components for remote debugging are:

  • remote server/machine

  • remote access tunnel

  • program to debug

  • local machine


The compulsory requirement in remote debugging is to build a connection with the remote server. Visual Studio introduces at least four remote methods in their tutorial for remote development.


The most convenient and secure way is the ssh tunnel. It is about the sharing of public-private keys. After building the ssh connection, the rest of the debug methods are what I have mentioned earlier in the section on basic methods.

Reverse (Replay) Debug

Have you ever thought about tracing back through your finished debugging instead of starting debugging from scratch? Here is where reverse debug or replay debug, or time travel debug comes.


Reverse debug points at the tracing-back ability in the history of your debug. The developer always crashes at some point when debugging. In most cases, they are not able to find what the values are in the last debug step, and they have to start the process again to figure that out.

PyTrace – Time Travel Debugger

PyTrace is an easy-to-use debugging tool. To use it, you need to install the pycrunch-trace library and add the decorator @trace in front of a function like this:

from pycrunch_trace.client.api import trace

@trace
def function():
    your_code()

After you run your code, the records will be saved in your working directory.

Open the HTML file through the web app. You can save an example like this.

Source

You can use the button at the top to trace back and forward through the debug.

Conclusion

From all the examples and methods given above, we can see that modern debugging methods are much more efficient than basic ones in more complex system scenarios. If it were just a single function, the basic methods would be more convenient to use. You should always consider the most hands-on methods to debug your program.


There is no best solution for all cases, but there should be at least one best solution for one situation. Thanks for reading. I hope you enjoyed it.


Cover Image Source