Tutorial

Python breakpoint()

Published on August 3, 2022
Default avatar

By Pankaj

Python breakpoint()

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Python breakpoint() is a new built-in function introduced in Python 3.7. Python code debugging has always been a painful process because of the tight coupling between the actual code and the debugging module code. For example, if you are using pdb debugger, then you will have to call pdb.set_trace() in your program code. If you want to use any other debugger, let’s say web-pdb then you will have to remove all the code related to PDB and add web_pdb.set_trace() method. This causes a huge overhead in using python debugger and makes the python code hard to debug and maintain. That’s why Python 3.7 has introduced breakpoint() method that allows us to write loosely coupled debugging code.

Python breakpoint()

Python breakpoint() function calls sys.breakpointhook() function. By default, sys.breakpointhook() calls pdb.set_trace() function. So at the very least, using breakpoint() provide convenience in using a debugger because we don’t have to explicitly import pdb module. Let’s look at a simple example of breakpoint() function usage. We have a python script python_breakpoint_examples.py with the following code.

x = 10
y = 'Hi'
z = 'Hello'
print(y)

breakpoint()

print(z)

When we execute this script, the PDB debugger console opens up.

$python3.7 python_breakpoint_examples.py
Hi
> /Users/pankaj/Documents/PycharmProjects/BasicPython/basic_examples/python_breakpoint_examples.py(8)()
-> print(z)
(Pdb) c
Hello
$

python breakpoint example

Python breakpoint() - Stop Debugging

Python sys.breakpointhook() function uses environment variable PYTHONBREAKPOINT to configure the debugger. If unset, the default PDB debugger is used. If it’s set to “0” then the function returns immediately and no code debugging is performed. It’s very helpful when we want to run our code without debugging.

$PYTHONBREAKPOINT=0 python3.7 python_breakpoint_examples.py
Hi
Hello
$

python breakpoint stop debugging

Python breakpoint() - Change Debugger Module

We can use PYTHONBREAKPOINT environment variable to provide the debugger method to be called by breakpoint() function. This is very helpful because we can change the debugger module easily without making any code change. Let’s say we want to use web-pdb debugger. We can easily hook it into our program using PYTHONBREAKPOINT=web_pdb.set_trace. First of all, make sure that web-pdb is installed. You can install it using pip3.7 install web-pdb command.

According to web-pdb documentation, it’s compatible with the new breakpoint() function added in Python 3.7.

$PYTHONBREAKPOINT=web_pdb.set_trace python3.7 python_breakpoint_examples.py
Hi
2018-08-10 12:49:54,339: root - web_console:110 - CRITICAL - Web-PDB: starting web-server on pankaj:5555...

python breakpoint change debugger Open the web-server URL provided in the console log and you will see the debugger window like the below image. python web-pdb debugger We can issue PDB commands using this UI, you can send command “c” to continue and complete our program.

Summary

Python breakpoint() function is a very helpful addition to the python debugging feature. It’s recommended to use this for debugging so that you can easily hook other third-party debuggers on the fly. It also provides an easy option to disable the debugger and runs the program normally.

You can check out more Python examples from our GitHub Repository.

Reference: Official Documentation

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
January 31, 2020

thanks pankaj

- sakldakldja

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel