PyCharm 2018.2 Help

Step 2. Debugging Your First Python Application

Finding out the origin of the problem

Remember, in the previous tutorial you've created and run the Car script? Let’s play a little more with it.

Let's see what happens when we start our script up, and try to find out our average speed:

py run error

Oops... PyCharm reports a run-time error: a ZeroDivisionError.

Let’s dig a little deeper into our code to find out what’s going wrong. We can us e the PyCharm debugger to see exactly what’s happening in our code. To start debugging, you have to set some breakpoints first. To create breakpoints, just click in the left gutter:

py breakpoints added

Next, click the run from left gutter icon icon in the left gutter, next to the main clause, and choose Debug 'Car'. PyCharm starts a debugging session and shows the Debug tool window.

py debugToolWindow

In the Console tab, enter S:

py debugToolWindow1

As you see, the breakpoint marker became blue. It means that we've reached the breakpoint; note that the highlighted line of code hasn't yet been executed at this point.

Click the icons actions resume svg button to resume the script execution. Now lo and behold! The exception is here. Another breakpoint appeared as well: by default PyCharm will halt for any exception that wasn't caught in your code, and it'll show an icon of a breakpoint with a lightning bolt. If you're interested in all types of breakpoints supported by PyCharm, see the table of breakpoint signs.

py debugToolWindow2

The Console also shows the error message. So we’ve found our problem. You can also see in the debugger, that the value self.time is equal to zero:

py debugToolWindow3

Surrounding code

To avoid running into the same problem again, let's add an if statement to check whether the time equals zero. To do that, select the statement return self.odometer / self.time in the method average_speed and then press Ctrl+Alt+T (Code | Surround with):

py surround

PyCharm creates a stub if construct, leaving you with the task of filling it with the proper contents.

After editing, we get the following:

py surround result

Let's take a closer look to see how the debugger can show your what your code is doing.

Debugging in detail

The Debug tool window shows dedicated panes for frames, variables, and watches, and the console, where all the input and output information is displayed. If you want the console to be always visible, you can drag it to one of the PyCharm window's edges.


If you want to see what your code does line by line, there's no need to put a breakpoint on every line, you can step through your code.

Let's see what it looks like to step through our example program: click the icons actions resume svg button, go to the Console to ask for the car's average speed (type 'S'), and we can see that we hit our breakpoint.

We can use the stepping toolbar buttons to choose on which line we'd like to stop next.

py stepping toolbar

For example, click the Step Over button (icons actions traceOver svg) and see the blue marker moving to the next line of code:

py debugging1 step over

If you click the Step Into button (icons actions traceInto svg), you will see that after the line action = input("What should I do? [A]ccelerate, [B]rake, " "show [O]dometer, or show average [S]peed?").upper() the debugger goes into the file

py debugging1 step into

However, if you continue using icons actions traceOver svg, you'll see that your application just passes to the next loop:

py debugging1 next input

If you want to concentrate on your own code, use the Step Into My Code button (python icons com jetbrains python debug StepIntoMyCode svg) - thus you'll avoid stepping into library classes.

See the Stepping toolbar and Stepping Through the Program sections for details.


PyCharm allows you to watch any variable. Just click artwork studio icons common add on the toolbar of the Watches tab, and type the name of the variable you want to watch - let it be my_car.time. Note that code completion is available here:

py debugging1 watch completion

At first, you see the time equals nil - it means that the variable is not yet defined:

py debugging1 watch error

However, when the program execution continues to the scope that defines the variable, the watch gets the following view:

py debugging1 watch normal

See Adding, Editing and Removing Watches section for details.

Inline debugging

You may have noticed another PyCharm feature that makes it easy to see what your code is doing: the inline debugger. As soon as you hit any breakpoint, PyCharm shows you the value of many of your variables right in the editor:

py debugging

This inline debugging feature is enabled by default. If you don't see the inline debugging values, please check that it's enabled using the settings icon icons general gearPlain svg on the Debug Tool Window:

inline debugging

Evaluating expressions

Finally, you can evaluate any expression at any time. For example, if you want to see the value of a variable, click the icons debugger evaluateExpression svg button.

Then in the dialog box that opens, click Evaluate:

py debugging1 evaluate expression action

Actually, you could see the same thing with a watch. With evaluate expression you can do things that you can't do with a watch: you can change things.

For example, if you enter the desired value of the odometer, say, 50, and then continue stepping through your script, you will get the following:

py debugging1 evaluate expression change value

See the Evaluating Expressions section for details.


So, you've done it! Congrats! Let's repeat what you've done with the help of PyCharm:

  • Found out the origin of the problem

  • Set breakpoints

  • Stepped through your program

  • Created a watch

  • Evaluated an expression

Last modified: 21 November 2018

See Also