How to Debug in Python Without an IDE

In this article, we are going to study how to perform debugging without an IDE in Python.

Aviral Bhardwaj
Towards Dev

--

Photo by AltumCode on Unsplash

First of all, let’s understand what debugging is. Debugging, as the word implies, is the process of finding and resolving bugs within computer programs, software, and systems. It relates to checking the diagnostics of your application.

There are mainly 4 ways in which we can debug in Python without an IDE. Here, we will be making a basic program in different ways.

Problem statement

The problem statement is that we will take 2 numbers, a numerator and a denominator, and divide them. As you know, we can’t take the denominator to be 0 because any number divided by 0 is not defined.

Print

So, let’s start with the simplest one, the print function. Almost all languages support the print command.

dIn this code, I have made a division function that takes 2 input values, numerator and denominator. After that, I have inserted the print statement, which will print the denominator.

Now, I have taken the numerator as 1024 and the denominator as 0. When I run the code, I get a zero division error in my terminal (see below). It is telling me to change the denominator as it can’t be 0.

Advantages of print

  • Easy to use
  • Levels of print output can be controlled
  • Provides a basic way to go about debugging your code to resolve issues

Disadvantages of print

  • Useless print has to be deleted before the print is executed if the code is lengthy
  • Creates an additional maintenance point in the code
  • It is cumbersome to remove all the print() statements when you are done with debugging

Assert

We can use the assert command instead of the print command in Python. Assert statements are much faster and fix bugs quickly. Asserts are used to assert and remove certain conditions when the code is run.

If the condition is true, it does nothing, and your program continues to execute. But if the assert condition evaluates as false, it raises an AssertionError exception with an optional error message.

In this code, I have made a division function that takes 2 inputs, a numerator and a denominator. After that, I have inserted the assert statement. Thus, if the condition is true, it will run the assert code. Then, I have added a return function, which returns numerator/denominator.

As you can see, I have taken the numerator as 1024 and the denominator as 0. When I run the code, I get an assertion error in my terminal (see below). It is telling me to change the denominator as it can’t be 0.

Advantages of assertion

  • Easy to use
  • Better than print function

Disadvantages of assertion

  • Assert cannot check the runtime error
  • Assertions are not a replacement for program errors such as SyntaxError or NameError
  • If an assertion fails, you have discovered a bug. It does not tell you what is going on behind the scene.

Logging

Python has its logging module. As the logging module is a part of the standard Python library, the logging module allows

  • audit logging, which records the events of a user’s transactions for analysis, and
  • diagnostic logging, which records events related to an application’s operation.

The logging module keeps a record of the events that occur in the program.

In this code, I have imported the logging module with the import command. Then, I have made a division function that takes 2 inputs, a numerator and a denominator. After that, I have assigned the logging level (logging.warn) to the error and added a return function, which returns (numerator/denominator).

As you can see, I have taken the numerator as 1024 and the denominator as 0. When I run the code, I get a warning error in my terminal (see below). It is telling me to change the denominator as it can’t be 0.

Logging levels

The log level corresponds to the priority a log is given. There are 6 log levels in Python.

NOTSET=0

DEBUG=10

INFO=20

WARN=30

ERROR=40

CRITICAL=50

Effective use of logging can be done through the use of remote debugging. It is the process of debugging using software that is installed on another device and accessing the code on your device remotely.

Advantages of logging

  • Categorizes messages by importance and take out less important messages
  • Logs show you behavior and errors over time

Disadvantages of logging

  • Difficulty managing consistent levels

Python Debugger (PDB)

Python Debugger (PDB) is the simplest debugger and is best suited for small and medium-sized projects. The debugger comes with Python, so you don’t need to install it separately.

The PDB is a command-line debugger. The most important command of any debugger is called a breakpoint. A breakpoint is a point in the code where the debugger stops its execution and gives us access to the values of the variables at that particular point. Using these breakpoints, we can inspect our code.

There are 2 methods by which you can debug the code using PDB:

  1. From the terminal
  2. From the code itself

First, we are going to run PDB from the terminal.

In the given code, instead of making a function, I have taken 2 variables, a numerator and a denominator, and print (numerator/denominator).

To start the debugger, you need to enter the following command in the terminal:

$python -m pdb “filename”

Here, my filename is test01.py, so my command is:

$python -m pdb rough3.py

After starting the debugger, I entered l for source code and then n to continue to the next line. All the commands we can enter are given below.

As you can see, in the second box, we got a zero division error.

After using these commands, we can also use the following commands.

l: to list the source code

p: expression to evaluate the expression in the current context and prints its value

n: to continue to the next line

q: to quit the debugger

s : step into definitions (built-in / user-defined)

u: to skip remaining iterations in a loop

c: continue execution or till the next breakpoint() is encountered

variable name: to examine the current state of the variable

h option name: displays help on the option provided

h: to view the options menu and explore more options as needed.

Second, we are going to import PDB and then set.trace() to break at the position we want.

As you can see, we get a zero division error.

PDB is a very basic debugger, but it has various advanced extensions that can be used to make our work easier, like pbd++ and rpbd. However, it only works in a dev environment. It is hard to use in a QA/production environment. Moreover, it is vulnerable to security breaches such that users are able to reverse engineer.

Conclusion

I hope in this article you get a very good understanding of how to do debugging in python without an IDE

--

--