Wednesday, September 9, 2020

Find & Fix Code Bugs in Python: Debug With IDLE

Everyone makes mistakes—even seasoned professional developers! Python’s interactive interpreter, IDLE, is pretty good at catching mistakes like syntax errors and runtime errors, but there’s a third type of error that you may have already experienced. Logic errors occur when an otherwise valid program doesn’t do what was intended. Logic errors cause unexpected behaviors called bugs. Removing bugs is called debugging.

A debugger is a tool that helps you hunt down bugs and understand why they’re happening. Knowing how to find and fix bugs in your code is a skill that you’ll use for your entire coding career!

In this tutorial, you’ll:

  • Learn how to use IDLE’s Debug Control window
  • Practice debugging on a buggy function
  • Learn alternative methods for debugging your code

Use the Debug Control Window#

The main interface to IDLE’s debugger is the Debug Control window, or the Debug window for short. You can open the Debug window by selecting Debug→Debugger from the menu in the interactive window. Go ahead and open the Debug window.

Whenever the Debug window is open, the interactive window displays [DEBUG ON] next to the prompt to indicate that the debugger is open. Now open a new editor window and arrange the three windows on your screen so that you can see all of them simultaneously.

In this section, you’ll learn how the Debug window is organized, how to step through your code with the debugger one line at a time, and how to set breakpoints to help speed up the debugging process.

The Debug Control Window: An Overview#

To see how the debugger works, you can start by writing a simple program without any bugs. Type the following into the editor window:

 1 for i in range(1, 4):
 2     j = i * 2
 3     print(f"i is {i} and j is {j}")

Save the file, then keep the Debug window open and press F5. You’ll notice that execution doesn’t get very far.

The Debug window will look like this:

Image of IDLE's Debug window

Notice that the Stack panel at the top of the window contains the following message:

> '__main__'.<module>(), line 1: for i in range(1, 4):

This tells you that line 1 (which contains the code for i in range(1, 4):) is about to be run but hasn’t started yet. The '__main__'.module() part of the message refers to the fact that you’re currently in the main section of the program, as opposed to being, for example, in a function definition before the main block of code has been reached.

Below the Stack panel is a Locals panel that lists some strange looking stuff like __annotations__, __builtins__, __doc__, and so on. These are internal system variables that you can ignore for now. As your program runs, you’ll see variables declared in the code displayed in this window so that you can keep track of their value.

There are five buttons located at the top left-hand corner of the Debug window: Go, Step, Over, Out, and Quit. These buttons control how the debugger moves through your code.

In the following sections, you’ll explore what each of these buttons does, starting with Step.

The Step Button#

Go ahead and click Step at the top left-hand corner of the Debug window. The Debug window changes a bit to look like this:

Read the full article at https://realpython.com/python-debug-idle/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]



from Real Python
read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...