your image

Debugging Python code using breakpoint() and pdb - GeeksforGeeks

Jitender_1998
.geeksforgeeks.org/
Related Topic
:- Strong debugging skills Code Debugging Python

Debugging Python code using breakpoint() and pdb

  • Difficulty Level : Medium
  • Last Updated : 23 Feb, 2019

While developing an application or exploring some features of a language, one might need to debug the code anytime. Therefore, having an idea of debugging the code is quite necessary. Let’s see some basics of debugging using built-in function breakpoint() and pdb module.

We know that debugger plays an important role when we want to find a bug in a particular line of code. Here, Python comes with the latest built-in function breakpoint which do the same thing as pdb.set_trace() in Python 3.6 and below versions.

Debugger finds the bug in the code line by line where we add the breakpoint, if a bug is found then program stops temporarily then you can remove the error and start to execute the code again.

Syntax:

1) breakpoint()           # in Python 3.72) import pdb; pdb.set_trace()   # in Python 3.6 and below

 
Method #1 : Using breakpoint() function
In this method, we simply introduce the breakpoint where you have doubt or somewhere you want to check for bugs or errors.

 

 

 

 

 

 

def debugger(a, b):

    breakpoint()

    result = a / b

    return result

  

print(debugger(5, 0))

Output :

In order to run the debugger just type c and press enter.

 

Commands for debugging :

c -> continue executionq -> quit the debugger/executionn -> step to next line within the same functions -> step to next line in this function or a called function

 

Method #2 : Using pdb module
As the same suggests, PDB means Python debugger. To use the PDB in the program we have to use one of its method named set_trace(). Although this will result the same but this the another way to introduce the debugger in python version 3.6 and below.

 

 

 

def debugger(a, b):

    import pdb; pdb.set_trace()

    result = a / b

    return result

  

print(debugger(5, 0))

Output :

In order to run the debugger just type c and press enter.

Example :

 

 

 

def debugger(a):

    import pdb; pdb.set_trace()

    result = [a[element] for element in range(0, len(a)+5)]

    return result

  

print(debugger([1, 2, 3]))

Output :

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

 

 

 

Like

0

Next

Check multiple conditions in if statement - Python

RECOMMENDED ARTICLES

Page :

Debugging in Python with Pdb

16, Mar 21

Python Debugger – Python pdb

02, Jan 21

Python | Basic Program Crash Debugging

10, Jun 19

Debugging decorators in Python

19, Aug 20

Performing Google Search using Python code

06, Jun 17

Python | Generate QR Code using pyqrcode module

05, Dec 18

Issues with using C code in Python | Set 1

19, Mar 19

Issues with using C code in Python | Set 2

20, Mar 19

Convert Python Code to a Software to Install on Windows Using Inno Setup Compiler

15, Jul 20

Application to get address details from zip code Using Python

22, Sep 20

Wi-Fi QR Code Generator Using Python

22, Sep 20

Get Bank details from IFSC Code Using Python

06, Oct 20

Create a GUI to search bank information with IFSC Code using Python

06, Oct 20

Get MICR Code using Python

20, Oct 20

Get Zip Code with given location using GeoPy in Python

20, Oct 20

Get Indian Railways Station code Using Python

20, Oct 20

Generate QR Code using qrcode in Python

13, Jan 21

Auto Search StackOverflow for Errors in Code using Python

16, Mar 21

Convert HTML source code to JSON Object using Python

03, Mar 21

Python code formatting using Black

31, Dec 19

Python - Morse Code Translator GUI using Tkinter

21, May 20

Reusable piece of python functionality for wrapping arbitrary blocks of code : Python Context Managers

31, Jul 20

Extract the HTML code of the given tag and its parent using BeautifulSoup

16, Mar 21

Packaging and Publishing Python code

06, Jan 17

Article Contributed By :

 

Jitender_1998

@Jitender_1998

Comments