Python:Debugging

From PrattWiki
Revision as of 15:21, 24 September 2019 by DukeEgr93 (talk | contribs) (Created page with "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 pag...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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><\code> 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:

# -*- coding: utf-8 -*-
"""
Comments
"""


def fun(a):
    b = a**2
    c = 2*b
    d = 25
    return b+c+d


def main():
    k = 2
    fun(k)
    print(fun(k))


if __name__ == "__main__":
    main()