Common Ways to Debug Python Code

Sometimes code breaks.

Identifying and fixing issues quickly and efficiently is a basic skill new coders must acquire. This process of finding and fixing errors in code is known as debugging, and is a fundamental skill for anyone programming in Python or any other language, for that matter.

To debug effectively, you should examine the code carefully, understand how it works, and use tools to help you find the source of the problems.

Today, we’re going to discuss popular debugging techniques that you can apply in your next Python project.

Read Errors First

Errors produce tracebacks that indicate the exact issue.

Python shows the file, line number, and error type, like NameError for missing variables.

One line says “TypeError: unsupported operand type(s) for /: ‘str’ and ‘int’.” That means you tried to divide a string to a number.

Here, we tried dividing a string by a number. Fix it by converting types, such as int("10"), and run again. Tracebacks save hours if you parse them right.​

Print to Track How Everything Flows

Print statements are a great tool for beginners.

Using the print() function along with f-strings at different stages in your code can really help you grasp how everything flows and what the variables are doing.

Consider a function like this:

By calling the area function with the area parameter, we can tell if everything is all right or if math goes wrong. We can remove prints later.

NOTE: All these prints make your code seem cluttered, but at the same time, they give a clear picture of how things flow. You can easily remove prints later.

Use Your Editor’s Power

ManyIntegrated Development Environments (IDEs) come equipped with sophisticated debugging tools, including features like breakpoints, step execution, and the ability to inspect variables.

IDEs like VS Code or PyCharm excel in this area.

Using VS Code Debugger

If you use VS Code, you can set breakpoints in your code and run the debugger to pause execution at those points.

Here’s how to debug the same divide function:

Step1: First, create a file and name it test.py. Then add the following code:

caption

Step 2: Setting a breakpoint

  1. In the left margin of line 35 (result = a * b), set a breakpoint.
  2. You will see a red dot appear there when you click in, which indicates that the breakpoint has been set.
caption

Step 3: Start the debugging

  1. Go to Run > Start Debugging
  2. The debugger will stop running at your breakpoint.
caption

Step 4: Inspect variables

  • Hover over variables to see their current values
  • Use the Variables panel (on the right) to see all local variables
caption

Step 5: Moving through code

Use the debug toolbar to execute the current line and move to the next by pressing F10.

caption

And we keep moving:

caption

And we inspect the new result:

caption

IDE debugging tools offer a user-friendly graphical interface that allows you to examine your program’s state. This makes it much easier to spot and fix problems compared to relying solely on print statements.

Use the Python standard library

Python has pdb, a debugger in your standard library.

The pdb debugger is an interactive tool that lets you pause your program, inspect what is happening, and step through code line by line. Since it is part of the standard library, you can use it without installing anything.

What pdb is designed to do

Pdb gives you a controlled environment where you can stop your program at a specific point, check the values of variables, move forward one line at a time, and continue execution until the next breakpoint.

This makes it useful when print statements are not enough, and you need a closer look at how your code behaves.

Using pdb for debugging

First, you import the module.

Then you use the .set_trace method to navigate your code, inspect the variables, and understand how the program works.

VS Code editor showing a Python file that imports pdb and defines a function multiply(a, b) containing pdb.set_trace() before the return statement. The script calls print(multiply(10, 2)). The terminal shows the program execution paused inside the debugger at the line return a * b, waiting for user commands in the (Pdb) prompt.

This stops execution at that line 5 and opens the debugger prompt.

Then you apply the right commands for debugging your code.

As an example, if you want to check the types of elements in your code at that time, you can use the whatis command:

caption

To keep your code running, you can use the continue command, or its shortcuts, cont or c.

VS Code editor displays a Python script defining a multiply(a, b) function with pdb.set_trace() before return a * b. The terminal shows the debugger paused at the return line. The user runs the command whatis a (showing <class 'int'>) and then enters continue, after which the program resumes and prints the result 20.” class=”wp-image-7259″/></figure><p></p><p>By typing “<em>help</em>” into the <strong>prompt</strong>, you’ll see <strong>a list of all the commands</strong> at your disposal.</p><figure class=Terminal screenshot showing the Python debugger (pdb) help output after typing help. A table lists available debugger commands such as break, bt, continue, c, next, step, quit, list, where, and others. The commands c and continue are highlighted, indicating they resume program execution.
The Python debugger (pdb) help output after typing help. A table lists available debugger commands such as break, bt, continue, c, next, step, quit, list, where, and others.
Note: To step out of the debugger prompt, just enter quit or its alias q

Conclusion

By learning these essential debugging techniques—like using print() statements, the pdb module, and tools in your IDE—you’ll be able to effectively identify and fix problems in your Python code.
Every technique has its own role: print() statements are great for quick checks, pdb is useful for interactive exploration, and IDE debuggers allow for visual inspection.

Although my blog doesn’t support comments, feel free to reply via email or X.