A software program called a debugger or troubleshooting tool is used to test and debug other applications (the “target” program). A debugger’s primary job is to execute the target program under controlled circumstances so that the programmer may follow its progress and keep an eye on any changes in the system resources that can point to broken code. The capability to execute or stop the target program at key locations, showcase the substance of recollection, CPU registers, or storage devices (like hard disks), and modify memory or register contents to enter chosen test data that may be the cause of incorrect program execution are examples of typical debugging facilities.
What is Debugging?
Software troubleshooting can occasionally be an unwanted task. You’re pressed for time and working hard to get things done. While studying a fresh feature or attempting a novel strategy, you can at other times desire a deeper comprehension of how something operates.
A dynamic system software analyzer for Python programs is provided by the pdb package. In addition to allowing for the establishment of breakpoints and individual skipping at the code line level, it also allows for the inspection of pile framing, the listing of the source code, and the execution of random Python script within any packet header. It is also capable of being called from within a system and provides post-mortem debugging.
The debugger can be extended because it is declared as the class Pdb. Although not currently detailed, reading the source makes it clear what is going on. The packages bdb and cmd are used by the enhanced version. The command for the debugger is pdb.
Activation of the Python debugger
You only need to enter the import pdb and pdb.set trace() statements to begin debugging the application. Execute your code ordinarily, and the timeout we set will cause processing to end. Consequently, we are essentially hard-wiring a breakpoint on the line below when we execute set trace (). Breakpoint(), a built-in function in Python 3.7 and subsequent versions, performs the same job. For information on inserting the set trace() function, see the sample below.
Using the Python pdb package to debug a Python script that multiplies numbers:
We cannot multiply strings since input() returns a string. Thus, ValueError will be raised.
Input:
import pdb
def multiply(p,q):
result = p*q
return result
pdb.set_trace()
p = input("First no. is: ")
q = input("Second no. is : ")
product = multiply(p,q)
print(product)
Output:
(Pdb)
The directory path to our file, the row position wherein our timeout is located, and the word “module” can all be found in the result on the initial line following the angle bracket. Essentially, it indicates that line 10 of exppdb.py, the module level, has a breakpoint. If the breakpoint is added inside the code, its name will show up inside >. The program block where our operation is halted is displayed in the next line. This line still needs to be executed. The pdb prompt follows.
Using the pdb ‘whatis’ command to determine the type of a parameter
Input:
p = 70
q = 56
h = 0
for i in range(p):
# this line will raise a ZeroDivision error
h += p/q
q -= 1
Output:
File “TechVidvan.py”, line 14, in <module>
h += p/q
ZeroDivisionError: division by zero
Using the Python pdb module for Post-mortem troubleshooting
Fixing in post-mortem mode refers to doing so after the code has completed its execution phase. The pm() and post mortem() functions in pdb support post-mortem debugging. When one of these procedures detects an active traceback, it starts the logger at the call stack line where the exception first appeared. When a program encounters an exception, you may see pdb appear in the result of the provided example.
Input:
def product(p,q):
ans = p * q
return ans
g = input("First no. is: ")
h = input("Second no. is: ")
op = product(g,h)
print(op)
Output:
The 2nd number is : 5
Traceback (most recent call last):
File “main.py”, line 15, in <module>
op = product(g,h)
File “main.py”, line 9, in product
ans = p * q
TypeError: can’t multiply sequence by non-int of type ‘str’
Parameter verification on the stack
The stack keeps track of all variables, including both instance variables and global variables specific to the method utilized by the code. To display all the parameters for a function that is presently active, use args. The p instruction assesses an equation that is sent to it and outputs the outcome.
Using and managing pdb Breakpoints in Python
We frequently wish to put a lot of set points wherever we anticipate failures might occur while dealing with large programs. You only need to utilize the break command to accomplish this. The logger gives a breakpoint a number, starting at 1, when you enter it.
We may control the thresholds using the enable, disable, and remove commands after adding them with the help of the integers supplied to them. Enabling switches on the deactivated breakpoints while disabled instructs the analyzer not to halt whenever that breakpoint is encountered.
Python Debugger commands
Below is a list of the commands that the debugger can recognize. The majority of instructions can be expressed including one or more alphabets as stated; for example, the command “help” can be entered using either the letter “h” or the letter “help”. Command arguments need to be isolated by spaces. The command syntax encloses optional arguments in square brackets ([]); the square brackets cannot be entered. A vertical line (|) is used in the program syntax to divide options.
1. help
Display the set of possible commands without a response. Print help for a command with that command as the parameter. help pdb provides the complete manual. To just get help on the command, you must type help exec because the command parameter needs to be an identifier.
2. where
The much more recent frame should be at the bottom of a printed stack trace. The recent frame, which defines the meaning of most commands, is shown by an arrow.
3. break
Create a break in the media library with a line_no parameter. Set a break at the function’s first executable statement using the function parameter. To set a halt in a different file (perhaps one that hasn’t been loaded yet), the line position may be prefixed by a directory and a colon. On sys.path, the file is searched. Keep in mind that every breakpoint has a unique number that all other breakpoint commands relate to.
If there is an additional parameter, it is an argument that needs to evaluate to be true for the breakpoint to be fulfilled.
Display all breakdowns, including the count of times each breakpoint has been reached, the present ignores count, and, if applicable, any associated conditions.
4. ignore
For the specified timeout number, set the ignore count. The disregard limit is lowered to 0 if the number is not provided. As soon as the ignore count reaches 0, a breakpoint is activated. When non-zero, any connected condition that evaluates to true and the timeout is not deactivated causes the count to drop every moment the breakpoint is reached.
5. clear
Clear all requirements of specific at this line with the input file_name:line_no. Clear the breakpoints in the space-separated list of breakpoint numbers.
Conclusion
In this TechVidvan tutorial, you learned about what is debugging in Python along with its activation. The demonstration of the pdb module is given to understand post-mortem troubleshooting, and managing the breakpoints and it also includes an overview of some significant debugger commands.
You’ll notice that there are many applications for debugging. It’s not overly difficult, yet at the same moment, it will benefit you greatly. In addition to building your reputation as a talented developer, you’ll be able to avoid certain errors in the future and save a good amount of time and effort. Of course, if you utilize an appropriate debugger, both the company and your customer will benefit. So everyone benefits equally, in a real sense.

