PyCharm 2017.3 Help

Part 1. Debugging Python Code

Preparing an example

Do you remember the quadratic formula from math class? This formula is also known as the A, B, C formula, it’s used for solving a simple quadratic equation: ax2 + bx + c = 0. As manually solving quadratic formulas gets boring quickly, let’s replace it with a script.

Copy the following code into a file in your project (though it is recommended to type this code manually):

import math class Solver: def demo(self, a, b, c): d = b ** 2 - 4 * a * c if d > 0: disc = math.sqrt(d) root1 = (-b + disc) / (2 * a) root2 = (-b - disc) / (2 * a) return root1, root2 elif d == 0: return -b / (2 * a) else: return "This equation has no roots" if __name__ == '__main__': solver = Solver() while True: a = int(input("a: ")) b = int(input("b: ")) c = int(input("c: ")) result = solver.demo(a, b, c) print(result)

As you see, there is the main clause here. It means that execution will begin with it, let you enter the desired values of the variables a, b and c, and then enter the method demo.

Placing breakpoints

To place breakpoints, just click the left gutter next to the line you want your application to suspend at:

/help/img/idea/2017.3/py_debugging_breakoints.png

Refer to the section Breakpoints for details.

Starting the debugger session

OK now, as we've added breakpoints, everything is ready for debugging.

PyCharm allows starting the debugger session in several ways. Let's choose one: click /help/img/idea/2017.3/run_from_left_gutter_icon.png in the left gutter, and then select the command Debug 'Solver' in the pop-up menu that opens:

/help/img/idea/2017.3/py_debugging_start.png

The debugger starts, shows the Console tab of the Debug tool window, and lets you enter the desired values:

/help/img/idea/2017.3/py_debugging_console.png

By the way, in the Console, you can show a Python prompt and enter the Python commands. To do that, click /help/img/idea/2017.3/icon_showCommandLine.png:

/help/img/idea/2017.3/py_debugging_show_prompt.png

If your debug console is too short for the prompt icon to be visible, click >>>.

Then the debugger suspends the program at the first breakpoint. It means that the line with the breakpoint is not yet executed. The line becomes blue:

/help/img/idea/2017.3/py_debugging_stop_at_first_breakpoint.png

On the stepping toolbar of the Debugger tab, click the button /help/img/idea/2017.3/debug_resume.png, to move to next breakpoint.

Inline debugging

In the editor, you see the grey text next to the lines of code:

/help/img/idea/2017.3/py_inline_debugging.png

What does it mean?

This is the result of the so-called inline debugging. The first lines show the address of the Solver object and the values of the variables a, b and c you've entered.

Inline debugging can be turned off.

Note that you can do it in course of the debugger session!

Let's step!

So, you've clicked the button /help/img/idea/2017.3/debug_resume.png, and now see that the blue marker moves to the next line with the breakpoint.

If you use the stepping toolbar buttons, you'll move to the next line. For example, click the button /help/img/idea/2017.3/frames_step_over.png. Since the inline debugging is enabled, the values of the variables show in italic in the editor.

/help/img/idea/2017.3/py_debugging_step.png

If you click the button /help/img/idea/2017.3/frames_step_into.png, you will see that after the line a = int(input("a: ")) the debugger goes into the file parse.py:

/help/img/idea/2017.3/py_debugging_step_into.png

However, if you continue using the button /help/img/idea/2017.3/frames_step_over.png, you'll see that your application just passes to the next loop:

/help/img/idea/2017.3/py_debugging_next_input.png

If you want to concentrate on your own code, use the button Step Into My Code (/help/img/idea/2017.3/step_into_my_code.png) - thus you'll avoid stepping into library classes.

Refer to the sections Stepping Through the Program and Stepping toolbar for details.

Watching

PyCharm allows you to watch a variable. Just click /help/img/idea/2017.3/show-watches.png on the toolbar of the Variables tab, and type the name of the variable you want to watch. Note that code completion is available:

/help/img/idea/2017.3/py_debugging_watches_completion.png

At first, you see an error - it means that the variable is not yet defined:

/help/img/idea/2017.3/py_debugging_watches_error.png

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

/help/img/idea/2017.3/py_debugging_watches_normal.png

Refer to the sections Debug Tool Window. Variables and Debug Tool Window. Watches.

Evaluating expressions

Finally, you can evaluate any expression at any time. For example, if you want to see the value of the variable, click the button /help/img/idea/2017.3/variables_evaluate_expr.png, and then in the dialog box that opens, click Evaluate:

/help/img/idea/2017.3/py_debugging_evaluate.png

PyCharm gives you the possibility to evaluate any expression. For example:

/help/img/idea/2017.3/py_debugging_evaluate1.png

Refer to the section Evaluating Expressions.

You can also click the button /help/img/idea/2017.3/icon_showCommandLine.png in the Debug console, and enter some commands that show the variables values. For example, with IPython installed, you can easily get an expression value:

/help/img/idea/2017.3/py_debugging_evaluate_ipython.png

Summary

This brief tutorial is over - congrats! Let's repeat what you've learnt from it:

  • You've refreshed your knowledge of the breakpoints and learnt how to place them.
  • You've learnt how to begin the debugger session, and how to show the Python prompt in the debugger console.
  • You've refreshed your knowledge about the inline debugging.
  • You've tried hands on stepping, watches and evaluating expressions.

The next step is intended for the Professional edition users - this is Debugging Django Templates.

See Also

Procedures:

Last modified: 15 September 2017