Sei sulla pagina 1di 8

INF-22306 Programming in Python Getting Started with PyCharm

Debugging in PyCharm

Table of Contents
Debugging in PyCharm ............................................................................................................... 1
1. Place breakpoints ............................................................................................................ 1
2. Start the debugger session ............................................................................................... 2
3. Step Over ...................................................................................................................... 3
4. Step Into ....................................................................................................................... 5
5. Resume Program ............................................................................................................. 6
6. Quit the debugger session ................................................................................................ 7

An advantage of having an IDE like PyCharm is that we can make use of the debugger, which can help us
inspect and check our code during and after running, and to correct mistakes. We will show the
debugger’s main functionality in this document.

1. Place breakpoints
A breakpoint is a deliberate pause of your program: the execution of your program will stop at the
breakpoint, and only continue after you indicated so manually. Placing breakpoints gives you control over
execution of your program: you can inspect what is going on and perhaps understand why there is a
mistake. This is especially useful when using loops: if there is a breakpoint inside the loop, the debugger
allows you to inspect the change of variable values step by step, for every repetition.

To get some experience with PyCharm’s debugger, save the file sum.py from Blackboard into your
PyCharm project folder.

We now place a breakpoint in the file. All we need to do is click the sidebar (margin) of the line
where we want the breakpoint to appear, which is next to the line number 11 in the grey area. A
red dot appears to indicate the breakpoint. (To remove it, we can simply click on the same spot again).

Information Technology Group (Sjoukje Osinga), September 2018 1


INF-22306 Programming in Python Getting Started with PyCharm

2. Start the debugger session


Once the breakpoint has been placed, we can start the debugger session. We do not run the code like
usual, because then the breakpoint will just be ignored.

To start the debugger, we have to explicitly use the Debug command. We can start it in several ways:
through the Menu: Run | Debug ‘sum’ (equal to Shift+F9) or Run | Debug... ‘sum’ (equal to
Alt+Shift+F9), or by righ-clicking and selecting Debug ‘sum’, or, or by clicking the small green ‘bug’
icon at the top right of the screen.

Here is a screenshot from the option of clicking the small green ‘bug’ icon on the top right:

A debug window opens in the bottom area of the screen. The debugger has its own Console window. The
Python Console is still available, see the bottom right corner where you can click its tab if needed, but for
now we work with the Debug Console window. In the console we see the result from our input-
command: we are asked to enter a number. As a response, type 4 and press enter:

Information Technology Group (Sjoukje Osinga), September 2018 2


INF-22306 Programming in Python Getting Started with PyCharm

Now, we enter the Debugger-part of the debug window. We see in the Variables window the variables
that Python has in memory at this moment, including their types (all int in this case). There are values
for i (the variable that keeps track of the loop count, which equals 0 because we have just started), n
(the function parameter, which equals the value we entered), and total (the local variable inside the
function that has just been initialised to 0). In the function window, we see that our breakpointline is
highlighted, and that current variable values have also been added in grey (behind the statements).

To the left, in the Frames window, we see the so-called ‘stack’ (Main Thread), which shows program lines
that have been started, including in yellow the underlying part of Python that is required to execute
them. We can see that module sum.py has been executed and that sum_of_sums from this module
has been called so far. Nothing happens at the moment, which is correct, because we introduced a
breakpoint. The debugger is waiting for us to indicate how to proceed.

3. Step Over
At the top of the Debug window, we see a number of controls that allow us to proceed now. First we will
explore Step Over:

Information Technology Group (Sjoukje Osinga), September 2018 3


INF-22306 Programming in Python Getting Started with PyCharm

Click on Step Over once, and see what happens.

We see that in our function window, the next line in function sum_of_sums is highlighted now. The
function has executed the entire line with the breakpoint in it, including a call to sum_to, without going
into details about it, and has added new_total to the Variables window, because it has calculated its
value now.

Click on Step Over again: the for-loop is highlighted now, but there is nothing new to show in the
Variables window [no screenshot from this].

Click on Step Over again: now we see that variable i has received a new value, 1. This is because we
have completed iteration for i=0, and will now do the same iteration again for i=1.

Information Technology Group (Sjoukje Osinga), September 2018 4


INF-22306 Programming in Python Getting Started with PyCharm

4. Step Into
Instead of stepping over like before, let’s now click on Step Into instead:

We see that the debugger shows something different this time:

To the left, we see that call to function sum_to has now been added to the stack. In the function
window, we see that the first line of function sum_to has been highlighted. In the Variables window, we
see only variable n with value 1. In our previous window, we also had n, but with value 4, and other
variables, where did they go? Remember that function sum_to has been called from within function
sum_of_sums with argument i, and we were in the iteration of the for-loop where i=1. Parameter n
from function sum_to has therefore also taken on value 1. The n in sum_to is not the same n as the
one from function sum_of_sums. We are inspecting variables in a different context now.

Click on Step Into a few times more, and notice that we see values appear for i, new_total, and
total. Also these variables are not the same variables as the ones we saw before, although they have
the same name. These are the variables that are valid within the scope of function sum_to, which is
where we are at the moment. Note that the for-loop inside function sum_to is carried out twice: once
for i=0 and once for i=1.

When we have completed all steps in function sum_to, we see that control is returned to function
sum_of_sums (from which this function was called in the first place).

In the Variables window, we now see again the “old” values for i, n, new_total, and total, because
now that the call to sum_to has been completed, we can continue with function sum_of_sums again.

Note that a line has been added to the top of the Variables window: “Return Values”. When we double
click it, we see with what value 1 was returned from function sum_to to, after it had been called from
function sum_of_sums. Also note that sum_to has disappeared from the stack now.

Information Technology Group (Sjoukje Osinga), September 2018 5


INF-22306 Programming in Python Getting Started with PyCharm

Continue with Step Over or Step Into while you try to predict which line will be highlighted next, and
what you will see in the Variables window and in the stack. Do this until you have seen enough. Instead
of completing the entire stepping process, you can also Resume Program or Quit the debugger (see
next).

5. Resume Program
The little green arrow to the left of the stack allows you to stop the line-by-line debugging mode, which
we were doing until now, and allows you to resume the execution of the program until the next
breakpoint. If you haven’t finished the steps yet, you can click this button next.

As you can see, execution of the program stops at our breakpoint: that’s the highlighted line.

If needed, you can do this a few times more, until the entire program has finished. If you want, you can
also remove the breakpoint, after which the program will be finished with only one click on the Resume
Program icon. This is effectively the same as quitting the debugger.

Information Technology Group (Sjoukje Osinga), September 2018 6


INF-22306 Programming in Python Getting Started with PyCharm

6. Quit the debugger session


To quit the debugging process at once, you can press the red square icon:

The result is an empty Variables and stack window.

You can now run the program in ordinary mode again (green triangle icon top right), but if you do, the
Debug window will remain visible. To get rid of it, just close the ‘sum’ program in the Debug window:

Information Technology Group (Sjoukje Osinga), September 2018 7


INF-22306 Programming in Python Getting Started with PyCharm

You can now also remove the breakpoint (click on it), and then you are entirely back in run-mode.

Information Technology Group (Sjoukje Osinga), September 2018 8

Potrebbero piacerti anche