Tutorials  /  Python

Python breakpoint() function

Ccentron Redaktion · February 2024 ·3 min read ·Python, 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.

How Python breakpoint() Works

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.

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

breakpoint()

print(z)

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

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

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Stopping Debugging with Python breakpoint()

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.

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

Changing Debugger Module with Python breakpoint()

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.

Code
$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...

Open the web-server URL provided in the console log and you will see the debugger window. 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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren