Signup/Sign In

Python breakpoint() - Improved Debugging starting from Python 3.7

Posted in Programming   LAST UPDATED: OCTOBER 14, 2019

    Sometimes while you are working on some script, it might be required to debug the code line by line to determine the error and fix it. Going through every line of code manually is a tedious task. This is where Python's debugging facility comes into the picture. Debugging can be done using pdb.set_trace() or other available debuggers.

    In addition to this, Python 3.7 has newly introduced built-in method breakpoint() which is much better in comparison to pdb.set_trace(). In functioning it is very much similar to pdb.set_trace available in earlier versions of Python.

    The pdb in pdb.set_trace() is a Python module that helps in code debugging.

    Below are a few commonly used debugging commands available in the pdb module, which we have used in the code examples ahead.

    c : continue executing the code
    n : step to next line present within the same method
    q : quit the debugger/execution of a program
    s : step to next line in that specific function or a called function


    Time for an example:

    The below program demonstrates the usage of pdb module and the function set_trace. In the below code, the pdb.set_trace has been inserted after the my_str has been displayed. Post this, the execution halts and upon clicking on the letter c, it continues executing like a normal Python program.

    import pdb
    
    x = 10
    my_str = "Hello"
    my_str_two = 'Studytonight'
    print(my_str)
    
    # program execution will pause here
    pdb.set_trace()
    
    print(my_str_two)

    Output:

    Hello
    --Return--
    None
    > <ipython-input-1-858df61efefd>(7)<module>()
    5 print(my_str)
    6
    ----> 7 pdb.set_trace()
    8
    9 print(my_str_two)
    
    ipdb>
    ipdb>

    Upon clicking on the letter c, the execution of the program continues and it prints 'Studytonight'.

    The final output:

    Hello
    --Return--
    None
    > <ipython-input-1-858df61efefd>(7)<module>()
    5 print(my_str)
    6
    ----> 7 pdb.set_trace()
    8
    9 print(my_str_two)
    
    ipdb>
    ipdb> c
    Studytonight

    When the execution of the program is paused, you can type in name of variables to see their values, to debug the execution of your code.


    Why not use pdb.set_trace()? Why introduce a new method named breakpoint()?

    Consider the below situation:

    1. You have imported the pdb module and wish to use pdb.set_trace(). After some time, you see that there is a requirement for you to use a different debugger (say web_pdb or any third-party debugger). You will have to remove all instances of pdb.set_trace() and replace them with web_pdb.set_trace(). This adds to the overhead of program maintenance.

    2. In cases where advanced debugging is needed, pdb.set_trace() falls short.

    3. In cases where a debugging session is needed or when an application isn't a console application, pdb.set_trace() falls behind.

    This is when breakpoint comes into play.

    When the breakpoint() is called, it calls sys.breakpointhook() under the hood. Further, sys.breakpointhook() calls pdb.set_trace() by default. This means there is no need to explicitly import the pdb module and call its set_trace() method when using breakpoint(). If you wish to use some other debugger, the breakpoint method can be disabled with the help of the PYTHONBREAKPOINT environment variable.

    Stop Debugging: If you wish to execute the breakpoints inserted in your code just once, set the environment variable PYTHONBREAKPOINT to 0. The next time the code is executed, the breakpoint is not even called.


    Time for an example:

    The below example has a breakpoint inserted right after the function is declared. Hence the execution stops the moment the function is called. Clicking on the letter c (which indicates the continuation of execution of code) continues the code execution, and results in an error since a number divided by 0 is invalid.

    def sample_debugger(a, b): 
        # adding a breakpoint
        breakpoint() 
        result = ((a/b) * (a+b))
        return result 
      
    print(sample_debugger(12, 0))

    Output:

    ipdb> 
    ipdb>

    The letter c is clicked on and below is the final output:

    ipdb>
    ipdb> c
    Traceback (most recent call last):
    
      File "<ipython-input-25-7ab2497ae08a>", line 7, in <module>
        print(sample_debugger(12, 0))
    
      File "<ipython-input-25-7ab2497ae08a>", line 3, in sample_debugger
        result = ((a/b) * (a+b))
    
    ZeroDivisionError: division by zero
    

    What about the third-party debuggers which could be used after calling breakpoint?

    Yes, this can be done by setting the name of the environment variable PYTHONBREAKPOINT to the callable's name. This callable will start the debugging session with the help of another third party debugging library. An example is pudb, which is very much similar to pdb but easier to use, or if you want to use the web-pdb for debugging, all you have to do is set the PYTHONBREAKPOINT environment variable to web-pdb.set_trace.

    If your code is saved in the file python-debug.py, run the following command to change the debugger using the breakpoint environment variable:

    PYTHONBREAKPOINT=web_pdb.set_trace python3.7 python_debug.py

    Similary, while executing your program you can even choose to stop debugging, by setting this environment variable to 0.




    Conclusion

    In this post, we saw how breakpoint can be used to debug Python code, as well as use debuggers of your choice instead of the default debugger to troubleshoot the code. Also, with the recent development in the python programming language, we can see that the modules that were popular amongst the developer community are being included in the python default package.

    About the author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    Tags:PythonPython breakpointDebugging
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS