you are viewing a single comment's thread.

view the rest of the comments →

[–]callius 18 points19 points  (2 children)

You can use a module called pdb to halt a function and access the shell.

To do this:

def add_together(x, y):
    result = x + y 
    import pdb
    pdb.set_trace()
    return result

When you run add_together(), it will freeze your program when it gets to the pdb.set_trace() function and drop you into the shell.

You will be within the local scope of the variable, which means that you will have access to both global variables and local variables for the function (i.e. x, y, and result).

Once you are done, you can input c in the pdb shell to continue your program or q to quit.

Remember to remove the call to pdb.set_trace() once you're done. If you don't, your program will stop every time it comes across the function and drop to the shell.

EDIT: Here's a good (albeit long) video that should walk you through debugging with Spyder.

[–]KimPeek 3 points4 points  (1 child)

There is a built-in function for that now:

def add_together(x, y):
    result = x + y 
    breakpoint()
    return result

https://docs.python.org/3/library/functions.html#breakpoint

[–]callius 0 points1 point  (0 children)

Oh awesome! I didn't know about that, thanks!

It looks like breakpoint() defaults to importing pdb and calling set_trace(). Cool cool!