For more information on concepts covered in this lesson, you can check out:
How to Debug in IDLE
00:04 A bug is an unexpected problem in your program. They can appear in many forms, and some are more difficult to fix than others. Some bugs are tricky enough that you won’t be able to catch them by just reading through your program. Luckily, Python IDLE provides some basic tools that will help you debug your programs with ease. If you want to run your code with a built-in debugger, then you’ll need to turn this feature on. To do so, select Debug → Debugger from the Python IDLE menu. Note that this menu only appears when you have the shell window in focus.
01:18 If you haven’t set any break points, the code will run as normal. Step: Press this to execute the current line and go to the next one. If you do this with anything but the simplest of scripts, you’ll see that it can be a long process as each line of code can take multiple steps.
01:46 Over: If the current line of code contains a function call, then press this to step over that function. In other words, execute that function and go to the next line, but don’t pause while executing the function, unless of course there’s a break point. Out: If the current line of code is in a function, then press this to step out of the function. In other words, continue the execution of this function until you return from it.
02:26 You’ll also see four checkboxes in the debug window. Globals: Your program’s global information. Locals: Your program’s local information during execution. Stack: The functions that run during execution. Source: Your file in the IDLE editor. When you select one of these, you’ll see the relevant information in the debug window.
02:53 A breakpoint is a line of code that you’ve identified as a place where the interpreter should pause while running the code. They will only work when debug mode is turned on, so make sure you’ve done that first. To set a breakpoint, right-click and choose Set Breakpoint on the line of code that you wish to pause.
03:13 This will highlight the line of code in yellow as a visual indication of a set breakpoint. You can set as many breakpoints in your code as you like. To undo a breakpoint, right-click the same line again and select Clear Breakpoint.
03:56 When you say an error reported to you in the interpreter, IDLE lets you jump right to the offending file or line from the interpreter window. All you have to do is highlight the reported line number or file name with your cursor and either right-click and select Go to File/Line or select Debug → Go to File/Line from the menu.
04:27 Python IDLE also provides a tool called a Stack Viewer. You can access it under the Debug option in the menu bar. This tool will show you the traceback of an error as it appears on the stack of the last error or exception that Python IDLE encountered while running your code.
04:43 When an unexpected or interesting error occurs, you might find it helpful to take a look at the stack. Otherwise, this feature can be difficult to parse and likely won’t be useful to you unless you’re writing very complicated code.
Become a Member to join the conversation.