|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PDSH-cover-small.png\">\n", |
| 8 | + "*This notebook contains an excerpt from the [Python Data Science Handbook](http://shop.oreilly.com/product/0636920034919.do) by Jake VanderPlas; the content is available [on GitHub](https://github.com/jakevdp/PythonDataScienceHandbook).*\n", |
| 9 | + "\n", |
| 10 | + "*The text is released under the [CC-BY-NC-ND license](https://creativecommons.org/licenses/by-nc-nd/3.0/us/legalcode), and code is released under the [MIT license](https://opensource.org/licenses/MIT). If you find this content useful, please support the work by [buying the book](http://shop.oreilly.com/product/0636920034919.do)!*" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "# Errors and Debugging\n", |
| 18 | + "\n", |
| 19 | + "Code development and data analysis always require a bit of trial and error, and IPython contains tools to streamline this process.\n", |
| 20 | + "This section will briefly cover some options for controlling Python's exception reporting, followed by exploring tools for debugging errors in code." |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "markdown", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "## Controlling Exceptions: ``%xmode``\n", |
| 28 | + "\n", |
| 29 | + "Most of the time when a Python script fails, it will raise an Exception.\n", |
| 30 | + "When the interpreter hits one of these exceptions, information about the cause of the error can be found in the *traceback*, which can be accessed from within Python.\n", |
| 31 | + "With the ``%xmode`` magic function, IPython allows you to control the amount of information printed when the exception is raised.\n", |
| 32 | + "Consider the following code:" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": 1, |
| 38 | + "metadata": { |
| 39 | + "collapsed": false |
| 40 | + }, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "def func1(a, b):\n", |
| 44 | + " return a / b\n", |
| 45 | + "\n", |
| 46 | + "def func2(x):\n", |
| 47 | + " a = x\n", |
| 48 | + " b = x - 1\n", |
| 49 | + " return func1(a, b)" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 2, |
| 55 | + "metadata": { |
| 56 | + "collapsed": false |
| 57 | + }, |
| 58 | + "outputs": [ |
| 59 | + { |
| 60 | + "ename": "ZeroDivisionError", |
| 61 | + "evalue": "division by zero", |
| 62 | + "output_type": "error", |
| 63 | + "traceback": [ |
| 64 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", |
| 65 | + "\u001b[0;32m<ipython-input-2-b2e110f6fc8f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfunc2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
| 66 | + "\u001b[0;32m<ipython-input-1-d849e34d61fb>\u001b[0m in \u001b[0;36mfunc2\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
| 67 | + "\u001b[0;32m<ipython-input-1-d849e34d61fb>\u001b[0m in \u001b[0;36mfunc1\u001b[0;34m(a, b)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mfunc2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 68 | + "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" |
| 69 | + ] |
| 70 | + } |
| 71 | + ], |
| 72 | + "source": [ |
| 73 | + "func2(1)" |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "markdown", |
| 78 | + "metadata": {}, |
| 79 | + "source": [ |
| 80 | + "Calling ``func2`` results in an error, and reading the printed trace lets us see exactly what happened.\n", |
| 81 | + "By default, this trace includes several lines showing the context of each step that led to the error.\n", |
| 82 | + "Using the ``%xmode`` magic function (short for *Exception mode*), we can change what information is printed.\n", |
| 83 | + "\n", |
| 84 | + "``%xmode`` takes a single argument, the mode, and there are three possibilities: ``Plain``, ``Context``, and ``Verbose``.\n", |
| 85 | + "The default is ``Context``, and gives output like that just shown before.\n", |
| 86 | + "``Plain`` is more compact and gives less information:" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": 3, |
| 92 | + "metadata": { |
| 93 | + "collapsed": false |
| 94 | + }, |
| 95 | + "outputs": [ |
| 96 | + { |
| 97 | + "name": "stdout", |
| 98 | + "output_type": "stream", |
| 99 | + "text": [ |
| 100 | + "Exception reporting mode: Plain\n" |
| 101 | + ] |
| 102 | + } |
| 103 | + ], |
| 104 | + "source": [ |
| 105 | + "%xmode Plain" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": 4, |
| 111 | + "metadata": { |
| 112 | + "collapsed": false |
| 113 | + }, |
| 114 | + "outputs": [ |
| 115 | + { |
| 116 | + "ename": "ZeroDivisionError", |
| 117 | + "evalue": "division by zero", |
| 118 | + "output_type": "error", |
| 119 | + "traceback": [ |
| 120 | + "Traceback \u001b[0;36m(most recent call last)\u001b[0m:\n", |
| 121 | + " File \u001b[1;32m\"<ipython-input-4-b2e110f6fc8f>\"\u001b[0m, line \u001b[1;32m1\u001b[0m, in \u001b[1;35m<module>\u001b[0m\n func2(1)\n", |
| 122 | + " File \u001b[1;32m\"<ipython-input-1-d849e34d61fb>\"\u001b[0m, line \u001b[1;32m7\u001b[0m, in \u001b[1;35mfunc2\u001b[0m\n return func1(a, b)\n", |
| 123 | + "\u001b[0;36m File \u001b[0;32m\"<ipython-input-1-d849e34d61fb>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0;36m, in \u001b[0;35mfunc1\u001b[0;36m\u001b[0m\n\u001b[0;31m return a / b\u001b[0m\n", |
| 124 | + "\u001b[0;31mZeroDivisionError\u001b[0m\u001b[0;31m:\u001b[0m division by zero\n" |
| 125 | + ] |
| 126 | + } |
| 127 | + ], |
| 128 | + "source": [ |
| 129 | + "func2(1)" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "markdown", |
| 134 | + "metadata": {}, |
| 135 | + "source": [ |
| 136 | + "The ``Verbose`` mode adds some extra information, including the arguments to any functions that are called:" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": 5, |
| 142 | + "metadata": { |
| 143 | + "collapsed": false |
| 144 | + }, |
| 145 | + "outputs": [ |
| 146 | + { |
| 147 | + "name": "stdout", |
| 148 | + "output_type": "stream", |
| 149 | + "text": [ |
| 150 | + "Exception reporting mode: Verbose\n" |
| 151 | + ] |
| 152 | + } |
| 153 | + ], |
| 154 | + "source": [ |
| 155 | + "%xmode Verbose" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "code", |
| 160 | + "execution_count": 6, |
| 161 | + "metadata": { |
| 162 | + "collapsed": false |
| 163 | + }, |
| 164 | + "outputs": [ |
| 165 | + { |
| 166 | + "ename": "ZeroDivisionError", |
| 167 | + "evalue": "division by zero", |
| 168 | + "output_type": "error", |
| 169 | + "traceback": [ |
| 170 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", |
| 171 | + "\u001b[0;32m<ipython-input-6-b2e110f6fc8f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mfunc2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m \u001b[0;36mglobal\u001b[0m \u001b[0;36mfunc2\u001b[0m \u001b[0;34m= <function func2 at 0x103729320>\u001b[0m\n", |
| 172 | + "\u001b[0;32m<ipython-input-1-d849e34d61fb>\u001b[0m in \u001b[0;36mfunc2\u001b[0;34m(x=1)\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m \u001b[0;36mglobal\u001b[0m \u001b[0;36mfunc1\u001b[0m \u001b[0;34m= <function func1 at 0x1037294d0>\u001b[0m\u001b[0;34m\n \u001b[0m\u001b[0;36ma\u001b[0m \u001b[0;34m= 1\u001b[0m\u001b[0;34m\n \u001b[0m\u001b[0;36mb\u001b[0m \u001b[0;34m= 0\u001b[0m\n", |
| 173 | + "\u001b[0;32m<ipython-input-1-d849e34d61fb>\u001b[0m in \u001b[0;36mfunc1\u001b[0;34m(a=1, b=0)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m \u001b[0;36ma\u001b[0m \u001b[0;34m= 1\u001b[0m\u001b[0;34m\n \u001b[0m\u001b[0;36mb\u001b[0m \u001b[0;34m= 0\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mfunc2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 174 | + "\u001b[0;31mZeroDivisionError\u001b[0m: division by zero" |
| 175 | + ] |
| 176 | + } |
| 177 | + ], |
| 178 | + "source": [ |
| 179 | + "func2(1)" |
| 180 | + ] |
| 181 | + }, |
| 182 | + { |
| 183 | + "cell_type": "markdown", |
| 184 | + "metadata": {}, |
| 185 | + "source": [ |
| 186 | + "This extra information can help narrow-in on why the exception is being raised.\n", |
| 187 | + "So why not use the ``Verbose`` mode all the time?\n", |
| 188 | + "As code gets complicated, this kind of traceback can get extremely long.\n", |
| 189 | + "Depending on the context, sometimes the brevity of ``Default`` mode is easier to work with." |
| 190 | + ] |
| 191 | + }, |
| 192 | + { |
| 193 | + "cell_type": "markdown", |
| 194 | + "metadata": {}, |
| 195 | + "source": [ |
| 196 | + "## Debugging: When Reading Tracebacks Is Not Enough\n", |
| 197 | + "\n", |
| 198 | + "The standard Python tool for interactive debugging is ``pdb``, the Python debugger.\n", |
| 199 | + "This debugger lets the user step through the code line by line in order to see what might be causing a more difficult error.\n", |
| 200 | + "The IPython-enhanced version of this is ``ipdb``, the IPython debugger.\n", |
| 201 | + "\n", |
| 202 | + "There are many ways to launch and use both these debuggers; we won't cover them fully here.\n", |
| 203 | + "Refer to the online documentation of these two utilities to learn more.\n", |
| 204 | + "\n", |
| 205 | + "In IPython, perhaps the most convenient interface to debugging is the ``%debug`` magic command.\n", |
| 206 | + "If you call it after hitting an exception, it will automatically open an interactive debugging prompt at the point of the exception.\n", |
| 207 | + "The ``ipdb`` prompt lets you explore the current state of the stack, explore the available variables, and even run Python commands!\n", |
| 208 | + "\n", |
| 209 | + "Let's look at the most recent exception, then do some basic tasks–print the values of ``a`` and ``b``, and type ``quit`` to quit the debugging session:" |
| 210 | + ] |
| 211 | + }, |
| 212 | + { |
| 213 | + "cell_type": "code", |
| 214 | + "execution_count": 7, |
| 215 | + "metadata": { |
| 216 | + "collapsed": false |
| 217 | + }, |
| 218 | + "outputs": [ |
| 219 | + { |
| 220 | + "name": "stdout", |
| 221 | + "output_type": "stream", |
| 222 | + "text": [ |
| 223 | + "> \u001b[0;32m<ipython-input-1-d849e34d61fb>\u001b[0m(2)\u001b[0;36mfunc1\u001b[0;34m()\u001b[0m\n", |
| 224 | + "\u001b[0;32m 1 \u001b[0;31m\u001b[0;32mdef\u001b[0m \u001b[0mfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 225 | + "\u001b[0m\u001b[0;32m----> 2 \u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 226 | + "\u001b[0m\u001b[0;32m 3 \u001b[0;31m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 227 | + "\u001b[0m\n", |
| 228 | + "ipdb> print(a)\n", |
| 229 | + "1\n", |
| 230 | + "ipdb> print(b)\n", |
| 231 | + "0\n", |
| 232 | + "ipdb> quit\n" |
| 233 | + ] |
| 234 | + } |
| 235 | + ], |
| 236 | + "source": [ |
| 237 | + "%debug" |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "markdown", |
| 242 | + "metadata": {}, |
| 243 | + "source": [ |
| 244 | + "The interactive debugger allows much more than this, though–we can even step up and down through the stack and explore the values of variables there:" |
| 245 | + ] |
| 246 | + }, |
| 247 | + { |
| 248 | + "cell_type": "code", |
| 249 | + "execution_count": 8, |
| 250 | + "metadata": { |
| 251 | + "collapsed": false |
| 252 | + }, |
| 253 | + "outputs": [ |
| 254 | + { |
| 255 | + "name": "stdout", |
| 256 | + "output_type": "stream", |
| 257 | + "text": [ |
| 258 | + "> \u001b[0;32m<ipython-input-1-d849e34d61fb>\u001b[0m(2)\u001b[0;36mfunc1\u001b[0;34m()\u001b[0m\n", |
| 259 | + "\u001b[0;32m 1 \u001b[0;31m\u001b[0;32mdef\u001b[0m \u001b[0mfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 260 | + "\u001b[0m\u001b[0;32m----> 2 \u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 261 | + "\u001b[0m\u001b[0;32m 3 \u001b[0;31m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 262 | + "\u001b[0m\n", |
| 263 | + "ipdb> up\n", |
| 264 | + "> \u001b[0;32m<ipython-input-1-d849e34d61fb>\u001b[0m(7)\u001b[0;36mfunc2\u001b[0;34m()\u001b[0m\n", |
| 265 | + "\u001b[0;32m 5 \u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 266 | + "\u001b[0m\u001b[0;32m 6 \u001b[0;31m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 267 | + "\u001b[0m\u001b[0;32m----> 7 \u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 268 | + "\u001b[0m\n", |
| 269 | + "ipdb> print(x)\n", |
| 270 | + "1\n", |
| 271 | + "ipdb> up\n", |
| 272 | + "> \u001b[0;32m<ipython-input-6-b2e110f6fc8f>\u001b[0m(1)\u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n", |
| 273 | + "\u001b[0;32m----> 1 \u001b[0;31m\u001b[0mfunc2\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 274 | + "\u001b[0m\n", |
| 275 | + "ipdb> down\n", |
| 276 | + "> \u001b[0;32m<ipython-input-1-d849e34d61fb>\u001b[0m(7)\u001b[0;36mfunc2\u001b[0;34m()\u001b[0m\n", |
| 277 | + "\u001b[0;32m 5 \u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 278 | + "\u001b[0m\u001b[0;32m 6 \u001b[0;31m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mx\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 279 | + "\u001b[0m\u001b[0;32m----> 7 \u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 280 | + "\u001b[0m\n", |
| 281 | + "ipdb> quit\n" |
| 282 | + ] |
| 283 | + } |
| 284 | + ], |
| 285 | + "source": [ |
| 286 | + "%debug" |
| 287 | + ] |
| 288 | + }, |
| 289 | + { |
| 290 | + "cell_type": "markdown", |
| 291 | + "metadata": {}, |
| 292 | + "source": [ |
| 293 | + "This allows you to quickly find out not only what caused the error, but what function calls led up to the error.\n", |
| 294 | + "\n", |
| 295 | + "If you'd like the debugger to launch automatically whenever an exception is raised, you can use the ``%pdb`` magic function to turn on this automatic behavior:" |
| 296 | + ] |
| 297 | + }, |
| 298 | + { |
| 299 | + "cell_type": "code", |
| 300 | + "execution_count": 9, |
| 301 | + "metadata": { |
| 302 | + "collapsed": false |
| 303 | + }, |
| 304 | + "outputs": [ |
| 305 | + { |
| 306 | + "name": "stdout", |
| 307 | + "output_type": "stream", |
| 308 | + "text": [ |
| 309 | + "Exception reporting mode: Plain\n", |
| 310 | + "Automatic pdb calling has been turned ON\n" |
| 311 | + ] |
| 312 | + }, |
| 313 | + { |
| 314 | + "ename": "ZeroDivisionError", |
| 315 | + "evalue": "division by zero", |
| 316 | + "output_type": "error", |
| 317 | + "traceback": [ |
| 318 | + "Traceback \u001b[0;36m(most recent call last)\u001b[0m:\n", |
| 319 | + " File \u001b[1;32m\"<ipython-input-9-569a67d2d312>\"\u001b[0m, line \u001b[1;32m3\u001b[0m, in \u001b[1;35m<module>\u001b[0m\n func2(1)\n", |
| 320 | + " File \u001b[1;32m\"<ipython-input-1-d849e34d61fb>\"\u001b[0m, line \u001b[1;32m7\u001b[0m, in \u001b[1;35mfunc2\u001b[0m\n return func1(a, b)\n", |
| 321 | + "\u001b[0;36m File \u001b[0;32m\"<ipython-input-1-d849e34d61fb>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0;36m, in \u001b[0;35mfunc1\u001b[0;36m\u001b[0m\n\u001b[0;31m return a / b\u001b[0m\n", |
| 322 | + "\u001b[0;31mZeroDivisionError\u001b[0m\u001b[0;31m:\u001b[0m division by zero\n" |
| 323 | + ] |
| 324 | + }, |
| 325 | + { |
| 326 | + "name": "stdout", |
| 327 | + "output_type": "stream", |
| 328 | + "text": [ |
| 329 | + "> \u001b[0;32m<ipython-input-1-d849e34d61fb>\u001b[0m(2)\u001b[0;36mfunc1\u001b[0;34m()\u001b[0m\n", |
| 330 | + "\u001b[0;32m 1 \u001b[0;31m\u001b[0;32mdef\u001b[0m \u001b[0mfunc1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 331 | + "\u001b[0m\u001b[0;32m----> 2 \u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 332 | + "\u001b[0m\u001b[0;32m 3 \u001b[0;31m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 333 | + "\u001b[0m\n", |
| 334 | + "ipdb> print(b)\n", |
| 335 | + "0\n", |
| 336 | + "ipdb> quit\n" |
| 337 | + ] |
| 338 | + } |
| 339 | + ], |
| 340 | + "source": [ |
| 341 | + "%xmode Plain\n", |
| 342 | + "%pdb on\n", |
| 343 | + "func2(1)" |
| 344 | + ] |
| 345 | + }, |
| 346 | + { |
| 347 | + "cell_type": "markdown", |
| 348 | + "metadata": {}, |
| 349 | + "source": [ |
| 350 | + "Finally, if you have a script that you'd like to run from the beginning in interactive mode, you can run it with the command ``%run -d``, and use the ``next`` command to step through the lines of code interactively." |
| 351 | + ] |
| 352 | + }, |
| 353 | + { |
| 354 | + "cell_type": "markdown", |
| 355 | + "metadata": {}, |
| 356 | + "source": [ |
| 357 | + "### Partial list of debugging commands\n", |
| 358 | + "\n", |
| 359 | + "There are many more available commands for interactive debugging than we've listed here; the following table contains a description of some of the more common and useful ones:\n", |
| 360 | + "\n", |
| 361 | + "| Command | Description |\n", |
| 362 | + "|-----------------|-------------------------------------------------------------|\n", |
| 363 | + "| ``list`` | Show the current ___location in the file |\n", |
| 364 | + "| ``h(elp)`` | Show a list of commands, or find help on a specific command |\n", |
| 365 | + "| ``q(uit)`` | Quit the debugger and the program |\n", |
| 366 | + "| ``c(ontinute)`` | Quit the debugger, continue in the program |\n", |
| 367 | + "| ``n(ext)`` | Go to the next step of the program |\n", |
| 368 | + "| ``<enter>`` | Repeat the previous command |\n", |
| 369 | + "| ``p(rint)`` | Print variables |\n", |
| 370 | + "| ``s(tep)`` | Step into a subroutine |\n", |
| 371 | + "| ``r(eturn)`` | Return out of a subroutine |\n", |
| 372 | + "\n", |
| 373 | + "For more information, use the ``help`` command in the debugger, or take a look at ``ipdb``'s [online documentation](https://github.com/gotcha/ipdb)." |
| 374 | + ] |
| 375 | + } |
| 376 | + ], |
| 377 | + "metadata": { |
| 378 | + "anaconda-cloud": {}, |
| 379 | + "kernelspec": { |
| 380 | + "display_name": "Python [default]", |
| 381 | + "language": "python", |
| 382 | + "name": "python3" |
| 383 | + }, |
| 384 | + "language_info": { |
| 385 | + "codemirror_mode": { |
| 386 | + "name": "ipython", |
| 387 | + "version": 3 |
| 388 | + }, |
| 389 | + "file_extension": ".py", |
| 390 | + "mimetype": "text/x-python", |
| 391 | + "name": "python", |
| 392 | + "nbconvert_exporter": "python", |
| 393 | + "pygments_lexer": "ipython3", |
| 394 | + "version": "3.5.1" |
| 395 | + } |
| 396 | + }, |
| 397 | + "nbformat": 4, |
| 398 | + "nbformat_minor": 0 |
| 399 | +} |
0 commit comments