{"id":5344,"date":"2022-01-11T06:29:21","date_gmt":"2022-01-11T06:29:21","guid":{"rendered":"https:\/\/www.aiproblog.com\/index.php\/2022\/01\/11\/python-debugging-tools\/"},"modified":"2022-01-11T06:29:21","modified_gmt":"2022-01-11T06:29:21","slug":"python-debugging-tools","status":"publish","type":"post","link":"https:\/\/www.aiproblog.com\/index.php\/2022\/01\/11\/python-debugging-tools\/","title":{"rendered":"Python debugging tools"},"content":{"rendered":"<p>Author: Adrian Tam<\/p>\n<div>\n<p>In all programming exercises, it is difficult to go far and deep without a handy debugger. The built-in debugger, <code>pdb<\/code>, in Python is a mature and capable one that can help us a lot if you know how to use it. In this tutorial we are going see what the <code>pdb<\/code> can do for you as well as some of its alternative.<\/p>\n<p>In this tutorial you will learn:<\/p>\n<ul>\n<li>What can a debugger do<\/li>\n<li>How to control a debugger<\/li>\n<li>The limitation of Python\u2019s pdb and its alternatives<\/li>\n<\/ul>\n<p>Let\u2019s get started.<\/p>\n<div id=\"attachment_13178\" style=\"width: 810px\" class=\"wp-caption aligncenter\">\n<img decoding=\"async\" aria-describedby=\"caption-attachment-13178\" class=\"size-full wp-image-13178\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/thomas-park-AF2k44m22-I-unsplash.jpg\" alt=\"\" width=\"800\" srcset=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/thomas-park-AF2k44m22-I-unsplash.jpg 1920w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/thomas-park-AF2k44m22-I-unsplash-300x200.jpg 300w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/thomas-park-AF2k44m22-I-unsplash-1024x682.jpg 1024w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/thomas-park-AF2k44m22-I-unsplash-768x512.jpg 768w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/thomas-park-AF2k44m22-I-unsplash-1536x1023.jpg 1536w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/thomas-park-AF2k44m22-I-unsplash-600x400.jpg 600w\" sizes=\"(max-width: 1920px) 100vw, 1920px\"><\/p>\n<p id=\"caption-attachment-13178\" class=\"wp-caption-text\">Python debugging tools<br \/>Photo by <a href=\"https:\/\/unsplash.com\/photos\/AF2k44m22-I\">Thomas Park<\/a>. Some rights reserved.<\/p>\n<\/div>\n<h2>Tutorial Overview<\/h2>\n<p>This tutorial is in 4 parts, they are<\/p>\n<ul>\n<li>The concept of running a debugger<\/li>\n<li>Walk-through of using a debugger<\/li>\n<li>Debugger in Visual Studio Code<\/li>\n<li>Using GDB on a running Python program<\/li>\n<\/ul>\n<h2>The concept of running a debugger<\/h2>\n<p>The purpose of a debugger is to provide you a slow motion button to control the flow of a program. It also allow you to freeze the program at certain point of time and examine the state.<\/p>\n<p>The simplest operation under a debugger is to <strong>step through<\/strong> the code. That is to run one line of code at a time and wait for your acknowledgment before proceeding into next. The reason we want to run the program in a stop-and-go fashion is to allow us to check the logic and value or verify the algorithm.<\/p>\n<p>For a larger program, we may not want to step through the code from the beginning as it may take a long time before we reached the line that we are interested in. Therefore, debuggers also provide a <strong>breakpoint<\/strong> feature that will kick in when a specific line of code is reached. From that point onward, we can step through it line by line.<\/p>\n<h2>Walk-through of using a debugger<\/h2>\n<p>Let\u2019s see how we can make use of a debugger with an example. The following is the Python code for showing <a href=\"https:\/\/machinelearningmastery.com\/a-gentle-introduction-to-particle-swarm-optimization\/\">particle swarm optimization<\/a> in an animation:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">import numpy as np\r\nimport matplotlib.pyplot as plt\r\nfrom matplotlib.animation import FuncAnimation\r\n\r\ndef f(x,y):\r\n    \"Objective function\"\r\n    return (x-3.14)**2 + (y-2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y-1.73)\r\n\r\n# Compute and plot the function in 3D within [0,5]x[0,5]\r\nx, y = np.array(np.meshgrid(np.linspace(0,5,100), np.linspace(0,5,100)))\r\nz = f(x, y)\r\n\r\n# Find the global minimum\r\nx_min = x.ravel()[z.argmin()]\r\ny_min = y.ravel()[z.argmin()]\r\n\r\n# Hyper-parameter of the algorithm\r\nc1 = c2 = 0.1\r\nw = 0.8\r\n\r\n# Create particles\r\nn_particles = 20\r\nnp.random.seed(100)\r\nX = np.random.rand(2, n_particles) * 5\r\nV = np.random.randn(2, n_particles) * 0.1\r\n\r\n# Initialize data\r\npbest = X\r\npbest_obj = f(X[0], X[1])\r\ngbest = pbest[:, pbest_obj.argmin()]\r\ngbest_obj = pbest_obj.min()\r\n\r\ndef update():\r\n    \"Function to do one iteration of particle swarm optimization\"\r\n    global V, X, pbest, pbest_obj, gbest, gbest_obj\r\n    # Update params\r\n    r1, r2 = np.random.rand(2)\r\n    V = w * V + c1*r1*(pbest - X) + c2*r2*(gbest.reshape(-1,1)-X)\r\n    X = X + V\r\n    obj = f(X[0], X[1])\r\n    pbest[:, (pbest_obj &gt;= obj)] = X[:, (pbest_obj &gt;= obj)]\r\n    pbest_obj = np.array([pbest_obj, obj]).min(axis=0)\r\n    gbest = pbest[:, pbest_obj.argmin()]\r\n    gbest_obj = pbest_obj.min()\r\n\r\n# Set up base figure: The contour map\r\nfig, ax = plt.subplots(figsize=(8,6))\r\nfig.set_tight_layout(True)\r\nimg = ax.imshow(z, extent=[0, 5, 0, 5], origin='lower', cmap='viridis', alpha=0.5)\r\nfig.colorbar(img, ax=ax)\r\nax.plot([x_min], [y_min], marker='x', markersize=5, color=\"white\")\r\ncontours = ax.contour(x, y, z, 10, colors='black', alpha=0.4)\r\nax.clabel(contours, inline=True, fontsize=8, fmt=\"%.0f\")\r\npbest_plot = ax.scatter(pbest[0], pbest[1], marker='o', color='black', alpha=0.5)\r\np_plot = ax.scatter(X[0], X[1], marker='o', color='blue', alpha=0.5)\r\np_arrow = ax.quiver(X[0], X[1], V[0], V[1], color='blue', width=0.005, angles='xy', scale_units='xy', scale=1)\r\ngbest_plot = plt.scatter([gbest[0]], [gbest[1]], marker='*', s=100, color='black', alpha=0.4)\r\nax.set_xlim([0,5])\r\nax.set_ylim([0,5])\r\n\r\ndef animate(i):\r\n    \"Steps of PSO: algorithm update and show in plot\"\r\n    title = 'Iteration {:02d}'.format(i)\r\n    # Update params\r\n    update()\r\n    # Set picture\r\n    ax.set_title(title)\r\n    pbest_plot.set_offsets(pbest.T)\r\n    p_plot.set_offsets(X.T)\r\n    p_arrow.set_offsets(X.T)\r\n    p_arrow.set_UVC(V[0], V[1])\r\n    gbest_plot.set_offsets(gbest.reshape(1,-1))\r\n    return ax, pbest_plot, p_plot, p_arrow, gbest_plot\r\n\r\nanim = FuncAnimation(fig, animate, frames=list(range(1,50)), interval=500, blit=False, repeat=True)\r\nanim.save(\"PSO.gif\", dpi=120, writer=\"imagemagick\")\r\n\r\nprint(\"PSO found best solution at f({})={}\".format(gbest, gbest_obj))\r\nprint(\"Global optimal at f({})={}\".format([x_min,y_min], f(x_min,y_min)))<\/pre>\n<p>The particle swarm optimization is done by executing the <code>update()<\/code> function a number of times. Each time it runs, we are closer to the optimal solution to the objective function. We are using matplotlib\u2019s <code>FuncAnimation()<\/code> function instead of a loop to run <code>update()<\/code>. So we can capture the position of the particles at each iteration.<\/p>\n<p>Assume this program is saved as <code>pso.py<\/code>, to run this program in command line is simply to enter:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">python pso.py<\/pre>\n<p>and the solution will be print out to the screen and the animation will be saved as <code>PSO.gif<\/code>. But if we want to run it with the Python debugger, we enter the following in command line:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">python -m pdb pso.py<\/pre>\n<p>The <code>-m pdb<\/code> part is to load the <code>pdb<\/code> module and let the module to execute the file <code>pso.py<\/code> for you. When you run this command, you will be welcomed with the <code>pdb<\/code> prompt as follows:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">&gt; \/Users\/mlm\/pso.py(1)&lt;module&gt;()\r\n-&gt; import numpy as np\r\n(Pdb)<\/pre>\n<p>At the prompt, you can type in the debugger commands. To show the list of supported commands, we can use <code>h<\/code>. And to show the detail of the specific command (such as <code>list<\/code>), we can use <code>h list<\/code>:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">&gt; \/Users\/mlm\/pso.py(1)&lt;module&gt;()\r\n-&gt; import numpy as np\r\n(Pdb) h\r\n\r\nDocumented commands (type help &lt;topic&gt;):\r\n========================================\r\nEOF    c          d        h         list      q        rv       undisplay\r\na      cl         debug    help      ll        quit     s        unt\r\nalias  clear      disable  ignore    longlist  r        source   until\r\nargs   commands   display  interact  n         restart  step     up\r\nb      condition  down     j         next      return   tbreak   w\r\nbreak  cont       enable   jump      p         retval   u        whatis\r\nbt     continue   exit     l         pp        run      unalias  where\r\n\r\nMiscellaneous help topics:\r\n==========================\r\nexec  pdb\r\n\r\n(Pdb)<\/pre>\n<p>At the beginning of a debugger session, we start with the first line of the program. Normally a Python program would start with a few lines of <code>import<\/code>. We can use <code>n<\/code> to move to the next line, or <code>s<\/code> to step into a function:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">&gt; \/Users\/mlm\/pso.py(1)&lt;module&gt;()\r\n-&gt; import numpy as np\r\n(Pdb) n\r\n&gt; \/Users\/mlm\/pso.py(2)&lt;module&gt;()\r\n-&gt; import matplotlib.pyplot as plt\r\n(Pdb) n\r\n&gt; \/Users\/mlm\/pso.py(3)&lt;module&gt;()\r\n-&gt; from matplotlib.animation import FuncAnimation\r\n(Pdb) n\r\n&gt; \/Users\/mlm\/pso.py(5)&lt;module&gt;()\r\n-&gt; def f(x,y):\r\n(Pdb) n\r\n&gt; \/Users\/mlm\/pso.py(10)&lt;module&gt;()\r\n-&gt; x, y = np.array(np.meshgrid(np.linspace(0,5,100), np.linspace(0,5,100)))\r\n(Pdb) n\r\n&gt; \/Users\/mlm\/pso.py(11)&lt;module&gt;()\r\n-&gt; z = f(x, y)\r\n(Pdb) s\r\n--Call--\r\n&gt; \/Users\/mlm\/pso.py(5)f()\r\n-&gt; def f(x,y):\r\n(Pdb) s\r\n&gt; \/Users\/mlm\/pso.py(7)f()\r\n-&gt; return (x-3.14)**2 + (y-2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y-1.73)\r\n(Pdb) s\r\n--Return--\r\n&gt; \/Users\/mlm\/pso.py(7)f()-&gt;array([[17.25... 7.46457344]])\r\n-&gt; return (x-3.14)**2 + (y-2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y-1.73)\r\n(Pdb) s\r\n&gt; \/Users\/mlm\/pso.py(14)&lt;module&gt;()\r\n-&gt; x_min = x.ravel()[z.argmin()]\r\n(Pdb)<\/pre>\n<p>In <code>pdb<\/code>, the line of code will be printed before the prompt. Usually <code>n<\/code> command is what we would prefer as it executes that line of code and moves the flow at the <strong>same level<\/strong> without drill down deeper. When we are at a line that calls a function (such as line 11 of the above program, that runs <code>z = f(x, y)<\/code>) we can use <code>s<\/code> to <strong>step into<\/strong> the function. In the above example, we first step into <code>f()<\/code> function, then another step to execute the computation, and finally, collect the return value from the function to give it back to the line that invoked the function. We see there are multiple <code>s<\/code> command needed for a function as simple as one line because finding the function from the statement, calling the function, and return each takes one step. We can also see that in the body of the function, we called <code>np.sin()<\/code> like a function but the debugger\u2019s <code>s<\/code> command does not go into it. It is because the <code>np.sin()<\/code> function is not implemented in Python but in C. The <code>pdb<\/code> does not support compiled code.<\/p>\n<p>If the program is long, it is quite boring to use the <code>n<\/code> command many times to move to somewhere we are interested. We can use <code>until<\/code> command with a line number to let the debugger run the program until that line is reached:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">&gt; \/Users\/mlm\/pso.py(1)&lt;module&gt;()\r\n-&gt; import numpy as np\r\n(Pdb) until 11\r\n&gt; \/Users\/mlm\/pso.py(11)&lt;module&gt;()\r\n-&gt; z = f(x, y)\r\n(Pdb) s\r\n--Call--\r\n&gt; \/Users\/mlm\/pso.py(5)f()\r\n-&gt; def f(x,y):\r\n(Pdb) s\r\n&gt; \/Users\/mlm\/pso.py(7)f()\r\n-&gt; return (x-3.14)**2 + (y-2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y-1.73)\r\n(Pdb) s\r\n--Return--\r\n&gt; \/Users\/mlm\/pso.py(7)f()-&gt;array([[17.25... 7.46457344]])\r\n-&gt; return (x-3.14)**2 + (y-2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y-1.73)\r\n(Pdb) s\r\n&gt; \/Users\/mlm\/pso.py(14)&lt;module&gt;()\r\n-&gt; x_min = x.ravel()[z.argmin()]\r\n(Pdb)<\/pre>\n<p>A command similar to <code>until<\/code> is <code>return<\/code>, which will execute the current function until the point that it is about to return. You can consider that as <code>until<\/code> with the line number equal to the last line of the current function. The <code>until<\/code> command is one-off, meaning it will bring you to that line only. If you want to stop at a particular line <strong>whenever<\/strong> it is being run, we can make a <strong>breakpoint<\/strong>\u00a0on it. For example, if we are interested in how each iteration of the optimization algorithm moves the solution, we can set a breakpoint right after the update is applied:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">&gt; \/Users\/mlm\/pso.py(1)&lt;module&gt;()\r\n-&gt; import numpy as np\r\n(Pdb) b 40\r\nBreakpoint 1 at \/Users\/mlm\/pso.py:40\r\n(Pdb) c\r\n&gt; \/Users\/mlm\/pso.py(40)update()\r\n-&gt; obj = f(X[0], X[1])\r\n(Pdb) bt\r\n  \/usr\/local\/Cellar\/python@3.9\/3.9.9\/Frameworks\/Python.framework\/Versions\/3.9\/lib\/python3.9\/bdb.py(580)run()\r\n-&gt; exec(cmd, globals, locals)\r\n  &lt;string&gt;(1)&lt;module&gt;()\r\n  \/Users\/mlm\/pso.py(76)&lt;module&gt;()\r\n-&gt; anim.save(\"PSO.gif\", dpi=120, writer=\"imagemagick\")\r\n  \/usr\/local\/lib\/python3.9\/site-packages\/matplotlib\/animation.py(1078)save()\r\n-&gt; anim._init_draw()  # Clear the initial frame\r\n  \/usr\/local\/lib\/python3.9\/site-packages\/matplotlib\/animation.py(1698)_init_draw()\r\n-&gt; self._draw_frame(frame_data)\r\n  \/usr\/local\/lib\/python3.9\/site-packages\/matplotlib\/animation.py(1720)_draw_frame()\r\n-&gt; self._drawn_artists = self._func(framedata, *self._args)\r\n  \/Users\/mlm\/pso.py(65)animate()\r\n-&gt; update()\r\n&gt; \/Users\/mlm\/pso.py(40)update()\r\n-&gt; obj = f(X[0], X[1])\r\n(Pdb) p r1\r\n0.8054505373292797\r\n(Pdb) p r2\r\n0.7543489945823536\r\n(Pdb) p X\r\narray([[2.77550474, 1.60073607, 2.14133019, 4.11466522, 0.2445649 ,\r\n        0.65149396, 3.24520628, 4.08804798, 0.89696478, 2.82703884,\r\n        4.42055413, 1.03681404, 0.95318658, 0.60737118, 1.17702652,\r\n        4.67551174, 3.95781321, 0.95077669, 4.08220292, 1.33330594],\r\n       [2.07985611, 4.53702225, 3.81359193, 1.83427181, 0.87867832,\r\n        1.8423856 , 0.11392109, 1.2635162 , 3.84974582, 0.27397365,\r\n        2.86219806, 3.05406841, 0.64253831, 1.85730719, 0.26090638,\r\n        4.28053621, 4.71648133, 0.44101305, 4.14882396, 2.74620598]])\r\n(Pdb) n\r\n&gt; \/Users\/mlm\/pso.py(41)update()\r\n-&gt; pbest[:, (pbest_obj &gt;= obj)] = X[:, (pbest_obj &gt;= obj)]\r\n(Pdb) n\r\n&gt; \/Users\/mlm\/pso.py(42)update()\r\n-&gt; pbest_obj = np.array([pbest_obj, obj]).min(axis=0)\r\n(Pdb) n\r\n&gt; \/Users\/mlm\/pso.py(43)update()\r\n-&gt; gbest = pbest[:, pbest_obj.argmin()]\r\n(Pdb) n\r\n&gt; \/Users\/mlm\/pso.py(44)update()\r\n-&gt; gbest_obj = pbest_obj.min()\r\n(Pdb)<\/pre>\n<p>After we set a breakpoint with the <code>b<\/code> command, we can let the debugger run our program until the breakpoint is hit. The <code>c<\/code> command means to <strong>continue<\/strong> until a trigger is met. At any point, we can use <code>bt<\/code> command to show the traceback to check how we reached here. We can also use the <code>p<\/code> command to print the variables (or an expression) to check what value they are holding.<\/p>\n<p>Indeed, we can place a breakpoint with a condition, so that it will stop only if the condition is met. The below will impose a condition that the first random number (<code>r1<\/code>) is greater than 0.5:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">(Pdb) b 40, r1 &gt; 0.5\r\nBreakpoint 1 at \/Users\/mlm\/pso.py:40\r\n(Pdb) c\r\n&gt; \/Users\/mlm\/pso.py(40)update()\r\n-&gt; obj = f(X[0], X[1])\r\n(Pdb) p r1, r2\r\n(0.8054505373292797, 0.7543489945823536)\r\n(Pdb) c\r\n&gt; \/Users\/mlm\/pso.py(40)update()\r\n-&gt; obj = f(X[0], X[1])\r\n(Pdb) p r1, r2\r\n(0.5404045753007164, 0.2967937508800147)\r\n(Pdb)<\/pre>\n<p>Indeed, we can also try to manipulate variables while we are debugging.<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">(Pdb) l\r\n 35  \t    global V, X, pbest, pbest_obj, gbest, gbest_obj\r\n 36  \t    # Update params\r\n 37  \t    r1, r2 = np.random.rand(2)\r\n 38  \t    V = w * V + c1*r1*(pbest - X) + c2*r2*(gbest.reshape(-1,1)-X)\r\n 39  \t    X = X + V\r\n 40 B-&gt;\t    obj = f(X[0], X[1])\r\n 41  \t    pbest[:, (pbest_obj &gt;= obj)] = X[:, (pbest_obj &gt;= obj)]\r\n 42  \t    pbest_obj = np.array([pbest_obj, obj]).min(axis=0)\r\n 43  \t    gbest = pbest[:, pbest_obj.argmin()]\r\n 44  \t    gbest_obj = pbest_obj.min()\r\n 45\r\n(Pdb) p V\r\narray([[ 0.03742722,  0.20930531,  0.06273426, -0.1710678 ,  0.33629384,\r\n         0.19506555, -0.10238065, -0.12707257,  0.28042122, -0.03250191,\r\n        -0.14004886,  0.13224399,  0.16083673,  0.21198813,  0.17530208,\r\n        -0.27665503, -0.15344393,  0.20079061, -0.10057509,  0.09128536],\r\n       [-0.05034548, -0.27986224, -0.30725954,  0.11214169,  0.0934514 ,\r\n         0.00335978,  0.20517519,  0.06308483, -0.22007053,  0.26176423,\r\n        -0.12617228, -0.05676629,  0.18296986, -0.01669114,  0.18934933,\r\n        -0.27623121, -0.32482898,  0.213894  , -0.34427909, -0.12058168]])\r\n(Pdb) p r1, r2\r\n(0.5404045753007164, 0.2967937508800147)\r\n(Pdb) r1 = 0.2\r\n(Pdb) p r1, r2\r\n(0.2, 0.2967937508800147)\r\n(Pdb) j 38\r\n&gt; \/Users\/mlm\/pso.py(38)update()\r\n-&gt; V = w * V + c1*r1*(pbest - X) + c2*r2*(gbest.reshape(-1,1)-X)\r\n(Pdb) n\r\n&gt; \/Users\/mlm\/pso.py(39)update()\r\n-&gt; X = X + V\r\n(Pdb) p V\r\narray([[ 0.02680837,  0.16594979,  0.06350735, -0.15577623,  0.30737655,\r\n         0.19911613, -0.08242418, -0.12513798,  0.24939995, -0.02217463,\r\n        -0.13474876,  0.14466204,  0.16661846,  0.21194543,  0.16952298,\r\n        -0.24462505, -0.138997  ,  0.19377154, -0.10699911,  0.10631063],\r\n       [-0.03606147, -0.25128615, -0.26362411,  0.08163408,  0.09842085,\r\n         0.00765688,  0.19771385,  0.06597805, -0.20564599,  0.23113388,\r\n        -0.0956787 , -0.07044121,  0.16637064, -0.00639259,  0.18245734,\r\n        -0.25698717, -0.30336147,  0.19354112, -0.29904698, -0.08810355]])\r\n(Pdb)<\/pre>\n<p>In the above, we use <code>l<\/code> command to list the code around the current statement (identified by the arrow <code>-&gt;<\/code>). In the listing, we can also see the breakpoint (marked with <code>B<\/code>) is set at line 40. As we can see the current value of <code>V<\/code> and <code>r1<\/code>, we can modify <code>r1<\/code> from 0.54 to 0.2 and run the statement on <code>V<\/code> again by using <code>j<\/code> (jump) to line 38. And as we see after we execute the statement with <code>n<\/code> command, the value of <code>V<\/code> is changed.<\/p>\n<p>If we use a breakpoint and found something unexpected, chances are that it was caused by issues in a different level of the call stack. Debuggers would allow you to navigate to different levels:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">(Pdb) bt\r\n  \/usr\/local\/Cellar\/python@3.9\/3.9.9\/Frameworks\/Python.framework\/Versions\/3.9\/lib\/python3.9\/bdb.py(580)run()\r\n-&gt; exec(cmd, globals, locals)\r\n  &lt;string&gt;(1)&lt;module&gt;()\r\n  \/Users\/mlm\/pso.py(76)&lt;module&gt;()\r\n-&gt; anim.save(\"PSO.gif\", dpi=120, writer=\"imagemagick\")\r\n  \/usr\/local\/lib\/python3.9\/site-packages\/matplotlib\/animation.py(1091)save()\r\n-&gt; anim._draw_next_frame(d, blit=False)\r\n  \/usr\/local\/lib\/python3.9\/site-packages\/matplotlib\/animation.py(1126)_draw_next_frame()\r\n-&gt; self._draw_frame(framedata)\r\n  \/usr\/local\/lib\/python3.9\/site-packages\/matplotlib\/animation.py(1720)_draw_frame()\r\n-&gt; self._drawn_artists = self._func(framedata, *self._args)\r\n  \/Users\/mlm\/pso.py(65)animate()\r\n-&gt; update()\r\n&gt; \/Users\/mlm\/pso.py(39)update()\r\n-&gt; X = X + V\r\n(Pdb) up\r\n&gt; \/Users\/mlm\/pso.py(65)animate()\r\n-&gt; update()\r\n(Pdb) bt\r\n  \/usr\/local\/Cellar\/python@3.9\/3.9.9\/Frameworks\/Python.framework\/Versions\/3.9\/lib\/python3.9\/bdb.py(580)run()\r\n-&gt; exec(cmd, globals, locals)\r\n  &lt;string&gt;(1)&lt;module&gt;()\r\n  \/Users\/mlm\/pso.py(76)&lt;module&gt;()\r\n-&gt; anim.save(\"PSO.gif\", dpi=120, writer=\"imagemagick\")\r\n  \/usr\/local\/lib\/python3.9\/site-packages\/matplotlib\/animation.py(1091)save()\r\n-&gt; anim._draw_next_frame(d, blit=False)\r\n  \/usr\/local\/lib\/python3.9\/site-packages\/matplotlib\/animation.py(1126)_draw_next_frame()\r\n-&gt; self._draw_frame(framedata)\r\n  \/usr\/local\/lib\/python3.9\/site-packages\/matplotlib\/animation.py(1720)_draw_frame()\r\n-&gt; self._drawn_artists = self._func(framedata, *self._args)\r\n&gt; \/Users\/mlm\/pso.py(65)animate()\r\n-&gt; update()\r\n  \/Users\/mlm\/pso.py(39)update()\r\n-&gt; X = X + V\r\n(Pdb) l\r\n 60\r\n 61     def animate(i):\r\n 62         \"Steps of PSO: algorithm update and show in plot\"\r\n 63         title = 'Iteration {:02d}'.format(i)\r\n 64         # Update params\r\n 65  -&gt;     update()\r\n 66         # Set picture\r\n 67         ax.set_title(title)\r\n 68         pbest_plot.set_offsets(pbest.T)\r\n 69         p_plot.set_offsets(X.T)\r\n 70         p_arrow.set_offsets(X.T)\r\n(Pdb) p title\r\n'Iteration 02'\r\n(Pdb)<\/pre>\n<p>In the above, the first <code>bt<\/code> command gives the call stack when we are at the bottom frame, i.e., the deepest of the call stack. We can see that we are about to execute the statement <code>X = X + V<\/code>. Then the <code>up<\/code> command moves our focus to one level up on the call stack, which is the line running <code>update()<\/code> function (as we see at the line preceded with <code>&gt;<\/code>). Since our focus is changed, the list command <code>l<\/code> will print a different fragment of code and the <code>p<\/code> command can examine a variable in a different scope.<\/p>\n<p>The above covers most of the useful commands in the debugger. If we want to terminate the debugger (which also terminates the program), we can use the <code>q<\/code> command to quit or hit Ctrl-D if your terminal supports.<\/p>\n<h2>Debugger in Visual Studio Code<\/h2>\n<p>If you are not very comfortable to run the debugger in command line, you can rely on the debugger from your IDE. Almost always the IDE will provide you some debugging facility. In Visual Studio Code for example, you can launch the debugger in the \u201cRun\u201d menu.<\/p>\n<p>The screen below shows Visual Studio Code at debugging session. The buttons at the center top are correspond to <code>pdb<\/code> commands <code>continue<\/code>, <code>next<\/code>, <code>step<\/code>, <code>return<\/code>, <code>restart<\/code>, and <code>quit<\/code> respectively. A breakpoint can be created by clicking on the line number, which a red dot will be appeared to identify that. The bonus of using an IDE is that the variables are shown immediately at each debugging step. We can also watch for an express and show the call stack. These are at left side of the screen below.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-13177\" src=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/VSCode-Debug.png\" alt=\"\" width=\"850\" srcset=\"https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/VSCode-Debug.png 2548w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/VSCode-Debug-300x236.png 300w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/VSCode-Debug-1024x806.png 1024w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/VSCode-Debug-768x605.png 768w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/VSCode-Debug-1536x1209.png 1536w, https:\/\/machinelearningmastery.com\/wp-content\/uploads\/2022\/01\/VSCode-Debug-2048x1612.png 2048w\" sizes=\"(max-width: 2548px) 100vw, 2548px\"><\/p>\n<h2>Using GDB on a running Python program<\/h2>\n<p>The <code>pdb<\/code> from Python is suitable only for programs running from scratch. If we have a program already running but stuck, we cannot use pdb to <strong>hook into<\/strong> it to check what\u2019s going on. The Python extension from GDB, however, can do this.<\/p>\n<p>To demonstrate, let\u2019s consider a GUI application. It will wait until user\u2019s action before the program can end. Hence it is a perfect example to see how we can use <code>gdb<\/code> to hook into a running process. The code below is a \u201chello world\u201d program using PyQt5 that just create an empty window and waiting for user to close it:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">import sys\r\nfrom PyQt5.QtWidgets import QApplication, QWidget, QMainWindow\r\n\r\nclass Frame(QMainWindow):\r\n        def __init__(self):\r\n                super().__init__()\r\n                self.initUI()\r\n        def initUI(self):\r\n                self.setWindowTitle(\"Simple title\")\r\n                self.resize(800,600)\r\n\r\ndef main():\r\n        app = QApplication(sys.argv)\r\n        frame = Frame()\r\n        frame.show()\r\n        sys.exit(app.exec_())\r\n\r\nif __name__ == '__main__':\r\n        main()<\/pre>\n<p>Let\u2019s save this program as <code>simpleqt.py<\/code> and run it using the following in Linux under X window environment:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">python simpleqt.py &amp;<\/pre>\n<p>The final <code>&amp;<\/code> will make it run in background. Now we can check for its process ID using the <code>ps<\/code> command:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">ps a | grep python<\/pre>\n<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">...\r\n   3997 pts\/1    Sl     0:00 python simpleqt.py\r\n...<\/pre>\n<p>The <code>ps<\/code> command will tell you the process ID at the first column. If you have <code>gdb<\/code> installed with python extension, we can run<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">gdb python 3997<\/pre>\n<p>and it will bring you into the GDB\u2019s prompt:<\/p>\n<pre class=\"urvanov-syntax-highlighter-plain-tag\">GNU gdb (Debian 10.1-1.7) 10.1.90.20210103-git\r\nCopyright (C) 2021 Free Software Foundation, Inc.\r\nLicense GPLv3+: GNU GPL version 3 or later &lt;http:\/\/gnu.org\/licenses\/gpl.html&gt;\r\nThis is free software: you are free to change and redistribute it.\r\nThere is NO WARRANTY, to the extent permitted by law.\r\nType \"show copying\" and \"show warranty\" for details.\r\nThis GDB was configured as \"x86_64-linux-gnu\".\r\nType \"show configuration\" for configuration details.\r\nFor bug reporting instructions, please see:\r\n&lt;https:\/\/www.gnu.org\/software\/gdb\/bugs\/&gt;.\r\nFind the GDB manual and other documentation resources online at:\r\n    &lt;http:\/\/www.gnu.org\/software\/gdb\/documentation\/&gt;.\r\n\r\nFor help, type \"help\".\r\nType \"apropos word\" to search for commands related to \"word\"...\r\nReading symbols from python...\r\nReading symbols from \/usr\/lib\/debug\/.build-id\/f9\/02f8a561c3abdb9c8d8c859d4243bd8c3f928f.debug...\r\nAttaching to program: \/usr\/local\/bin\/python, process 3997\r\n[New LWP 3998]\r\n[New LWP 3999]\r\n[New LWP 4001]\r\n[New LWP 4002]\r\n[New LWP 4003]\r\n[New LWP 4004]\r\n[Thread debugging using libthread_db enabled]\r\nUsing host libthread_db library \"\/lib\/x86_64-linux-gnu\/libthread_db.so.1\".\r\n0x00007fb11b1c93ff in __GI___poll (fds=0x7fb110007220, nfds=3, timeout=-1) at ..\/sysdeps\/unix\/sysv\/linux\/poll.c:29\r\n29      ..\/sysdeps\/unix\/sysv\/linux\/poll.c: No such file or directory.\r\n(gdb) py-bt\r\nTraceback (most recent call first):\r\n  &lt;built-in method exec_ of QApplication object at remote 0x7fb115f64c10&gt;\r\n  File \"\/mnt\/data\/simpleqt.py\", line 16, in main\r\n    sys.exit(app.exec_())\r\n  File \"\/mnt\/data\/simpleqt.py\", line 19, in &lt;module&gt;\r\n    main()\r\n(gdb) py-list\r\n  11    \r\n  12    def main():\r\n  13            app = QApplication(sys.argv)\r\n  14            frame = Frame()\r\n  15            frame.show()\r\n &gt;16            sys.exit(app.exec_())\r\n  17    \r\n  18    if __name__ == '__main__':\r\n  19            main()\r\n(gdb)<\/pre>\n<p>GDB is supposed to be a debugger for compiled programs (usually from C or C++). The Python extension allows you to check the code (written in Python) being run by the Python interpreter (which is written in C). It is less feature-rich than the Python\u2019s <code>pdb<\/code> in terms of handling Python code but useful when you want to need to hook into a running process.<\/p>\n<p>The command supported under GDB are <code>py-list<\/code>, <code>py-bt<\/code>, <code>py-up<\/code>, <code>py-down<\/code>, and <code>py-print<\/code>. They are comparable to the same commands in <code>pdb<\/code> without the <code>py-<\/code> prefix.<\/p>\n<p>GDB is useful if your Python code uses a library that is compiled from C (such as numpy) and want to investigate how the it runs. It is also useful to learn why your program is frozen by checking the call stack in run time. However, it may be rarely the case that you need to use GDB to debug your machine learning project.<\/p>\n<h2>Further Readings<\/h2>\n<p>The Python <code>pdb<\/code> module\u2019s document is at<\/p>\n<ul>\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/pdb.html\">https:\/\/docs.python.org\/3\/library\/pdb.html<\/a><\/li>\n<\/ul>\n<p>But <code>pdb<\/code> is not the only debugger available. Some third-party tools are listed in:<\/p>\n<ul>\n<li><a href=\"https:\/\/wiki.python.org\/moin\/PythonDebuggingTools\">Python Debugging Tools wiki page<\/a><\/li>\n<\/ul>\n<p>For GDB with Python extension, it is most mature to be used in Linux environment. Please see the following for more details on its usage:<\/p>\n<ul>\n<li><a href=\"https:\/\/fedoraproject.org\/wiki\/Features\/EasierPythonDebugging\">Easier Python Debugging<\/a><\/li>\n<li><a href=\"https:\/\/wiki.python.org\/moin\/DebuggingWithGdb\">Debugging with GDB<\/a><\/li>\n<\/ul>\n<p>The command interface of <code>pdb<\/code> is influenced by that of GDB. Hence we can learn the technique of debugging a program in general from the latter. A good primer on how to use a debugger would be<\/p>\n<ul>\n<li>\n<a href=\"https:\/\/www.amazon.com\/dp\/B00HQ1L78K\">The Art of Debugging with GDB, DDD, and Eclipse<\/a>, by Norman Matloff (2008)<\/li>\n<\/ul>\n<h2>Summary<\/h2>\n<p>In this tutorial, you discovered the features of Python\u2019s <code>pdb<\/code><\/p>\n<p>Specifically, you learned:<\/p>\n<ul>\n<li>What can <code>pdb<\/code> do and how to use it<\/li>\n<li>The limitation and alternatives of <code>pdb<\/code>\n<\/li>\n<\/ul>\n<p>In the next post, we will see that <code>pdb<\/code> is also a Python function that can be called inside a Python program.<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/python-debugging-tools\/\">Python debugging tools<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/machinelearningmastery.com\/\">Machine Learning Mastery<\/a>.<\/p>\n<\/div>\n<p><a href=\"https:\/\/machinelearningmastery.com\/python-debugging-tools\/\">Go to Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Author: Adrian Tam In all programming exercises, it is difficult to go far and deep without a handy debugger. The built-in debugger, pdb, in Python [&hellip;] <span class=\"read-more-link\"><a class=\"read-more\" href=\"https:\/\/www.aiproblog.com\/index.php\/2022\/01\/11\/python-debugging-tools\/\">Read More<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":5345,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/5344"}],"collection":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=5344"}],"version-history":[{"count":0,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/posts\/5344\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media\/5345"}],"wp:attachment":[{"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=5344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=5344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aiproblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=5344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}