Step 2. Debug 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 and modify the average_speed
function as follows:
Let's see what happens when we start our script up, and try to find out the average speed by typing s
in the command line:
![Error messages Error messages](https://resources.jetbrains.com/help/img/idea/2022.3/py_run_error.png)
Oops... PyCharm reports a runtime error: a ZeroDivisionError
.
Let’s dig a little deeper into our code to find out what’s going wrong. We can use 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 gutter
![Adding breakpoints Adding breakpoints](https://resources.jetbrains.com/help/img/idea/2022.3/py_breakpoints_added.png)
Next, click the icon in the gutter, next to the
main
clause, and choose Debug 'Car'.
![Debug command Debug command](https://resources.jetbrains.com/help/img/idea/2022.3/py_debug_menu_command.png)
PyCharm starts a debugging session and shows the Debug tool window
![Debug tool window Debug tool window](https://resources.jetbrains.com/help/img/idea/2022.3/py_debugToolWindow.png)
Click the button to proceed with the script execution and in the Console tab, enter
S
and press Enter:
![Debug tool window: Console tab Debug tool window: Console tab](https://resources.jetbrains.com/help/img/idea/2022.3/py_debugToolWindow1.png)
Click the button to resume the script execution. 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.
![Exception breakpoint Exception breakpoint](https://resources.jetbrains.com/help/img/idea/2022.3/py_debugToolWindow2.png)
The debugger 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:
![aself.time is equal to zero aself.time is equal to zero](https://resources.jetbrains.com/help/img/idea/2022.3/py_debug_exception_zero.png)
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 ( ):
![Surround code Surround code](https://resources.jetbrains.com/help/img/idea/2022.3/py_surround.png)
PyCharm creates a stub if
construct, leaving you with the task of filling it with the proper contents.
![A stub for surrunding with an if statement A stub for surrunding with an if statement](https://resources.jetbrains.com/help/img/idea/2022.3/py_surround_if_stub.png)
After editing, we get the following:
![Results of the surrounded code Results of the surrounded code](https://resources.jetbrains.com/help/img/idea/2022.3/py_surround_result.png)
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.
Stepping
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 button, go to the Console to ask for the car's average speed (type 'S'), and we can see that we press our breakpoint.
We can use the stepping toolbar buttons to choose on which line we'd like to stop next.
![Stepping toolbar Stepping toolbar](https://resources.jetbrains.com/help/img/idea/2022.3/py_stepping_toolbar.png)
For example, click the Step Over button and see the blue marker moving to the next line of code:
![Stepping over during the debugging Stepping over during the debugging](https://resources.jetbrains.com/help/img/idea/2022.3/py_debugging1_step_over.png)
If you click the Step Into button , 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 parse.py:
![Stepping into during the debugging Stepping into during the debugging](https://resources.jetbrains.com/help/img/idea/2022.3/py_debugging1_step_into.png)
However, if you continue using , you'll see that your application just passes to the next loop:
![Debugging: passing to the next loop Debugging: passing to the next loop](https://resources.jetbrains.com/help/img/idea/2022.3/py_debugging1_next_input.png)
If you want to concentrate on your own code, use the Step Into My Code button - thus you'll avoid stepping into library classes.
See the Stepping toolbar and Step through the program sections for details.
Watching
PyCharm allows you to watch any variable. Just type the name of the variable you want to watch in the Evaluate and Watch field - let it be my_car.time
. Note that code completion is available here. Then click on next to the field.
![Watch completion Watch completion](https://resources.jetbrains.com/help/img/idea/2022.3/py_debugging1_watch_completion.png)
At first, you see the time equals 0 - it means that the variable is not yet defined:
![Watch error Watch error](https://resources.jetbrains.com/help/img/idea/2022.3/py_debugging1_watch_error.png)
However, when the program execution continues to the scope that defines the variable, the watch gets the following view:
![Watched variable gets a value Watched variable gets a value](https://resources.jetbrains.com/help/img/idea/2022.3/py_debugging1_watch_normal.png)
See 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 press any breakpoint, PyCharm shows you the value of many of your variables right in the editor:
![Inline debugging Inline debugging](https://resources.jetbrains.com/help/img/idea/2022.3/py_debugging_inline.png)
This inline debugging feature is enabled by default. If you don't see the inline debugging values, check that it's enabled using the settings icon on the debug toolbar :
![Show values inline Show values inline](https://resources.jetbrains.com/help/img/idea/2022.3/py_inline_debugging_command.png)
Summary
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