Python:Debugging

From PrattWiki
Revision as of 15:33, 24 September 2019 by DukeEgr93 (talk | contribs) (Simple Example of Step)
Jump to navigation Jump to search

There are several different ways to debug code. The Spyder user interface provides access to a system called ipdb. There is documentation for it at the Spyder Docs page on Debugging though that page might be a little too complicated to understand at first. This page will seek to go through the basics of how to debug using the Debug features of the Spyder IDE.

Introduction

The debugging tools show up in two places in Spyder. The Debug menu at the top contains a list of all the options for debugging and also shows the icons associated with those tasks. The navigation bar has a subset of the commands in graphical form. The commands are:

  • Debug
  • Step
  • Step Into
  • Step Return
  • Continue
  • Stop
  • Set/Clear breakpoint
  • Set/Edit conditional breakpoint
  • Clear breakpoints in all files
  • List breakpoints

We will now cover each of the options above.

Debug

The Debug command will start the ipdb process on your script. The IPython console prompt will change to read ipdb> and the console will also show you where the debugger is in the code. As a first step, the Debug command will skip to the end of any initial comments in your script. Generally you will only use the Debug command once while debugging - it starts the process for you.

Step

The Step command will tell the debugger to run the current line of code. You can go step by step through your code and monitor how variables change either in the Variable explorer or by asking questions at the ipdb prompt. Note that when ipdb hits a line of code that starts to define a function or calls a function, it will consider defining or calling the entire function as one step. The Step command is probably the most useful button as it goes through your code, but if you are trying to troubleshoot something going on inside a function, you will need to look at the next command.

Step Into

The Step Into command is used when you want to actually look at how a function runs line by line. You need to be careful with this because sometimes it will lead you to "debugging" built-in Python code! If you have a line of code that calls the functions you want to investigate, use Step to get the debugger to the point where it is pointing at that line of code, then use Step Into. The debugger should now be pointing at the function definition of the first function called in that line. You can now use Step to step through that function and any others called in that line. Note: if your line has both functions that you created and built-in functions, ipdb will also try to step through the built in functions in the order that they will run.

Examples

Simple Example of Step

Assume you have the following code:

 1 # -*- coding: utf-8 -*-
 2 """
 3 Comments
 4 """
 5 
 6 
 7 def fun(a):
 8     b = a**2
 9     c = 2*b
10     d = 25
11     return b+c+d
12 
13 
14 def main():
15     k = 2
16     fun(k)
17     print(fun(k))
18 
19 
20 if __name__ == "__main__":
21     main()

and you want to debug it step by step. Here's what you would do and what you would see in the IPython console and the variable explorer assuming you cleared variables before starting:

  • Start the debugging process by either going to the Debug menu and selecting Debug, or by clicking the Start Debugging button, or using the keyboard shortcut for Debug.
    • The console will show that it is pointing at line 4, the last of the commented lines at the top
    • The variable explorer is empty
  • Step through by either going to the Debug menu and selecting Step, or by clicking the Step button, or by using the keyboard shortcut for Step
    • The console will show that it is pointing at line 7 - it hasn't run this line, it is showing that line 7 will be the next line to run.
    • The variable explorer is still empty
  • Take another step
    • The console will show that it is pointing at line 14
    • The variable explorer will show that there is now a function called fun, indicating that lines 7-11 have run
  • Take another step
    • The console will show that it is pointing at line 14
    • The variable explorer will show that there is now a function called main, indicating that lines 14-17 have run
  • Take another step
    • Since __name__ is __main__, the console will show that it went into the if branch and is pointing at like 21
    • The console has not changed
  • Take another step
    • Since main() ran, the console will indicate that a 37 was printed and returned.
    • The console has not changed
  • Take another step. The program is over and the debugger exits