
#Python script debugger how to
In the next section, you’ll look at some buggy code and learn how to fix it with IDLE. Likewise, if you’re already inside a function or loop, then the Out button executes the remaining code inside the function or loop body and then pauses. The Over button takes you directly to the result of running that function. In other words, if you’re about to step into a function with the debugger, then you can still run that function’s code without having to step all the way through each line of it. The Over button works sort of like a combination of Step and Go. What do you think will happen when you press Go one more time? Since the for loop only iterates three times, when you press Go again, the program will finish running. Since you set the breakpoint on line 3, which is inside the for loop, the debugger stops on this line each time it goes through the loop. The interactive window also displays the output from having run the line with print() the first time through the loop.Įach time you press the Go button, the debugger runs the code continuously until it reaches the next breakpoint. However, the values of the variables i and j are now 2 and 4. The Debug window now looks like this:ĭo you see what changed? The same message as before is displayed in the Stack panel, indicating that the debugger is waiting to execute line 3 again. By clicking Go, you told the debugger to run your code continuously until it reached either a breakpoint or the end of the program. If you look at the Locals panel, then you’ll see that both variables i and j have the values 1 and 2, respectively. The Stack panel now shows the following message indicating that it’s waiting to execute line 3: Click Go and watch what happens in the Debug window: Just like before, the Stack panel of the Debug window indicates that the debugger has started and is waiting to execute line 1. The editor window should now look like this: Set a breakpoint on the line of code with the print() statement. This won’t close the window, and you’ll want to keep it open because you’ll be using it again in just a moment. Go ahead and press Quit at the top of the Debug window to turn off the debugger for now. To remove a breakpoint, right-click the line with the breakpoint and select Clear Breakpoint. IDLE highlights the line in yellow to indicate that your breakpoint has been set. To set a breakpoint, right-click ( Ctrl-click on a Mac) the line of code in your editor window that you would like to pause at and select Set Breakpoint.

Rather than clicking the Step button all day long, you can set a breakpoint that tells the debugger to continuously run all code until it reaches the breakpoint.īreakpoints tell the debugger when to pause code execution so that you can take a look at the current state of the program. Often, you may know that the bug must be in a particular section of your code, but you may not know precisely where. First, the message in the Stack panel changes to the following: There are two differences to pay attention to here. The Debug window changes a bit to look like this: Go ahead and click Step at the top left-hand corner of the Debug window.


In the following sections, you’ll explore what each of these buttons does, starting with Step. These buttons control how the debugger moves through your code. There are five buttons located at the top left-hand corner of the Debug window: Go, Step, Over, Out, and Quit. As your program runs, you’ll see variables declared in the code displayed in this window so that you can keep track of their value. These are internal system variables that you can ignore for now. The '_main_'.module() part of the message refers to the fact that you’re currently in the main section of the program, as opposed to being, for example, in a function definition before the main block of code has been reached.īelow the Stack panel is a Locals panel that lists some strange looking stuff like _annotations_, _builtins_, _doc_, and so on. This tells you that line 1 (which contains the code for i in range(1, 4):) is about to be run but hasn’t started yet.
