Debugging Guide

Let’s say you have a piece of code that isn’t producing the answer you expected. The Pycharm debugger can help you go through your code one line at a time to figure out what it going wrong.

Here’s a screen shot labeling the various icons that the following steps are going to refer to:

Have the file with the code you want to debug in the editing window (as if you were just working on the assignment). Now:

  1. Under the Run menu, select the "Debug <your file name>" option (in my version, this is the second option, just under the one to run the file). This will open some new windows/areas within your Pycharm window, in particular creating one called “Debugger”.

  2. Next, you want to tell Pycharm where to start going through your code line by line (this will save you some clicking through). Say you have a test case or sample expression that is failing. Click in the area just to the right of the line number where you want Pycharm to start showing you step-by-step execution. You should get a red circle between the line number and the code.

    This is called a “breakpoint”, and it tells Pycharm “run my file until you get here, then wait so I can explore what happens step-by-step”.

  3. Click the little green bug icon in the lower window to start Pycharm running your program (before you start the debugger, this icon will be in the same spot as the “restart the debugger” icon as marked in the figure). It will run up until your breakpoint, then stop and wait for you to do more. You will see the variables area populated with the values of the variables just before you run the code at the breakpoint line.

  4. There’s a strip of blue/gray arrows at the top of the debug area at the bottom of your window. Click the one with the arrow pointing straight down at the gray bar (it will read as “step into” if you hover your mouse over it). Clicking this button tells Pycharm to execute the next line of your code (but no more!). You will see which line Pycharm runs next and the variables window will update.

  5. Keep stepping through until you understand how your program is running. Figuring out how to fix the problem might be harder, but you’ll at least see what your code is currently doing.

You can restart the debugging session by clicking the circular green arrow on the left side of the debugging area.

More debugging tips

You can turn off a breakpoint, or add another one, by clicking to the right of the line numbers as you did to put in the first breakpoint. A program can have as many breakpoints as you want.

If your program has multiple breakpoints, you can tell Pycharm to start running again until it hits the next one. To do that, use the green icon with the green bar and arrow on the left side of the debug area.

You can switch between seeing the debugger contents and the console (what has printed out) by clicking between the debug and console tabs in the lower area of the screen.

There isn’t a way to “back up” to the previous step. If you need to see something again, restart the debugging session.