Final Up to date on Could 6, 2022
There are alternative ways of debugging code in Python, one in every of which is to introduce breakpoints into the code at factors the place one wish to invoke a Python debugger. The statements used to enter a debugging session at completely different name websites depend upon the model of the Python interpreter that one is working with, as we will see on this tutorial.Â
On this tutorial, you’ll uncover varied methods of setting breakpoints in several variations of Python.Â
After finishing this tutorial, you’ll know:
- How one can invoke the
pdb
debugger in earlier variations of Python - How one can use the brand new, built-in
breakpoint() operate launched in Python 3.7 - How one can write your personal
breakpoint() operate to simplify the debugging course of in earlier variations of Python - How one can use a autopsy debugger
Let’s get began.Â

Setting Breakpoints in Totally different Variations of Python
Photograph by Josh Withers, some rights reserved.
Tutorial Overview
This tutorial is split into three components; they’re:
- Setting Breakpoints in Python Code
- Invoking the pdb Debugger in Earlier Variations of Python
- Utilizing the
breakpoint()
Perform in Python 3.7
- Writing One’s Personal
breakpoint()
Perform for Earlier Variations of Python - Limitations of the
breakpoint()
Perform
Setting Breakpoints in Python Code
We’ve beforehand seen that a method of debugging a Python script is to run it within the command line with the Python debugger.Â
So as to take action, we would wish to make use of the
–m pdb command that hundreds the pdb module earlier than executing the Python script. In the identical command-line interface, we might then comply with this by a selected debugger command of selection, comparable to
n to maneuver to the subsequent line or
s if we intend to step right into a operate.Â
This technique might turn out to be cumbersome rapidly because the size of the code will increase. One method to handle this drawback and acquire higher management over the place to interrupt your code is to insert a breakpoint immediately into the code.Â
Invoking the pdb Debugger in Earlier Variations of Python
Invoking the pdb debugger previous to Python 3.7 would require you to
import pdb and name
pdb.set_trace()Â on the level in your code the place you wish to enter an interactive debugging session.Â
If we rethink, for example, the code for implementing the overall consideration mechanism, we are able to break into the code as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
from numpy import array from numpy import random from numpy import dot from scipy.particular import softmax  # importing the Python debugger module import pdb  # encoder representations of 4 completely different phrases word_1 = array([1, 0, 0]) word_2 = array([0, 1, 0]) word_3 = array([1, 1, 0]) word_4 = array([0, 0, 1])  # stacking the phrase embeddings right into a single array phrases = array([word_1, word_2, word_3, word_4])  # producing the load matrices random.seed(42) W_Q = random.randint(3, dimension=(3, 3)) W_K = random.randint(3, dimension=(3, 3)) W_V = random.randint(3, dimension=(3, 3))  # producing the queries, keys and values Q = dot(phrases, W_Q) Okay = dot(phrases, W_K) V = dot(phrases, W_V)  # inserting a breakpoint pdb.set_trace()  # scoring the question vectors towards all key vectors scores = dot(Q, Okay.transpose())  # computing the weights by a softmax operation weights = softmax(scores / Okay.form[1] ** 0.5, axis=1)  # computing the eye by a weighted sum of the worth vectors consideration = dot(weights, V)  print(consideration) |
Executing the script now opens up the pdb
debugger proper earlier than we compute the variable scores
, and we are able to proceed to situation any debugger instructions of selection, comparable to
n to maneuver to the subsequent line or
c to proceed execution:
/Customers/multi level marketing/important.py(33)<module>() -> scores = dot(Q, Okay.transpose()) (Pdb) n > /Customers/multi level marketing/important.py(36)<module>() -> weights = softmax(scores / Okay.form[1] ** 0.5, axis=1) (Pdb) c [[0.98522025 1.74174051 0.75652026] [0.90965265 1.40965265 0.5Â Â Â Â Â Â ] [0.99851226 1.75849334 0.75998108] [0.99560386 1.90407309 0.90846923]] |
Though useful, this isn’t probably the most elegant and intuitive method of inserting a breakpoint into your code. Python 3.7 implements a extra easy approach of doing so, as we will see subsequent.
Utilizing the breakpoint() Perform in Python 3.7Â
Python 3.7 comes with a built-in
breakpoint()Â operate that enters the Python debugger on the name website (or the purpose within the code at which the
breakpoint()Â assertion is positioned).Â
When referred to as, the default implementation of the
breakpoint() operate will name
sys.breakpointhook(), which in flip calls the
pdb.set_trace()Â operate. That is handy as a result of we is not going to must
import pdb and name
pdb.set_trace()Â explicitly ourselves.Â
Let’s rethink the code for implementing the overall consideration mechanism and now introduce a breakpoint by way of the
breakpoint() assertion:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
from numpy import array from numpy import random from scipy.particular import softmax  # encoder representations of 4 completely different phrases word_1 = array([1, 0, 0]) word_2 = array([0, 1, 0]) word_3 = array([1, 1, 0]) word_4 = array([0, 0, 1])  # stacking the phrase embeddings right into a single array phrases = array([word_1, word_2, word_3, word_4])  # producing the load matrices random.seed(42) W_Q = random.randint(3, dimension=(3, 3)) W_K = random.randint(3, dimension=(3, 3)) W_V = random.randint(3, dimension=(3, 3))  # producing the queries, keys and values Q = phrases @ W_Q Okay = phrases @ W_Okay V = phrases @ W_V  # inserting a breakpoint breakpoint()  # scoring the question vectors towards all key vectors scores = Q @ Okay.transpose()  # computing the weights by a softmax operation weights = softmax(scores / Okay.form[1] ** 0.5, axis=1)  # computing the eye by a weighted sum of the worth vectors consideration = weights @ V  print(consideration) |
One benefit of utilizing the
breakpoint() operate is that, in calling the default implementation of
sys.breakpointhook(), the worth of a brand new atmosphere variable,
PYTHONBREAKPOINT, is consulted. This atmosphere variable can take varied values, based mostly on which completely different operations may be carried out.Â
For instance, setting the worth of
PYTHONBREAKPOINT to 0 disables all breakpoints. Therefore, your code might comprise as many breakpoints as essential, however these may be simply stopped from halting the execution of the code with out having to take away them bodily. If (for instance) the title of the script containing the code is important.py, we might disable all breakpoints by calling it within the command line interface as follows:
PYTHONBREAKPOINT=0 python important.py |
In any other case, we are able to obtain the identical end result by setting the atmosphere variable within the code itself:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
from numpy import array from numpy import random from scipy.particular import softmax  # setting the worth of the PYTHONBREAKPOINT atmosphere variable import os os.environ[‘PYTHONBREAKPOINT’] = ‘0’  # encoder representations of 4 completely different phrases word_1 = array([1, 0, 0]) word_2 = array([0, 1, 0]) word_3 = array([1, 1, 0]) word_4 = array([0, 0, 1])  # stacking the phrase embeddings right into a single array phrases = array([word_1, word_2, word_3, word_4])  # producing the load matrices random.seed(42) W_Q = random.randint(3, dimension=(3, 3)) W_K = random.randint(3, dimension=(3, 3)) W_V = random.randint(3, dimension=(3, 3))  # producing the queries, keys and values Q = phrases @ W_Q Okay = phrases @ W_Okay V = phrases @ W_V  # inserting a breakpoint breakpoint()  # scoring the question vectors towards all key vectors scores = Q @ Okay.transpose()  # computing the weights by a softmax operation weights = softmax(scores / Okay.form[1] ** 0.5, axis=1)  # computing the eye by a weighted sum of the worth vectors consideration = weights @ V  print(consideration) |
The worth of
PYTHONBREAKPOINT is consulted each time that
sys.breakpointhook() known as. Which means that the worth of this atmosphere variable may be modified through the code execution, and the
breakpoint() operate would reply accordingly. Â
The
PYTHONBREAKPOINT atmosphere variable may also be set to different values, such because the title of a callable. Say, for example, that we’d like to make use of a distinct Python debugger aside from pdb, comparable to ipdb (run
pip set up ipdb first if the debugger has not but been put in). On this case, we might name the important.py script within the command line interface and hook the debugger with out making any adjustments to the code itself:
PYTHONBREAKPOINT=ipdb.set_trace python important.py |
In doing so, the
breakpoint() operate enters the ipdb debugger on the subsequent name website:
> /Customers/Stefania/Paperwork/PycharmProjects/BreakpointPy37/important.py(33)<module>() Â Â Â Â 32 # scoring the question vectors towards all key vectors —> 33 scores = Q @ Okay.transpose() Â Â Â Â 34 Â ipdb> n > /Customers/Stefania/Paperwork/PycharmProjects/BreakpointPy37/important.py(36)<module>() Â Â Â Â 35 # computing the weights by a softmax operation —> 36 weights = softmax(scores / Okay.form[1] ** 0.5, axis=1) Â Â Â Â 37 Â ipdb> c [[0.98522025 1.74174051 0.75652026] [0.90965265 1.40965265 0.5Â Â Â Â Â Â ] [0.99851226 1.75849334 0.75998108] [0.99560386 1.90407309 0.90846923]] |
The operate can even take enter arguments as
breakpoint(*args, **kws), that are then handed on to
sys.breakpointhook(). It is because any callable (comparable to a third-party debugger module) would possibly settle for optionally available arguments, which may be handed by way of the
breakpoint() operate.Â
Writing Your Personal breakpoint() Perform in Earlier Variations of Python
Let’s return to the truth that variations of Python sooner than v3.7 don’t include the
breakpoint() operate readily inbuilt. We are able to write our personal.Â
Equally to how the
breakpoint() operate is carried out from Python 3.7 onwards, we are able to implement a operate that checks the worth of an atmosphere variable and:
- Skips all breakpoints within the code if the worth of the atmosphere variable is about to 0.
- Enters into the default Python pdb debugger if the atmosphere variable is an empty string.
- Enters into one other debugger as specified by the worth of the atmosphere variable.Â
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
...  # defining our breakpoint() operate def breakpoint(*args, **kwargs):     import importlib     # studying the worth of the atmosphere variable     val = os.environ.get(‘PYTHONBREAKPOINT’)     # if the worth has been set to 0, skip all breakpoints     if val == ‘0’:         return None     # else if the worth is an empty string, invoke the default pdb debugger     elif len(val) == 0:         hook_name = ‘pdb.set_trace’     # else, assign the worth of the atmosphere variable     else:         hook_name = val     # break up the string into the module title and the operate title     mod, dot, func = hook_name.rpartition(‘.’)     # get the operate from the module     module = importlib.import_module(mod)     hook = getattr(module, func)      return hook(*args, **kwargs)  ... |
We are able to embody this operate into the code and run it (utilizing a Python 2.7 interpreter, on this case). If we set the worth of the atmosphere variable to an empty string, we discover that the pdb debugger stops on the level within the code at which we have now positioned our
breakpoint() operate. We are able to then situation debugger instructions into the command line from there onwards:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
from numpy import array from numpy import random from numpy import dot from scipy.particular import softmax  # setting the worth of the atmosphere variable import os os.environ[‘PYTHONBREAKPOINT’] = ”   # defining our breakpoint() operate def breakpoint(*args, **kwargs):     import importlib     # studying the worth of the atmosphere variable     val = os.environ.get(‘PYTHONBREAKPOINT’)     # if the worth has been set to 0, skip all breakpoints     if val == ‘0’:         return None     # else if the worth is an empty string, invoke the default pdb debugger     elif len(val) == 0:         hook_name = ‘pdb.set_trace’     # else, assign the worth of the atmosphere variable     else:         hook_name = val     # break up the string into the module title and the operate title     mod, dot, func = hook_name.rpartition(‘.’)     # get the operate from the module     module = importlib.import_module(mod)     hook = getattr(module, func)      return hook(*args, **kwargs)   # encoder representations of 4 completely different phrases word_1 = array([1, 0, 0]) word_2 = array([0, 1, 0]) word_3 = array([1, 1, 0]) word_4 = array([0, 0, 1])  # stacking the phrase embeddings right into a single array phrases = array([word_1, word_2, word_3, word_4])  # producing the load matrices random.seed(42) W_Q = random.randint(3, dimension=(3, 3)) W_K = random.randint(3, dimension=(3, 3)) W_V = random.randint(3, dimension=(3, 3))  # producing the queries, keys and values Q = dot(phrases, W_Q) Okay = dot(phrases, W_K) V = dot(phrases, W_V)  # inserting a breakpoint breakpoint()  # scoring the question vectors towards all key vectors scores = dot(Q, Okay.transpose())  # computing the weights by a softmax operation weights = softmax(scores / Okay.form[1] ** 0.5, axis=1)  # computing the eye by a weighted sum of the worth vectors consideration = dot(weights, V)  print(consideration) |
> /Customers/Stefania/Paperwork/PycharmProjects/BreakpointPy27/important.py(32)breakpoint()->None -> return hook(*args, **kwargs) (Pdb) n > /Customers/Stefania/Paperwork/PycharmProjects/BreakpointPy27/important.py(59)<module>() -> scores = dot(Q, Okay.transpose()) (Pdb) n > /Customers/Stefania/Paperwork/PycharmProjects/BreakpointPy27/important.py(62)<module>() -> weights = softmax(scores / Okay.form[1] ** 0.5, axis=1) (Pdb) c [[0.98522025 1.74174051 0.75652026] [0.90965265 1.40965265 0.5Â Â Â Â Â Â ] [0.99851226 1.75849334 0.75998108] [0.99560386 1.90407309 0.90846923]] |
Equally, if we set the atmosphere variable to:
os.environ[‘PYTHONBREAKPOINT’] = ‘ipdb.set_trace’ |
The
breakpoint() operate that we have now carried out now enters the ipdb debugger and stops on the name website:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
> /Customers/Stefania/Paperwork/PycharmProjects/BreakpointPy27/important.py(31)breakpoint() Â Â Â Â 30 —> 31Â Â Â Â return hook(*args, **kwargs) Â Â Â Â 32 Â ipdb> n > /Customers/Stefania/Paperwork/PycharmProjects/BreakpointPy27/important.py(58)<module>() Â Â Â Â 57 # scoring the question vectors towards all key vectors —> 58 scores = dot(Q, Okay.transpose()) Â Â Â Â 59 Â ipdb> n > /Customers/Stefania/Paperwork/PycharmProjects/BreakpointPy27/important.py(61)<module>() Â Â Â Â 60 # computing the weights by a softmax operation —> 61 weights = softmax(scores / Okay.form[1] ** 0.5, axis=1) Â Â Â Â 62 Â ipdb> c [[0.98522025 1.74174051 0.75652026] [0.90965265 1.40965265 0.5Â Â Â Â Â Â ] [0.99851226 1.75849334 0.75998108] [0.99560386 1.90407309 0.90846923]] |
Setting the atmosphere variable to 0 merely skips all breakpoints, and the computed consideration output is returned within the command line, as anticipated:
os.environ[‘PYTHONBREAKPOINT’] = ‘0’ |
[[0.98522025 1.74174051 0.75652026] [0.90965265 1.40965265 0.5Â Â Â Â Â Â ] [0.99851226 1.75849334 0.75998108] [0.99560386 1.90407309 0.90846923]] |
This facilitates the method of breaking into the code for Python variations sooner than v3.7 as a result of it now turns into a matter of setting the worth of an atmosphere variable reasonably than having to manually introduce (or take away) the
import pdb; pdb.set_trace()Â assertion at completely different name websites within the code.Â
Limitations of the breakpoint()
Perform
The breakpoint()
operate means that you can deliver within the debugger in some unspecified time in the future in this system. You might want to discover the precise place that you simply want the debugger to place the breakpoint into it. In case you take into account the next code:
attempt: Â Â Â Â func() besides: Â Â Â Â breakpoint() Â Â Â Â print(“exception!”) |
This may deliver you the debugger when the operate func()
raised exceptions. It may be triggered by the operate itself or deep inside another features that it calls. However the debugger will begin on the line print("exception!")
above, which will not be very helpful.
The way in which that we are able to deliver up the debugger on the level of exception known as the autopsy debugger. It really works by asking Python to register the debugger pdb.pm()
because the exception handler when an uncaught exception is raised. When it’s referred to as, it is going to search for the final exception raised and begin the debugger at that time. To make use of the autopsy debugger, we simply want so as to add the next code earlier than this system is run:
import sys import pdb  def debughook(etype, worth, tb):     pdb.pm() # autopsy debugger sys.excepthook = debughook |
That is helpful as a result of nothing else must be modified in this system. For instance, assume we wish to consider the typical of $1/x$ utilizing the next program. It’s fairly straightforward to miss some nook instances, however we are able to catch the problem when an exception is raised:
import sys import pdb import random  def debughook(etype, worth, tb):     pdb.pm() # autopsy debugger sys.excepthook = debughook  # Experimentally discover the typical of 1/x the place x is a random integer in 0 to 9999 N = 1000 randomsum = 0 for i in vary(N):     x = random.randint(0,10000)     randomsum += 1/x  print(“Common is”, randomsum/N) |
Once we run the above program, this system could terminate, or it might increase a division by zero exception, relying on whether or not the random quantity generator ever produces zero within the loop. In that case, we might even see the next:
> /Customers/multi level marketing/py_pmhook.py(17)<module>() -> randomsum += 1/x (Pdb) p i 16 (Pdb) p x 0 |
the place we discovered the exception is raised at which line, and we are able to verify the worth of the variables as we are able to normally do in pdb
.
Actually, it’s extra handy to print the traceback and the exception when the autopsy debugger is launched:
import sys import pdb import traceback  def debughook(etype, worth, tb):     traceback.print_exception(etype, worth, tb)     print() # make a brand new line earlier than launching autopsy     pdb.pm() # autopsy debugger sys.excepthook = debughook |
And the debugger session might be began as follows:
Traceback (most latest name final):   File “/Customers/multi level marketing/py_pmhook.py”, line 17, in <module>     randomsum += 1/x ZeroDivisionError: division by zero  > /Customers/multi level marketing/py_pmhook.py(17)<module>() -> randomsum += 1/x (Pdb) |
Additional Studying
This part supplies extra sources on the subject if you’re trying to go deeper.
Web sites
Abstract
On this tutorial, you found varied methods of setting breakpoints in several variations of Python.Â
Particularly, you realized:
- How one can invoke the pdb debugger in earlier variations of Python
- How one can make use of the brand new, built-in
breakpoint() operate launched in Python 3.7 - How one can write your personal
breakpoint() operate to simplify the debugging course of in earlier variations of Python
Do you might have any questions?
Ask your questions within the feedback under, and I’ll do my finest to reply.