How can I debug Python code

Tips and tricks for debugging python on the pi-top[3] & pi-top[4}

It is easy to get frustrated when something doesn’t work. At times, it can be enough to give up. One helpful way to help avoid this situation is a methodical approach. Here are some tips for helping to prevent and fix errors in your code.

The following is a reinterpretation of Hartley Brody’s excellent blog post, 10 Debugging Tips for Beginners: How to Troubleshoot and Fix Your Code Without Pulling Your Hair Out.

Preventing errors

Start with code that already works

Find code samples where possible. Start from something that you can verify without modifying it.

Run your code every time you make a small change

This will save a lot of time in the long run. Whenever you make a change, verify it. Even if you think it’s so small that it couldn’t possibly be wrong. If you don’t, you might make 5 or 6 changes that you thought you couldn’t possibly get wrong, and you’ll spend more time in the long run trying to figure out what you did.

Focus on readability

  • Use sensible names for variables and functions
  • Use comments where it is tricky to understand what is happening quickly

Print things a lot

On every single line of code, you should have a sense of what all of the variables’ values are. print statements are an essential part of a programmer’s toolkit.

It’s usually helpful to print a fixed string right before you print a variable, so that you can easily identify what you are actually printing out, and trace the print out in the result back to somewhere in your code.

Sometimes you may not be sure if a block of code is being run at all. A simple print "got here" is usually enough to see whether you have a mistake in your control flow like if-statements or for-loops.

Bad:

my_var = 0

target = 10

print(my_var)




while my_var < target:

my_var = my_var + 1

print(my_var)

print(target)

 

Good:

my_var = 0

my_target = 10


print("My variable starts at", my_var)

while my_var < my_target:

my_var = my_var + 1

print("My variable is currently", my_var)

print("My target is", my_target)


print("My variable has reached my target")

Finding the source of an errors

Read the error message

Python’s output from an error can seem scary, but it at least provides a useful way to see where the interpreter failed to do what you wanted, and the sort of issue that it had. If you spend a bit of time reading it, finding that part of your code, and checking carefully, you can see the mistake.

Add more information about what your code is doing

If you can, add more print statements to determine what is going on. Figure out the part of the code responsible for the error, and know that it might not be where your mistake is - sometimes, an earlier mistake will cause an error later on.

Comment-out code

As mentioned earlier, comments are a useful tool. However, you can also use them to temporarily “comment out” code that you don’t want to lose track of, but that you just don’t want running right now. Disabling parts of the code can be a helpful way to figure out where an issue is occurring. This is more helpful when your code is getting long, and you want to make it run faster and/or make it easier to search the remainder of the code for the mistake.

Just make sure you don’t comment out some code that sets variables that your program is using later on.

And of course, make sure you remove the comment characters so that it turns back into instructions when you’re done testing the other sections.

Finding solutions to errors

Searching Stack Overflow For Coding Solutions

Still having trouble?

Take a break