×

Python Debugger

In this tutorial, we will understand what is debugging in general. Then we will realize what the python debugger means and what is the method to perform it.

Now, let us move toward our topic.

Debugging

It is the process which is used by the developers to find out the error or bug and then resolve it to enable the code to run smoothly. Simply, it is a way of keeping the code free of bugs.

Python Debugger

A communicating source code environment for Python programs is known as Python Debugger Python allows debugging the code using a default module named pdb module. (Python developer module)The module needs to be imported into a python script to use it. The pdb module employs the basic debugger functions i.e., bdb, and line-oriented command interpreters i.e., cmd.

The pdb is advantageous as it runs entirely in the command line so making it great for debugging code on isolated servers in case the user lacks the privilege of a debugger based on Graphic User Interface.

Following are the things supported by pdb:

  1. Setting of breakpoints
  2. Stepping through code
  3. Listing of source code
  4. Inspecting stack traces

It is required to import the module to be able to use the pdb module.

Syntax to import pdb:

import pdb

Invoking a debugger

A debugger can be invoked by several methods.

  • For debugging within the program-

It is required to insert the following two commands import pdb and pdb.set_trace().

Further, it is required to execute the script ordinarily and execution will continue until it reaches the introduced breakpoint.

Here is an example that shows how to insert the set_trace() function.

Example 1: Addition of two numbers

Planned error: Asthe input() function returns a string the program concatenates those strings as a replacement for adding input numbers

import pdb




def sum(a1, a2):
	answer = a1 + a2
	return answer




pdb.set_trace()
b1 = input("Enter the 1st number: ")
b2 = input("Enter the 2nd number : ")
result = sum(b1, b2)
print(result)

Output:

Python Debugger

Explanation: This image shows the output of the code written above. Here, the number 18 denotes the line number where the breakpoint is found in the main.py module. The next line denotes the code line where the execution stops.

  • From the Command Line:

This method is considered the simplest means of employing a debugger. It just requires running the following command in the terminal.

python -m pdb file name

The above declaration loads the source code and stops implementation on the first line of code

#Python program to understand the debugger


def add(a1, a2):
	res = a1 + a2
	return res




b1 = input("Enter the 1st number : ")
b2 = input("Enter the 2nd number : ")
sum = add(b1, b2)
print(sum)

Output:

Python Debugger
  • Post-mortem debugging

It means getting into debug mode once after the execution process of the program is completed which means the failure has already occurred. 

The pdb module supports post-mortem debugging through the function named post_mortem() or pm(). An active trace back is looked at by this function and the debugger is initiated at the line in the call stack where the exception occurred.

Example:

# Python program to understand post-mortem debugging


def multiply(a1, a2):
	res = a1 * a2
	return res




x1 = input("Enter the 1st number : ")
x2 = input("Enter the 2nd number : ")
res1 = multiply(x1, x2)
print(res2)

Output:

Python Debugger

Checking variables on the Stack

Each variable including local variables as well as global variables is maintained on the stack. All the arguments of a function can be printed using args(or use a) function that is currently active. p command evaluates an expression given as an argument and prints the result.

Python pdb Breakpoint

While working with bulky programs we often need to increase the number of breakpoints at the point where we know errors might occur. To organize this user just need to usethe break command. When a breakpoint is inserted, the debugger allocates a number to it starting from 1.

Then the break command is employed to display all the breakpoints present in the program.

Managing Breakpoints

we can accomplish the breakpoints usingthe enable and disable and remove commands after the addition of breakpoints with the aid of numbers allocated to them.

It is the disable command which prevents the debugger from stopping when that breakpoint is reached while enable command turns on the disabled breakpoints.


Related Topics

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the...

5 minutes read.

Python String expandtabs() method

Python String expandtabs() method The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces. Syntax String.expandtabs([tabsize]) Parameter tabsize: This parameter specifies the number of characters to...

1 minute read.

Expressions in Python

What is Expression in Python? The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming...

12 minutes read.

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

Allocate a minimum number of pages in python

You have given a sorted array of size n which represents the number of pages in n different books and an integer value which denotes the number of students. We...

4 minutes read.

Managing Multiple Python Versions With pyenv

Introduction If you had ever wished to assist to an application that makes use of several different Python versions but weren't sure whether you would quickly test them all? Do you...

4 minutes read.

_name_ in Python

Introduction: The code at level 0 indentation is to be performed when the command to run a Python program is supplied to the interpreter because Python does not have a main() function...

4 minutes read.

Program of Cumulative Sum in Python

Introduction This tutorial, explains what is meant by lists, the cumulative total, several approaches to calculating the cumulative sum of digits in a list, etc. Learn the straightforward Python codes for...

5 minutes read.

Data Class in Python

Introduction In Python 3.7, the dataclass modules are introduced as a practical tool for creating organized classes designed specifically for data storage. These classes contain specific attributes and capabilities to deal...

6 minutes read.

Python math.cos and math.acos function

Math.cos() function In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in...

3 minutes read.

Reading a File Line by Line in Python

Introduction In this tutorial, we will learn about reading files in python line by line. Before reading the files, let us know a little information about the files first. Files A file is...

6 minutes read.

Nested for Loop in Python

"Loop" is one of the foundation concepts to learn programming in any language. Nested loops are significant steps in solving many types of problems, ranging from basic to complex scenarios....

9 minutes read.

Python RegEx

Python RegEx (python regular expressions) is a concept of writing and finding expressions in a pool of characters easily. A Regular expression (RegEx) is a set of characters that defines search...

10 minutes read.

What is the re.sub() function in Python

There. sub () function is used to return a string by replacing the occurrences of a specific character or pattern with a replacement string. To use this function, import the...

3 minutes read.

GET and POST requests using Python

‘GET’ and ‘POST’ are two request methods of Hypertext Transfer Protocol (HTTP). What is HTTP? HTTP stands for Hypertext Transfer Protocol. It is a collection of protocols which makes communication between client...

4 minutes read.

Python data science course

What is meant by Data Science? When processing raw, structured, and unstructured data utilizing various technologies, algorithms, and the scientific method, data science is a detailed study of the enormous quantity...

4 minutes read.

BOTTLE Python Web Framework

The Bottle is a lightweight WSGI micro web framework for python. It acts like a thin wrapper around a web server where it is distributed as a single file module...

4 minutes read.

Comment starts with the symbol in Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

3 minutes read.

Application to Search Installed Application using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

4 minutes read.