In all programming workouts, it’s troublesome to go far and deep and not using a useful debugger. The built-in debugger, pdb
, in Python is a mature and succesful one that may assist us lots if you know the way to make use of it. On this tutorial we’re going see what the pdb
can do for you in addition to a few of its various.
On this tutorial you’ll study:
- What can a debugger do
- management a debugger
- The limitation of Python’s pdb and its alternate options
Let’s get began.

Python debugging instruments
Photograph by Thomas Park. Some rights reserved.
Tutorial Overview
This tutorial is in 4 components, they’re
- The idea of working a debugger
- Stroll-through of utilizing a debugger
- Debugger in Visible Studio Code
- Utilizing GDB on a working Python program
The idea of working a debugger
The aim of a debugger is to offer you a gradual movement button to regulate the circulate of a program. It additionally assist you to freeze this system at sure level of time and study the state.
The best operation underneath a debugger is to step by the code. That’s to run one line of code at a time and wait on your acknowledgment earlier than continuing into subsequent. The explanation we wish to run this system in a stop-and-go vogue is to permit us to examine the logic and worth or confirm the algorithm.
For a bigger program, we could not wish to step by the code from the start as it might take a very long time earlier than we reached the road that we’re enthusiastic about. Subsequently, debuggers additionally present a breakpoint characteristic that may kick in when a particular line of code is reached. From that time onward, we are able to step by it line by line.
Stroll-through of utilizing a debugger
Let’s see how we are able to make use of a debugger with an instance. The next is the Python code for displaying particle swarm optimization in an animation:
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 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation
def f(x,y): “Goal perform” return (x–3.14)**2 + (y–2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y–1.73)
# Compute and plot the perform in 3D inside [0,5]x[0,5] x, y = np.array(np.meshgrid(np.linspace(0,5,100), np.linspace(0,5,100))) z = f(x, y)
# Discover the worldwide minimal x_min = x.ravel()[z.argmin()] y_min = y.ravel()[z.argmin()]
# Hyper-parameter of the algorithm c1 = c2 = 0.1 w = 0.8
# Create particles n_particles = 20 np.random.seed(100) X = np.random.rand(2, n_particles) * 5 V = np.random.randn(2, n_particles) * 0.1
# Initialize knowledge pbest = X pbest_obj = f(X[0], X[1]) gbest = pbest[:, pbest_obj.argmin()] gbest_obj = pbest_obj.min()
def replace(): “Perform to do one iteration of particle swarm optimization” world V, X, pbest, pbest_obj, gbest, gbest_obj # Replace params r1, r2 = np.random.rand(2) V = w * V + c1*r1*(pbest – X) + c2*r2*(gbest.reshape(–1,1)–X) X = X + V obj = f(X[0], X[1]) pbest[:, (pbest_obj >= obj)] = X[:, (pbest_obj >= obj)] pbest_obj = np.array([pbest_obj, obj]).min(axis=0) gbest = pbest[:, pbest_obj.argmin()] gbest_obj = pbest_obj.min()
# Arrange base determine: The contour map fig, ax = plt.subplots(figsize=(8,6)) fig.set_tight_layout(True) img = ax.imshow(z, extent=[0, 5, 0, 5], origin=‘decrease’, cmap=‘viridis’, alpha=0.5) fig.colorbar(img, ax=ax) ax.plot([x_min], [y_min], marker=‘x’, markersize=5, coloration=“white”) contours = ax.contour(x, y, z, 10, colours=‘black’, alpha=0.4) ax.clabel(contours, inline=True, fontsize=8, fmt=“%.0f”) pbest_plot = ax.scatter(pbest[0], pbest[1], marker=‘o’, coloration=‘black’, alpha=0.5) p_plot = ax.scatter(X[0], X[1], marker=‘o’, coloration=‘blue’, alpha=0.5) p_arrow = ax.quiver(X[0], X[1], V[0], V[1], coloration=‘blue’, width=0.005, angles=‘xy’, scale_units=‘xy’, scale=1) gbest_plot = plt.scatter([gbest[0]], [gbest[1]], marker=‘*’, s=100, coloration=‘black’, alpha=0.4) ax.set_xlim([0,5]) ax.set_ylim([0,5])
def animate(i): “Steps of PSO: algorithm replace and present in plot” title = ‘Iteration {:02d}’.format(i) # Replace params replace() # Set image ax.set_title(title) pbest_plot.set_offsets(pbest.T) p_plot.set_offsets(X.T) p_arrow.set_offsets(X.T) p_arrow.set_UVC(V[0], V[1]) gbest_plot.set_offsets(gbest.reshape(1,–1)) return ax, pbest_plot, p_plot, p_arrow, gbest_plot
anim = FuncAnimation(fig, animate, frames=checklist(vary(1,50)), interval=500, blit=False, repeat=True) anim.save(“PSO.gif”, dpi=120, author=“imagemagick”)
print(“PSO discovered finest answer at f({})={}”.format(gbest, gbest_obj)) print(“World optimum at f({})={}”.format([x_min,y_min], f(x_min,y_min))) |
The particle swarm optimization is completed by executing the replace()
perform quite a lot of occasions. Every time it runs, we’re nearer to the optimum answer to the target perform. We’re utilizing matplotlib’s FuncAnimation()
perform as a substitute of a loop to run replace()
. So we are able to seize the place of the particles at every iteration.
Assume this program is saved as pso.py
, to run this program in command line is solely to enter:
and the answer shall be print out to the display and the animation shall be saved as PSO.gif
. But when we wish to run it with the Python debugger, we enter the next in command line:
The -m pdb
half is to load the pdb
module and let the module to execute the file pso.py
for you. Whenever you run this command, you can be welcomed with the pdb
immediate as follows:
> /Customers/multi level marketing/pso.py(1)<module>() -> import numpy as np (Pdb) |
On the immediate, you’ll be able to sort within the debugger instructions. To indicate the checklist of supported instructions, we are able to use h
. And to indicate the element of the precise command (resembling checklist
), we are able to use h checklist
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
> /Customers/multi level marketing/pso.py(1)<module>() -> import numpy as np (Pdb) h
Documented instructions (sort assist <subject>): ======================================== EOF c d h checklist q rv undisplay a cl debug assist ll give up s unt alias clear disable ignore longlist r supply till args instructions show work together n restart step up b situation down j subsequent return tbreak w break cont allow soar p retval u whatis bt proceed exit l pp run unalias the place
Miscellaneous assist matters: ========================== exec pdb
(Pdb) |
In the beginning of a debugger session, we begin with the primary line of this system. Usually a Python program would begin with a number of traces of import
. We are able to use n
to maneuver to the subsequent line, or s
to step right into a perform:
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 |
> /Customers/multi level marketing/pso.py(1)<module>() -> import numpy as np (Pdb) n > /Customers/multi level marketing/pso.py(2)<module>() -> import matplotlib.pyplot as plt (Pdb) n > /Customers/multi level marketing/pso.py(3)<module>() -> from matplotlib.animation import FuncAnimation (Pdb) n > /Customers/multi level marketing/pso.py(5)<module>() -> def f(x,y): (Pdb) n > /Customers/multi level marketing/pso.py(10)<module>() -> x, y = np.array(np.meshgrid(np.linspace(0,5,100), np.linspace(0,5,100))) (Pdb) n > /Customers/multi level marketing/pso.py(11)<module>() -> z = f(x, y) (Pdb) s –Name– > /Customers/multi level marketing/pso.py(5)f() -> def f(x,y): (Pdb) s > /Customers/multi level marketing/pso.py(7)f() -> return (x-3.14)**2 + (y-2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y-1.73) (Pdb) s –Return– > /Customers/multi level marketing/pso.py(7)f()->array([[17.25… 7.46457344]]) -> return (x-3.14)**2 + (y-2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y-1.73) (Pdb) s > /Customers/multi level marketing/pso.py(14)<module>() -> x_min = x.ravel()[z.argmin()] (Pdb) |
In pdb
, the road of code shall be printed earlier than the immediate. Often n
command is what we would like because it executes that line of code and strikes the circulate on the similar degree with out drill down deeper. Once we are at a line that calls a perform (resembling line 11 of the above program, that runs z = f(x, y)
) we are able to use s
to step into the perform. Within the above instance, we first step into f()
perform, then one other step to execute the computation, and at last, accumulate the return worth from the perform to provide it again to the road that invoked the perform. We see there are a number of s
command wanted for a perform so simple as one line as a result of discovering the perform from the assertion, calling the perform, and return every takes one step. We are able to additionally see that within the physique of the perform, we known as np.sin()
like a perform however the debugger’s s
command doesn’t go into it. It’s as a result of the np.sin()
perform isn’t applied in Python however in C. The pdb
doesn’t help compiled code.
If this system is lengthy, it’s fairly boring to make use of the n
command many occasions to maneuver to someplace we have an interest. We are able to use till
command with a line quantity to let the debugger run this system till that line is reached:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
> /Customers/multi level marketing/pso.py(1)<module>() -> import numpy as np (Pdb) till 11 > /Customers/multi level marketing/pso.py(11)<module>() -> z = f(x, y) (Pdb) s –Name– > /Customers/multi level marketing/pso.py(5)f() -> def f(x,y): (Pdb) s > /Customers/multi level marketing/pso.py(7)f() -> return (x-3.14)**2 + (y-2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y-1.73) (Pdb) s –Return– > /Customers/multi level marketing/pso.py(7)f()->array([[17.25… 7.46457344]]) -> return (x-3.14)**2 + (y-2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y-1.73) (Pdb) s > /Customers/multi level marketing/pso.py(14)<module>() -> x_min = x.ravel()[z.argmin()] (Pdb) |
A command much like till
is return
, which can execute the present perform till the purpose that it’s about to return. You possibly can take into account that as till
with the road quantity equal to the final line of the present perform. The till
command is one-off, which means it should convey you to that line solely. If you wish to cease at a specific line at any time when it’s being run, we are able to make a breakpoint on it. For instance, if we’re enthusiastic about how every iteration of the optimization algorithm strikes the answer, we are able to set a breakpoint proper after the replace is utilized:
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 |
> /Customers/multi level marketing/pso.py(1)<module>() -> import numpy as np (Pdb) b 40 Breakpoint 1 at /Customers/multi level marketing/pso.py:40 (Pdb) c > /Customers/multi level marketing/pso.py(40)replace() -> obj = f(X[0], X[1]) (Pdb) bt /usr/native/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Variations/3.9/lib/python3.9/bdb.py(580)run()
-> exec(cmd, globals, locals) <string>(1)<module>() /Customers/multi level marketing/pso.py(76)<module>() -> anim.save(“PSO.gif”, dpi=120, author=”imagemagick”) /usr/native/lib/python3.9/site-packages/matplotlib/animation.py(1078)save() -> anim._init_draw() # Clear the preliminary body /usr/native/lib/python3.9/site-packages/matplotlib/animation.py(1698)_init_draw() -> self._draw_frame(frame_data) /usr/native/lib/python3.9/site-packages/matplotlib/animation.py(1720)_draw_frame() -> self._drawn_artists = self._func(framedata, *self._args) /Customers/multi level marketing/pso.py(65)animate() -> replace() > /Customers/multi level marketing/pso.py(40)replace() -> obj = f(X[0], X[1]) (Pdb) p r1 0.8054505373292797 (Pdb) p r2 0.7543489945823536 (Pdb) p X array([[2.77550474, 1.60073607, 2.14133019, 4.11466522, 0.2445649 , 0.65149396, 3.24520628, 4.08804798, 0.89696478, 2.82703884, 4.42055413, 1.03681404, 0.95318658, 0.60737118, 1.17702652, 4.67551174, 3.95781321, 0.95077669, 4.08220292, 1.33330594], [2.07985611, 4.53702225, 3.81359193, 1.83427181, 0.87867832, 1.8423856 , 0.11392109, 1.2635162 , 3.84974582, 0.27397365, 2.86219806, 3.05406841, 0.64253831, 1.85730719, 0.26090638, 4.28053621, 4.71648133, 0.44101305, 4.14882396, 2.74620598]]) (Pdb) n > /Customers/multi level marketing/pso.py(41)replace() -> pbest[:, (pbest_obj >= obj)] = X[:, (pbest_obj >= obj)] (Pdb) n > /Customers/multi level marketing/pso.py(42)replace() -> pbest_obj = np.array([pbest_obj, obj]).min(axis=0) (Pdb) n > /Customers/multi level marketing/pso.py(43)replace() -> gbest = pbest[:, pbest_obj.argmin()] (Pdb) n > /Customers/multi level marketing/pso.py(44)replace() -> gbest_obj = pbest_obj.min() (Pdb) |
After we set a breakpoint with the b
command, we are able to let the debugger run our program till the breakpoint is hit. The c
command means to proceed till a set off is met. At any level, we are able to use bt
command to indicate the traceback to examine how we reached right here. We are able to additionally use the p
command to print the variables (or an expression) to examine what worth they’re holding.
Certainly, we are able to place a breakpoint with a situation, so that it’ll cease provided that the situation is met. The beneath will impose a situation that the primary random quantity (r1
) is larger than 0.5:
(Pdb) b 40, r1 > 0.5 Breakpoint 1 at /Customers/multi level marketing/pso.py:40 (Pdb) c > /Customers/multi level marketing/pso.py(40)replace() -> obj = f(X[0], X[1]) (Pdb) p r1, r2 (0.8054505373292797, 0.7543489945823536) (Pdb) c > /Customers/multi level marketing/pso.py(40)replace() -> obj = f(X[0], X[1]) (Pdb) p r1, r2 (0.5404045753007164, 0.2967937508800147) (Pdb) |
Certainly, we are able to additionally attempt to manipulate variables whereas we’re debugging.
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 |
(Pdb) l 35 world V, X, pbest, pbest_obj, gbest, gbest_obj 36 # Replace params 37 r1, r2 = np.random.rand(2) 38 V = w * V + c1*r1*(pbest – X) + c2*r2*(gbest.reshape(-1,1)-X) 39 X = X + V 40 B-> obj = f(X[0], X[1]) 41 pbest[:, (pbest_obj >= obj)] = X[:, (pbest_obj >= obj)] 42 pbest_obj = np.array([pbest_obj, obj]).min(axis=0) 43 gbest = pbest[:, pbest_obj.argmin()] 44 gbest_obj = pbest_obj.min() 45 (Pdb) p V array([[ 0.03742722, 0.20930531, 0.06273426, -0.1710678 , 0.33629384, 0.19506555, -0.10238065, -0.12707257, 0.28042122, -0.03250191, -0.14004886, 0.13224399, 0.16083673, 0.21198813, 0.17530208, -0.27665503, -0.15344393, 0.20079061, -0.10057509, 0.09128536], [-0.05034548, -0.27986224, -0.30725954, 0.11214169, 0.0934514 , 0.00335978, 0.20517519, 0.06308483, -0.22007053, 0.26176423, -0.12617228, -0.05676629, 0.18296986, -0.01669114, 0.18934933, -0.27623121, -0.32482898, 0.213894 , -0.34427909, -0.12058168]]) (Pdb) p r1, r2 (0.5404045753007164, 0.2967937508800147) (Pdb) r1 = 0.2 (Pdb) p r1, r2 (0.2, 0.2967937508800147) (Pdb) j 38 > /Customers/multi level marketing/pso.py(38)replace() -> V = w * V + c1*r1*(pbest – X) + c2*r2*(gbest.reshape(-1,1)-X) (Pdb) n > /Customers/multi level marketing/pso.py(39)replace() -> X = X + V (Pdb) p V array([[ 0.02680837, 0.16594979, 0.06350735, -0.15577623, 0.30737655, 0.19911613, -0.08242418, -0.12513798, 0.24939995, -0.02217463, -0.13474876, 0.14466204, 0.16661846, 0.21194543, 0.16952298, -0.24462505, -0.138997 , 0.19377154, -0.10699911, 0.10631063], [-0.03606147, -0.25128615, -0.26362411, 0.08163408, 0.09842085, 0.00765688, 0.19771385, 0.06597805, -0.20564599, 0.23113388, -0.0956787 , -0.07044121, 0.16637064, -0.00639259, 0.18245734, -0.25698717, -0.30336147, 0.19354112, -0.29904698, -0.08810355]]) (Pdb) |
Within the above, we use l
command to checklist the code across the present assertion (recognized by the arrow ->
). Within the itemizing, we are able to additionally see the breakpoint (marked with B
) is about at line 40. As we are able to see the present worth of V
and r1
, we are able to modify r1
from 0.54 to 0.2 and run the assertion on V
once more through the use of j
(soar) to line 38. And as we see after we execute the assertion with n
command, the worth of V
is modified.
If we use a breakpoint and located one thing surprising, chances are high that it was brought on by points in a unique degree of the decision stack. Debuggers would assist you to navigate to totally different ranges:
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 |
(Pdb) bt /usr/native/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Variations/3.9/lib/python3.9/bdb.py(580)run()
-> exec(cmd, globals, locals) <string>(1)<module>() /Customers/multi level marketing/pso.py(76)<module>() -> anim.save(“PSO.gif”, dpi=120, author=”imagemagick”) /usr/native/lib/python3.9/site-packages/matplotlib/animation.py(1091)save() -> anim._draw_next_frame(d, blit=False) /usr/native/lib/python3.9/site-packages/matplotlib/animation.py(1126)_draw_next_frame() -> self._draw_frame(framedata) /usr/native/lib/python3.9/site-packages/matplotlib/animation.py(1720)_draw_frame() -> self._drawn_artists = self._func(framedata, *self._args) /Customers/multi level marketing/pso.py(65)animate() -> replace() > /Customers/multi level marketing/pso.py(39)replace() -> X = X + V (Pdb) up > /Customers/multi level marketing/pso.py(65)animate() -> replace() (Pdb) bt /usr/native/Cellar/[email protected]/3.9.9/Frameworks/Python.framework/Variations/3.9/lib/python3.9/bdb.py(580)run()
-> exec(cmd, globals, locals) <string>(1)<module>() /Customers/multi level marketing/pso.py(76)<module>() -> anim.save(“PSO.gif”, dpi=120, author=”imagemagick”) /usr/native/lib/python3.9/site-packages/matplotlib/animation.py(1091)save() -> anim._draw_next_frame(d, blit=False) /usr/native/lib/python3.9/site-packages/matplotlib/animation.py(1126)_draw_next_frame() -> self._draw_frame(framedata) /usr/native/lib/python3.9/site-packages/matplotlib/animation.py(1720)_draw_frame() -> self._drawn_artists = self._func(framedata, *self._args) > /Customers/multi level marketing/pso.py(65)animate() -> replace() /Customers/multi level marketing/pso.py(39)replace() -> X = X + V (Pdb) l 60 61 def animate(i): 62 “Steps of PSO: algorithm replace and present in plot” 63 title=”Iteration {:02d}”.format(i) 64 # Replace params 65 -> replace() 66 # Set image 67 ax.set_title(title) 68 pbest_plot.set_offsets(pbest.T) 69 p_plot.set_offsets(X.T) 70 p_arrow.set_offsets(X.T) (Pdb) p title ‘Iteration 02’ (Pdb) |
Within the above, the primary bt
command provides the decision stack once we are on the backside body, i.e., the deepest of the decision stack. We are able to see that we’re about to execute the assertion X = X + V
. Then the up
command strikes our focus to at least one degree up on the decision stack, which is the road working replace()
perform (as we see on the line preceded with >
). Since our focus is modified, the checklist command l
will print a unique fragment of code and the p
command can study a variable in a unique scope.
The above covers many of the helpful instructions within the debugger. If we wish to terminate the debugger (which additionally terminates this system), we are able to use the q
command to give up or hit Ctrl-D in case your terminal helps.
Debugger in Visible Studio Code
If you’re not very comfy to run the debugger in command line, you’ll be able to depend on the debugger out of your IDE. Virtually all the time the IDE will present you some debugging facility. In Visible Studio Code for instance, you’ll be able to launch the debugger within the “Run” menu.
The display beneath exhibits Visible Studio Code at debugging session. The buttons on the heart high are correspond to pdb
instructions proceed
, subsequent
, step
, return
, restart
, and give up
respectively. A breakpoint might be created by clicking on the road quantity, which a crimson dot shall be appeared to determine that. The bonus of utilizing an IDE is that the variables are proven instantly at every debugging step. We are able to additionally look ahead to an categorical and present the decision stack. These are at left aspect of the display beneath.
Utilizing GDB on a working Python program
The pdb
from Python is appropriate just for packages working from scratch. If we have now a program already working however caught, we can’t use pdb to hook into it to examine what’s happening. The Python extension from GDB, nonetheless, can do that.
To reveal, let’s take into account a GUI utility. It should wait till consumer’s motion earlier than this system can finish. Therefore it’s a excellent instance to see how we are able to use gdb
to hook right into a working course of. The code beneath is a “howdy world” program utilizing PyQt5 that simply create an empty window and ready for consumer to shut it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import sys from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
class Body(QMainWindow): def __init__(self): tremendous().__init__() self.initUI() def initUI(self): self.setWindowTitle(“Easy title”) self.resize(800,600)
def important(): app = QApplication(sys.argv) body = Body() body.present() sys.exit(app.exec_())
if __name__ == ‘__main__’: important() |
Let’s save this program as simpleqt.py
and run it utilizing the next in Linux underneath X window atmosphere:
The ultimate &
will make it run in background. Now we are able to examine for its course of ID utilizing the ps
command:
… 3997 pts/1 Sl 0:00 python simpleqt.py … |
The ps
command will inform you the method ID on the first column. You probably have gdb
put in with python extension, we are able to run
and it’ll convey you into the GDB’s immediate:
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 |
GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git Copyright (C) 2021 Free Software program Basis, Inc. License GPLv3+: GNU GPL model 3 or later <http://gnu.org/licenses/gpl.html> That is free software program: you might be free to vary and redistribute it. There’s NO WARRANTY, to the extent permitted by legislation. Sort “present copying” and “present guarantee” for particulars. This GDB was configured as “x86_64-linux-gnu”. Sort “present configuration” for configuration particulars. For bug reporting directions, please see: <https://www.gnu.org/software program/gdb/bugs/>. Discover the GDB handbook and different documentation sources on-line at: <http://www.gnu.org/software program/gdb/documentation/>.
For assist, sort “assist”. Sort “apropos phrase” to seek for instructions associated to “phrase”… Studying symbols from python… Studying symbols from /usr/lib/debug/.build-id/f9/02f8a561c3abdb9c8d8c859d4243bd8c3f928f.debug… Attaching to program: /usr/native/bin/python, course of 3997 [New LWP 3998] [New LWP 3999] [New LWP 4001] [New LWP 4002] [New LWP 4003] [New LWP 4004] [Thread debugging using libthread_db enabled] Utilizing host libthread_db library “/lib/x86_64-linux-gnu/libthread_db.so.1”. 0x00007fb11b1c93ff in __GI___poll (fds=0x7fb110007220, nfds=3, timeout=-1) at ../sysdeps/unix/sysv/linux/ballot.c:29 29 ../sysdeps/unix/sysv/linux/ballot.c: No such file or listing. (gdb) py-bt Traceback (most up-to-date name first): <built-in technique exec_ of QApplication object at distant 0x7fb115f64c10> File “/mnt/knowledge/simpleqt.py”, line 16, in important sys.exit(app.exec_()) File “/mnt/knowledge/simpleqt.py”, line 19, in <module> important() (gdb) py-list 11 12 def important(): 13 app = QApplication(sys.argv) 14 body = Body() 15 body.present() >16 sys.exit(app.exec_()) 17 18 if __name__ == ‘__main__’: 19 important() (gdb) |
GDB is meant to be a debugger for compiled packages (normally from C or C++). The Python extension lets you examine the code (written in Python) being run by the Python interpreter (which is written in C). It’s much less feature-rich than the Python’s pdb
when it comes to dealing with Python code however helpful while you wish to have to hook right into a working course of.
The command supported underneath GDB are py-list
, py-bt
, py-up
, py-down
, and py-print
. They’re akin to the identical instructions in pdb
with out the py-
prefix.
GDB is beneficial in case your Python code makes use of a library that’s compiled from C (resembling numpy) and wish to examine how the it runs. Additionally it is helpful to study why your program is frozen by checking the decision stack in run time. Nonetheless, it might be not often the case that it’s good to use GDB to debug your machine studying challenge.
Additional Readings
The Python pdb
module’s doc is at
However pdb
isn’t the one debugger out there. Some third-party instruments are listed in:
For GDB with Python extension, it’s most mature for use in Linux atmosphere. Please see the next for extra particulars on its utilization:
The command interface of pdb
is influenced by that of GDB. Therefore we are able to study the strategy of debugging a program basically from the latter. A great primer on find out how to use a debugger can be
Abstract
On this tutorial, you found the options of Python’s pdb
Particularly, you discovered:
- What can
pdb
do and find out how to use it - The limitation and alternate options of
pdb
Within the subsequent publish, we’ll see that pdb
can also be a Python perform that may be known as inside a Python program.