Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

How to Debug in IDLE

For more information on concepts covered in this lesson, you can check out:

00:00 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.

00:41 In the interpreter, you should see DEBUG ON appear just before the prompt, which means the interpreter is ready and waiting. When you execute your Python file, the debugger window will appear.

00:54 In this window, you can inspect the values of your local and global variables as your code executes. This gives you insight into how your data is being manipulated as your code runs.

01:07 You can also click the following buttons to move through your code. Go: Press this to advance execution to the next break point. You’ll learn about these in the next section.

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:14 Quit: This quits the running of the current program. Be careful because there’s no Reverse button. You can only step forward in time through your program’s execution.

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:28 Once you’ve set your breakpoints, you can run your code as you would normally. The debugger window will pop up, and you can start stepping through your code manually.

03:45 When you run a script with a syntax error in it, IDLE will show a pop-up with that error and afterwards will highlight the line where the error occurred.

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 DebugGo to File/Line from the menu.

04:16 This will open up the offending file and take you to the line that contains the error. This feature works regardless of whether or not debug mode is turned on.

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.

04:58 In the next section of the course, we’ll take a look at how you can customize Python IDLE to give you a personalized working environment.

Become a Member to join the conversation.