Science and technology

7 steps for looking down Python code bugs

It is three pm on a Friday afternoon. Why? Because it’s at all times three pm on a Friday when issues go down. You get a notification buyer has discovered a bug in your software program. After you recover from your preliminary disbelief, you contact DevOps to seek out out what is going on with the logs on your app, since you keep in mind receiving a notification that they have been being moved.

Turns out they’re someplace you’ll be able to’t get to, however they’re within the technique of being moved to an online utility—so you’ll have this nifty utility for looking and studying them, however after all, it isn’t completed but. It must be up in a few days. I do know, completely unrealistic scenario, proper? Unfortunately not; it appears logs or log messages usually come up lacking at simply the flawed time. Before we monitor down the bug, a public service announcement: Check your logs to ensure they’re the place you suppose they’re and logging what you suppose they need to log, recurrently. Amazing how these items simply change if you aren’t trying.

OK, so that you discovered the logs or tried the decision, and certainly, the shopper has discovered a bug. Maybe you even suppose the place the bug is.

You instantly open the file you suppose may be the issue and begin poking round.

1. Don’t contact your code but

Go forward and have a look at it, possibly even provide you with a speculation. But earlier than you begin mucking about within the code, take that decision that creates the bug and switch it right into a take a look at. This will likely be an integration take a look at as a result of though you could have suspicions, you don’t but know precisely the place the issue is.

Make certain this take a look at fails. This is necessary as a result of generally the take a look at you make does not mimic the damaged name; that is very true if you’re utilizing an online or different framework that may obfuscate the exams. Many issues could also be saved in variables, and it’s sadly not at all times apparent, simply by trying on the take a look at, what name you make within the take a look at. I am not going to say that I’ve created a take a look at that handed after I was making an attempt to mimic a damaged name, however, properly, I’ve, and I do not suppose that’s significantly uncommon. Learn from my errors.

2. Write a failing take a look at

Now that you’ve a failing take a look at or possibly a take a look at with an error, it’s time to troubleshoot. But earlier than you do this, let’s do a evaluation of the stack, as this makes troubleshooting simpler.

The stack consists of the entire duties you will have began however not completed. So, if you’re baking a cake and including the flour to the batter, then your stack can be:

  • Make cake
  • Make batter
  • Add flour

You have began making your cake, you will have began making the batter, and you might be including the flour. Greasing the pan is just not on the record because you already completed that, and making the frosting is just not on the record as a result of you haven’t began that.

If you might be fuzzy on the stack, I extremely suggest taking part in round on Python Tutor, the place you’ll be able to watch the stack as you execute strains of code.

Now, if one thing goes flawed together with your Python program, the interpreter helpfully prints out the stack for you. This signifies that no matter this system was doing in the mean time it grew to become obvious that one thing went flawed is on the underside.

three. Always verify the underside of the stack first

Not solely is the underside of the stack the place you’ll be able to see which error occurred, however usually the final line of the stack is the place you’ll find the difficulty. If the underside does not assist, and your code has not been linted shortly, it’s superb how useful it may be to run. I like to recommend pylint or flake8. More usually than not, it factors proper to the place there may be an error that I’ve been overlooking.

If the error is one thing that appears obscure, your subsequent transfer may simply be to Google it. You may have higher luck if you happen to do not embody data that’s related solely to your code, just like the title of variables, recordsdata, and so on. If you might be utilizing Python three (which you need to be), it is useful to incorporate the three within the search; in any other case, Python 2 options are inclined to dominate the highest.

Once upon a time, builders needed to troubleshoot with out the good thing about a search engine. This was a darkish time. Take benefit of all of the instruments obtainable to you.

Unfortunately, generally the issue occurred earlier and solely grew to become obvious in the course of the line executed on the underside of the stack. Think about how forgetting so as to add the baking powder turns into apparent when the cake does not rise.

It is time to lookup the stack. Chances are fairly good that the issue is in your code, and never Python core and even third-party packages, so scan the stack on the lookout for strains in your code first. Plus it’s often a lot simpler to place a breakpoint in your individual code. Stick the breakpoint in your code slightly additional up the stack and go searching to see if issues appear like they need to.

“But Maria,” I hear you say, “this is all helpful if I have a stack trace, but I just have a failing test. Where do I start?”

Pdb, the Python Debugger.

Find a spot in your code the place this name ought to hit. You ought to be capable to discover at the very least one place. Stick a pdb break in there.

A digression

Why not a print assertion? I used to depend upon print statements. They nonetheless turn out to be useful generally. But as soon as I began working with sophisticated code bases, and particularly ones making community calls, print simply grew to become too gradual. I ended up with print statements in all places, I misplaced monitor of the place they have been and why, and it simply received sophisticated. But there’s a extra necessary cause to largely use pdb. Let’s say you set a print assertion in and uncover that one thing is flawed—and will need to have gone flawed earlier. But trying on the perform the place you set the print assertion, you don’t have any thought how you bought there. Looking at code is an effective way to see the place you’re going, however it’s horrible for studying the place you’ve got been. And sure, I’ve executed a grep of my code base on the lookout for the place a perform known as, however this may get tedious and does not slender it down a lot with a preferred perform. Pdb could be very useful.

You observe my recommendation, and put in a pdb break and run your take a look at. And it whooshes on by and fails once more, with no break in any respect. Leave your breakpoint in, and run a take a look at already in your take a look at suite that does one thing similar to the damaged take a look at. If you will have a good take a look at suite, you must be capable to discover a take a look at that’s hitting the identical code you suppose your failed take a look at ought to hit. Run that take a look at, and when it will get to your breakpoint, do a w and have a look at the stack. If you don’t have any thought by trying on the stack how/the place the opposite name could have gone haywire, then go about midway up the stack, discover some code that belongs to you, and put a breakpoint in that file, one line above the one within the stack hint. Try once more with the brand new take a look at. Keep going forwards and backwards, shifting up the stack to determine the place your name went off the rails. If you get all the best way as much as the highest of the hint with out hitting a breakpoint, then congratulations, you will have discovered the difficulty: Your app was spelled flawed. No expertise right here, nope, none in any respect.

four. Change issues

If you continue to really feel misplaced, strive making a brand new take a look at the place you differ one thing barely. Can you get the brand new take a look at to work? What is completely different? What is similar? Try altering one thing else. Once you will have your take a look at, and possibly extra exams in place, it’s protected to begin altering issues within the code to see if you happen to can slender down the issue. Remember to begin troubleshooting with a recent commit so you’ll be able to simply again out adjustments that don’t assist. (This is a reference to model management, if you happen to aren’t utilizing model management, it can change your life. Well, possibly it can simply make coding simpler. See “A Visual Guide to Version Control” for a pleasant introduction.)

5. Take a break

In all seriousness, when it stops feeling like a enjoyable problem or recreation and begins turning into actually irritating, your greatest plan of action is to stroll away from the issue. Take a break. I extremely suggest going for a stroll and making an attempt to consider one thing else.

6. Write every thing down

When you come again, if you happen to aren’t abruptly impressed to strive one thing, write down any data you will have about the issue. This ought to embody:

  • Exactly the decision that’s inflicting the issue
  • Exactly what occurred, together with any error messages or associated log messages
  • Exactly what you have been anticipating to occur
  • What you will have executed to date to seek out the issue and any clues that you’ve found whereas troubleshooting

Sometimes it is a lot of knowledge, however belief me, it’s actually annoying making an attempt to pry data out of somebody piecemeal. Try to be concise, however full.

7. Ask for assist

I usually discover that simply writing down all the data triggers a considered one thing I’ve not tried but. Sometimes, after all, I notice what the issue is instantly after hitting the submit button. At any price, if you happen to nonetheless haven’t considered something after writing every thing down, strive sending an electronic mail to somebody. First, strive colleagues or different folks concerned in your venture, then transfer on to venture electronic mail lists. Don’t be afraid to ask for assist. Most individuals are sort and useful, and I’ve discovered that to be very true within the Python neighborhood. 


Maria McKinley will current Hunting the Bugs at PyCascades 2019, February 23-24 in Seattle.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular

breakingExpress.com features the latest multimedia technologies, from live video streaming to audio packages to searchable archives of news features and background information. The site is updated continuously throughout the day.

Copyright © 2017 Breaking Express, Green Media Corporation

To Top