How to Debug Pythonby Mike Wallace
Computer programs can be quite complex, and this complexity makes tracking down bugs very difficult. Luckily for programmers, there are specialized tools that help analyze software behavior and help to track down bugs. These tools are known as debuggers. The Python programming language downloadable package has a text editor with a built-in debugger. You should learn how to use this debugger so that you can trace the origins of any bugs you encounter.
Open a source code file using the IDLE text editor that comes with the Python download. The IDLE text editor is found in Program Files (or Applications for Macintosh), in the Python directory. A blank source code file opens in the IDLE text editor window.
Right-click any line of source code in the IDLE text editor. A context menu appears. Select "Set Breakpoint." A breakpoint is a line of code that will pause the execution of the program during debugging. Breakpoints should be placed around suspect areas of code that may be the source of bugs. Knowing where to place breakpoints highly depends on the source code in question. For now, just place a breakpoint anywhere to get a feel for how the debugger works.
Right-click any line of source code in the IDLE text editor. A context menu appears. Select "Set Breakpoint." A breakpoint is a line of code that will pause the execution of the program during debugging. Breakpoints should be placed around suspect areas of code that may be the source of bugs. Knowing where to place breakpoints depends greatly on the source code in question. For now, just place a breakpoint anywhere to get a feel for how the debugger works.
Press the F5 key to execute the program. This will cause the Python shell to open up.
Click on the word 'Debug' from the row of words at the top of the Python shell window. A menu will slide open. Select 'Debugger' to launch the Python debugger.
Click on the IDLE text editor window and press F5 again. The program will now run in debug mode, and a small debugger window appears. The debugger has halted the program on the first line of code. The main window of the debugger displays information about that line of code, such as the module it belongs to, the name of the function that encapsulates it, and the value held by variables that exist in the current block of code. The debugger window has five buttons that resume program execution in a variety of ways. These buttons are labeled Go, Step, Over, Out, and Quit.
Press the 'Go' button to execute all of the code up until your first breakpoint. This is a good option when you are interested only in the suspect areas of code around your breakpoints, and want to skip ahead to them.
Press the 'Step' button to execute one line of code at a time. This is a very valuable technique for slowly progressing through a program's execution and watching as each statement gets executed. By monitoring the changes as they occur, you have a good chance of witnessing what exactly is causing your bug. By using the 'Step' button, whenever a function call is made, the debugger will 'step into' the function and debug it. If you want to leave this function, you can press the 'Out' button to 'step out' of it.
Press the 'Over' button to step through the program as in 'Step,' with one difference. When a function call is reached, the debugger will execute the function call and not enter the function's block of code. The debugger will resume debugging on the line immediately following the function call.
Press the 'Quit' button to end debugging.
Items you will need
Click here to provide feedback on this article