logo

Get in touch

Awesome Image Awesome Image

Python Development March 6, 2023

Beginner to Advanced Debugging in Python

Written by Dharmesh Patel

1.8K

What is Debugging in Python?

Developers often find themselves in situations where the code they’ve written is not working quite right. When that happens, a developer debugs their code by instrumenting, executing and inspecting the code to determine what state of the application does not match the assumptions of how the code should be correctly running.

There has never been an unexpectedly short debugging period in the history of computers.

In general, Debugging is the process of identifying and fixing errors or defects in software code, hardware, or any other system. It involves examining the program’s behavior and identifying the root cause of any errors or unexpected behavior that occur.

The goal of debugging is to identify and resolve issues that prevent the software or system from functioning correctly. This involves analyzing the code, inspecting variables and data structures, and testing different scenarios to determine the cause of the error. Once the root cause of the problem is identified, developers can make changes to the code to fix the issue.

Now specific for python language, A debugger is a program that can help you find out what is going on in a computer program. You can stop the execution at any prescribed line number, print out variables, continue execution, stop again, execute statements one by one, and repeat such actions until you have tracked down abnormal behavior and found bugs.

Why Debugging is Important?

There are bugs in every modest sized or larger application. Every developer has to learn how to debug code in order to write programs that work as correctly as time and budget allow.

In addition to fixing bugs, debugging is an important skill for improving the efficiency of an application by optimizing performance and improving the logic. Debugging is a complex skill that takes time and practice for a developer to gain as a capability.

Debugging is an essential skill for software developers, as it helps ensure that software is functioning correctly and free from defects. Debugging can be a time-consuming process, but it is critical for ensuring the quality and reliability of software.

Bugs and errors happen in computer programming because it is an abstract and conceptual activity. Computers manipulate data in the form of electronic signals. Programming languages abstract this information so humans can interact with computers more efficiently. Any type of software has several layers of abstraction, with different components communicating for an application to work correctly. When errors occur, finding and resolving the issue can be challenging. Debugging tools and strategies help to fix problems faster and improve developer productivity. As a result, both software quality and the end-user experience improve.

How to Perform debugging in python?

There are many tools and techniques available for debugging, including debugging tools integrated into development environments, command-line tools, logging, and various testing frameworks. Effective debugging requires careful attention to detail, patience, and a systematic approach to problem-solving.

How does the debugging process work?

The debugging process typically requires the following steps.

  1. Error identification

Developers, testers, and end-users report bugs they discover while testing or using the software. Developers locate the exact line of codes or code module causing the bug. This can be a tedious and time-consuming process.

  1. Error analysis

Coders analyze the error by recording all program state changes and data values. They also prioritize the bug fix based on its impact on software functionality. The software team also identifies a timeline for bug fixing depending on development goals and requirements.

  1. Fix and validation

Developers fix the bug and run tests to ensure the software continues to work as expected. They may write new tests to check if the bug recurs in the future.

Debugging vs. Testing: Debugging and testing are complementary processes that ensure software programs run as they should. After writing a complete section or part of a code, programmers test to identify bugs and errors. Once bugs are found, coders can begin the process of debugging and work towards ridding software of any errors.

What are the coding errors that require debugging?

Software defects arise due to the complexity that is inherent to software development. Minor production errors are also observed after the software is live because customers use it in unexpected ways. We give below some common types of errors that often require the process of debugging.

  1. Syntax errors

A syntax error is a bug that occurs when a computer program has an incorrectly typed statement. It is the equivalent of a typo or spelling error in word processing. The program will not compile or run if syntax errors are present. The code editing software typically highlights this error.

      2. Semantic errors

Semantic errors occur due to the improper use of programming statements. For example, if you are translating the expression 100/(2×10) into Python, you might write:

unnamed (38)

However, this statement is not correct because multiplication and division have the same precedence in Python and are evaluated from left to right. Therefore, this expression computes as (100/2) *10, leading to bugs.

       3. Logic errors

Logic errors occur when programmers misrepresent the step-wise process or algorithm of a computer program. For example, the code may exit a loop too early or may have an incorrect if-then outcome. You can identify logic errors by stepping through the code for several different input/output scenarios.

      4.Runtime errors

Runtime errors occur due to the computing environment in which the software code runs. Examples include insufficient memory space or stack overflow. You can resolve runtime errors by surrounding statements in try-catch blocks or logging the exception with an appropriate message. 

What are some common debugging techniques?

There are several strategies programmers use to minimize errors and reduce the time required for debugging.

  • Printing values

As name suggest, we print out or display values of variables and state at certain times during the execution of an application.

  • Stepping on code line

It will step on each code line by line during the execution of a program. Through this technique, we can trace state of variables changing during execution.

  • Altering path of program

Changing the state of a program to make it do different things.

  • Adding breakpoints

It is indicating line of code where we can stop execution of code to access or manipulate state.

  • Adding trace points

A trace point is a breakpoint with a custom action associated with it. The trace command defines a trace point, which is a point in the target program where the debugger will briefly stop, collect some data, and then allow the program to continue.

  • Adding watchpoint

Stopping the program at certain events. For this, we have to give some variable value and variable value with relational operator. When this condition gets satisfied, the system will automatically stop there.

  • Analyzing output

Viewing the output of a program in a debugger window

  • Logging

Study log files to locate and resolve bugs.

What is the Python Debugger (a python module)?

The Python debugger is an interactive source code debugger for Python programs. It can set conditional breakpoints and single stepping at the source line level. 

It also supports inspection of stack frames, source code listing, and evaluation of arbitrary Python code in any stack frame’s context.

Create a file ‘demopdb.py’ with the following contents:

First, let’s see what this code does. We first import pdb. Then, we define a variable ‘x’ with the value 8.

Next, we define a function ‘power_of_itself’ that returns a number to its own power.

Now here, we slip in a breakpoint in the code; we enter the debugger. The next statement is a call to ‘power_of_itself’, with the argument 7, storing the return value into the variable ‘seven’.

After that, we print the value of ‘seven’. Finally, we store the return value of power_of_itself(3) into the variable ‘three’, and print it out.

Now, let’s save this as ‘demopdb.py’ on the Desktop, and switch to the command line interface (CLI).

Once Command Prompt, and run this python file: 

As you can see, the prompt is at the first line after the breakpoint we set in our code. This is a call to ‘power_of_itself’ with the argument 7.

Now we’re at the debugger, and we can type in some Python debugger commands to proceed with the debugging. 

unnamed (41)

Here, l (small L) lists out the source code. It points at line 6, because that is where we’re at right now.

If we type ‘n’, it will execute until the next line in this function, or until it returns.

In debug console, we can write commands to navigate through code. `help` is command which list all commands that we can use. 

There are plenty of command in debugger mode, refer here.  

How to debug python code in Pycharm IDE? 

There is no one-size-fits-all procedure for debugging applications. Depending on actual requirements you may have to use different actions in different order. This topic provides general guidelines, which represent typical debugging steps. The details on how and when to use particular features are provided in the respective topics.

  1. Define where the program needs to be stopped. This is done using breakpoints. Breakpoints are special markers, which represent places and/or conditions when the debugger needs to step in and freeze the program state. A program, which has been frozen by the debugger is referred to as suspended.
  2. Run your program in debug mode. Just right-click any line in the editor and select the Debug <filename> command from the context menu.
  3. After the program has been suspended, use the debugger to get the information about the state of the program and how it changes during running.
  4. The debugger provides you with the information about variable values, the current state of the threads, breakdown of objects that are currently in the heap, and so on. It also allows you to test your program in various conditions by throwing exceptions (for example, to check how they are handled) or running arbitrary code right in the middle of the program execution.

While these tools let you examine the program state at a particular instant, the stepping feature gives you the control over step-by-step execution of the program. By combining the tools, you can deduce where the bug is coming from and test your program for robustness. 

Example of debugging in Pycharm IDE:

Breakpoints suspends the program upon reaching the line of code where the breakpoint was set. This type of breakpoints can be set on any executable line of code.

Create main.py file and open inside pycharm.

Here, red dots imply breakpoints.  

In the next step, run program in debug mode. To do so, click debug icon in top right of screen in pycharm as shown below or alternatively you can use short-cut (Shift + F9). 

Screenshot from 2023-02-20 18-42-07

As soon as you click debug icon, it will pop debug toolbar in bottom. As you can see in below image, it is stopped running at line 28 which was first breakpoint of script. In debug toolbar, left side indicates details of code where it is currently stopped while right side indicates variables stored in heap till current breakpoint hit. 

unnamed (44)

Now to move forward with execution, we can use ` Resume Program ` button. As name suggest, by clicking this button program continues to run and stops to next breakpoint if any. 

Screenshot from 2023-02-20 18-44-58

As we can see in below image, after clicking ` Resume Program` button, program continue to execute and stopped again at line 5 which is second breakpoint of program. Pycharm provides blue line indicating that we are stopped at line 5 for better visualization.  

unnamed (45)

Also with proceeding code execution, we can see it maintains records in debug toolbar for all breakpoint’s detail that we hit during execution. 

Also in the right side, we can add/check values of variable by typing in input bar. With that input bar, we can also insert new code to check behavior of further program. We can raise error or change value to see if further program is capable of handling it, this is mostly used for manual testing of code. 

Conclusion

In this blog, we covered a few basic and common uses of debugging with examples:

  • types of errors where you can use debugging
  • common techniques for debugging like printing expressions 
  • explored python debugger module (pdb)
  • commands used during debugging mode
  • stepping through code in Pycharm IDE
  • using breakpoints
  • modifying values in-between execution of code
  • displaying expressions
  • displaying values at different point in code execution

I prefer print statements myself to single line debuggers such as PDB, but visual debuggers are much better (as you can see what’s changed, plus you get the enhanced navigation, etc.) It’s just my opinion, but I haven’t found anything that comes close to Pycharm for debugging.

My preferred method of debugging Python and other languages when a debugger is absent is to add logging. Python has extensive logging facilities and the documentation is superb.

Hope it’s been helpful to you. If you’re curious about learning more, see Python’s pdb docs. Happy Pythoning!

Bringing Software Development Expertise to Every
Corner of the World

United States

India

Germany

United Kingdom

Canada

Singapore

Australia

New Zealand

Dubai

Qatar

Kuwait

Finland

Brazil

Netherlands

Ireland

Japan

Kenya

South Africa