×

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

Accessing Key-value in Dictionary in Python

A Python word reference is an assortment of key-esteem matches where each key is related to worth. Worth in the key-esteem pair can be a number, a string, a rundown,...

5 minutes read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

Raise Exception in Python

Most of the programmers/developers create large programs to solve complex tasks in Python. The code can be of 1000 lines and even more based on the need of the problem....

5 minutes read.

Python String find() method

Python String find() method The string.find() method in Python finds the first occurrence of the specified value and returns the lowest index in the string if the substring sub is found else it...

2 minutes read.

FOO in Python

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

Python callable() Function

Python callable() Function The callable() function returns a boolean value ‘True’ if the specified object is callable, else it returns False. Syntax callable(object) Parameter Object:  The object parameter represents the value to test if it is callable...

1 minute read.

Python Dictionary pop() method

Python Dictionary pop() method The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key. Syntax dictionary.pop(key[, default]) Parameter key – This argument signifies the key which is to...

2 minutes read.

Python pynmea2

Define Pynmea2 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...

4 minutes read.

Python if statement

Python if statement Decision making is an essential feature of any programming languages. Condition checking is the strength of decision making. Python provides many decision-making statements, such as: If statementIf-else statementelif statement if statement The if statement is...

2 minutes read.

Excel to CSV in Python

Define MS Excel Microsoft Excel is an application provided by the Microsoft corporation which allows us to build graphs to build tables, and it is also used for the macro programming...

4 minutes read.

Python lambda() Function

In this tutorial, we will learn about a new concept known as the Lambda function. It is a relatively new concept while learning Python. Python lambda functions are anonymous functions. It...

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.

Difference between Package and Module in Python

What are Python modules? A file with the “.py” suffix that includes Python or C executable code is known as a module. Multiple Python commands and expressions make compose a module....

3 minutes read.

Python OOPs: Class, Object, Inheritance and Constructor

Basic Object-Oriented Programming (OOP) Concepts in Python Python is similar to most general-purpose programming languages with an Object-Oriented environment system since its existence. Being an Object-Oriented Programming language, Python provides ease...

9 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

Find Median of List in Python

The median is an enlightening measurement that is utilized as a proportion of the focal inclination of a circulation. It is equivalent to the centre worth of the conveyance. There...

3 minutes read.

Executing Shell Commands in Python

This tutorial aims to make us understand what a Shell is, what is the importance of a Shell, what are Shell commands in Python, and how can we execute the...

3 minutes read.

Python for Loop Increment

Introduction In general, loops are employed for sequential traversal. It belongs to the definite iteration category. Definite iterations imply that the number of iterations is explicitly set in advance.  In this article,...

4 minutes read.

Python Set issuperset() method

Python Set issuperset() method The set.issuperset() method in Python returns a boolean value True if all items in the specified set exists in the original set, else it returns False. Syntax set.issuperset (set1) Parameter set- This parameter represents the...

2 minutes read.