|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: The Visual Debugger |
| 4 | +i18n: en |
| 5 | +--- |
| 6 | + |
| 7 | +# The Visual Debugger |
| 8 | + |
| 9 | +Have you ever wanted to look over the shoulder of Python as it was running |
| 10 | +your code? It's a useful ability because you get to compare how you *think* |
| 11 | +your code is run with how Python *actually* runs it. This is extraordinarily |
| 12 | +helpful if you're trying to find bugs. |
| 13 | + |
| 14 | +This is what Mu's simple visual debugger allows you to do with Python 3 code. |
| 15 | + |
| 16 | +The functionality provided by the debugger happens in three places. |
| 17 | + |
| 18 | +First of all, while in Python 3 mode or when the debugger is running, it's |
| 19 | +possible to indicate where Python should pause execution of your script |
| 20 | +(giving you an opportunity to look around at that point). This is done with a |
| 21 | +breakpoint, indicated by a circle in the margin next to the line-numbers and |
| 22 | +toggled on and off by clicking the line number of the desired line. In the |
| 23 | +picture below, there's a breakpoint set on line 4, and the debugger will run |
| 24 | +your code up until this line, at which point, it will pause: |
| 25 | + |
| 26 | +<div class="row"> |
| 27 | + <img src="/img/en/tutorials/breakpoint.png" alt="A breakpoint" class="img-responsive center-block img-rounded movie"/> |
| 28 | + <br/> |
| 29 | +</div> |
| 30 | + |
| 31 | +Secondly, when you click the "Debug" button, two things will happen: your |
| 32 | +code will be run under the control of the debugger (as a result, it takes |
| 33 | +a little longer than normal for your code to start running), and the buttons |
| 34 | +associated with the Python 3 mode will be replaced with the following new |
| 35 | +buttons that allow you to navigate your code: |
| 36 | + |
| 37 | +<div class="row"> |
| 38 | + <img src="/img/en/tutorials/debug_buttons.png" alt="The Visual Debugger buttons" class="img-responsive center-block img-rounded movie"/> |
| 39 | + <br/> |
| 40 | +</div> |
| 41 | + |
| 42 | +Finally, while the debugger is running the inspector pane will appear on the |
| 43 | +right hand side and list all the objects Python knows about at that moment in |
| 44 | +the execution of your script, along with their associated value. This allows |
| 45 | +you to see into the current state of your program and check things are as you |
| 46 | +expect them to be. |
| 47 | + |
| 48 | +<div class="row"> |
| 49 | + <img src="/img/en/tutorials/debug_inspector.png" alt="The debug inspector" class="img-responsive center-block img-rounded movie"/> |
| 50 | + <br/> |
| 51 | +</div> |
| 52 | + |
| 53 | +Just like when you "Run" a program, a new pane will appear below the code area |
| 54 | +and above the footer. This will contain any textual input and output for your |
| 55 | +program. |
| 56 | + |
| 57 | +<div class="panel panel-warning"> |
| 58 | + <div class="panel-heading"><strong>Code is Read-Only when Debugging</strong></div> |
| 59 | + <div class="panel-body"> |
| 60 | + <p>To avoid the potential for confusion, when the debugger is running, |
| 61 | + you won't be able to change your code. This is because Python's |
| 62 | + debugger runs your program <em>as it existed when you clicked the |
| 63 | + "Debug" button</em>. If you were able to change it after this point, |
| 64 | + there is a possibility that the code the debugger highlights in your |
| 65 | + code area doesn't actually exist in the running program.</p> |
| 66 | + </div> |
| 67 | +</div> |
| 68 | + |
| 69 | +If you click the "Debug" button without having first set some breakpoints then |
| 70 | +the debugger will pause on the first available line. |
| 71 | + |
| 72 | +In any case, once the debugger has control of your program, it will |
| 73 | +highlight the line in your code that Python is about to execute, update the |
| 74 | +inspector with the names and values of all the things it curretly knows about |
| 75 | +and wait for you to do something. At this point you can use the buttons to |
| 76 | +navigate your code. |
| 77 | + |
| 78 | +The "Stop" button does what you expect: the debugger stops and you're returned |
| 79 | +to Python 3 mode. |
| 80 | + |
| 81 | +The "Continue" button will run your code until either the next breakpoint is |
| 82 | +reached (at which point, the debugger will pause again) or your program |
| 83 | +finishes. |
| 84 | + |
| 85 | +The "Step Over" button moves forward through your code one line at a time, no |
| 86 | +matter if there is or isn't a breakpoint on the next line. It means you can |
| 87 | +watch as each individual line of code effects the state of your program. |
| 88 | + |
| 89 | +Sometimes the debugger will highlight a line that calls a function. If you were |
| 90 | +to "Step Over" this line then you'll move onto the next line without seeing |
| 91 | +what the function was doing. But what if you |
| 92 | +wanted to step into the function that was called, to see what that block of |
| 93 | +code did? Well, that's the purpose of the "Step In" button. Instead of |
| 94 | +moving onto the next line in the *current* block of code, it'll skip |
| 95 | +into the function and highlight the first block of code *in the function*. |
| 96 | + |
| 97 | +Of course, you may find yourself in a function which isn't very interesting and |
| 98 | +you just want to move back to the line of code that called it (rather than |
| 99 | +having to keep pressing "Step Over" until you got to the end of the function). |
| 100 | +Happily, this is exactly what the "Step Out" button does: if you're in a |
| 101 | +function, it'll move to the line of code immediately *after* the line that |
| 102 | +called the current function. |
| 103 | + |
| 104 | +All these sorts of interactions are illustrated below. |
| 105 | + |
| 106 | +<div class="row"> |
| 107 | + <img src="/img/en/tutorials/debugger.gif" alt="The Visual Debugger" class="img-responsive center-block img-rounded movie"/> |
| 108 | + <br/> |
| 109 | +</div> |
0 commit comments