×

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

Poolmanager in Python

Python: 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 faster way....

4 minutes read.

Convert XML to JSON in Python

XML conversion is very useful if we work on an API that returns data in JSON format and the source of data is in XML format. JSON A JSON file reserves the...

4 minutes read.

Python issubclass() Function

Python issubclass() Function The issubclass() function in Python returns a boolean value ‘True’ if the given object is a subclass of classinfo, else it returns False. Syntax issubclass(class, classinfo) Parameter class: It is a required parameter which represents a tuple...

1 minute read.

Python List extend() method

Python List extend() method The list.extend() method extends the list by appending all the items from the iterable. Syntax list.extend(iterable) Parameter iterable: It is a required parameter which represents any iterable unlike list, set, tuple, etc. Example 1 # Python...

1 minute read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

Change Data Type in Python

Python is a dynamic language where it is not always required to consider every variable type. Python supports a wide range of data types, but There are mainly six data...

3 minutes read.

Python next() function

Python next() function The next() function in Python retrieves the next item from the iterator.  Syntax next(iterator[, default]) Parameter iterable: It is a required parameter which represents an iterable object. default: This parameter represents a default value to...

1 minute read.

Python IDE names

Python is one of the most renowned programming languages. It has different execution conditions and a great many compilers to execute the python programs, e.g., PyCharm, PyDev, Jupyter Notebook, Visual...

6 minutes read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Anaconda python 3 installation for windows 10

If you are a problem solver and like to solve programming questions, the anaconda distribution for python could be one of the best options to use and solve problems in...

3 minutes read.

Features of Python

Python is a powerful, easy to learn, popular programming language. It has effective data structures and its elegant syntax makes it user-friendly language. Below are the key features of Python: 1. Python...

4 minutes read.

Convert Float to Int in Python using Pandas

Introduction To play with huge amounts of data, in python we require a tool. The tool which is available in Python is pandas. A panda is an open-source library. It is...

4 minutes read.

Read numpy array in Python

Numpy is a numerical python that deals with multidimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library provides multidimensional array...

9 minutes read.

Filter List in Python

Python: Python is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programming...

3 minutes read.

Attributes in python

In this article, we shall learn about attributes in python. Classes are a mix of data and functions, which in reality mean attributes and methods respectively. Typically, the body of a...

3 minutes read.

Checking whether a String Contains a Set of Characters in python

In this tutorial, we will learn how to examine or check whether a string contains any set of characters or a substring and if it contains, we will learn how...

6 minutes read.

Python Assert

Python Assert Python provides an assert statement which is used to check the logical expression. If the given logical expression is true, then it precedes for the next line; otherwise, it raises an...

2 minutes read.

SKLearn Model Selection

The model selections of SK learn has many functions with which we can work on.It has functions to cross-validate the model and, it also provides validation and learning curves.It is...

3 minutes read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.

Python Struct

Python 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 faster way....

3 minutes read.