Tutorial: Debugging from the Python Shell

Index of All Documentation » Wing Tutorial » Tutorial: Debugging »

In addition to launching code to debug from Wing's menu bar and Debug menu, it is also possible to debug code that is entered into the Python Shell (and in Wing Pro the Debug Probe).

Enable this now by clicking on the bug icon in the top right of the Python Shell. Once this is done, the status message at the top of the Python Shell should change to include Commands will be debugged and an extra margin is shown in which you can set breakpoints. Wing will reach those breakpoints, as well as any breakpoints in editors for code that is invoked. Any exceptions will also be reported in the debugger.

Let's try this out. Paste the following into the Python Shell and press Enter so that you are returned to the >>> prompt:

def test_function():
  x = 10
  x += 5
  y = 20

Then place a breakpoint on the line that reads print(x) by clicking in the breakpoint margin at the left of the line text and prompt.

Next type this into the Python Shell and press Enter:


Wing should reach the breakpoint on the print(x) line.

You can now work with the debugger in the same way that you would if you have launched code from the toolbar or Debug menu. Try stepping and viewing the values of x and y as they change, either in the Stack Data tool or by hovering the mouse over the variable names.

Take a look at the stack in the Call Stack or Stack Data tool to see how stack frames that occur withing the Python Shell are listed. You can move up and down the stack just as you would if your stack frames were in an editor.

Notice that if you step off the end of the call, you will return to the shell prompt. If you press the red Stop button in the toolbar or select Stop Debugging from the Debug menu, Wing will complete execution of the code without debug and return you to the >>> prompt. Note that the code is still executed to completion in this case becaused there is no way to simply abandon a number of stack frames in the Python interpreter.

Recursive Debugging

By default Wing will not return you to the >>> prompt until your code has finished executing. In Wing Pro, it is possible to enable recursive debugging but this can be quite confusing, so it is disabled by default.

To try this out, check the Enable Recursive Debug item in the Options menu in the Python Shell. Then type test_function() again in the Python Shell. You will see that the shell returns immediately to the >>> prompt even though you are now at the breakpoint you set earlier on print(x). Note that the message area in the Python Shell indicates that you are debugging recursively and gives you the level to which you have recursed (for example Debugging recursively (R=2) indicates two levels of recursive debugging).

Try typing test_function() again (or just press the Up arrow on the keyboard) and press Enter. This is essentially the same thing as invoking test_function() from the line at which the debugger is currently paused, in this case within test_function itself.

Try doing this several times. Each time you do this, another level of recursive debugging is entered. Look at the Call Stack tool and go up and down the stack to better understand what is happening.

Now if you press Continue in the toolbar or use Start / Continue in the Debug menu you will exit one level of recursion. Similarly, Stop exits one level of recursion without debugging the remainder of that recursive invocation.