Live data from Hacker News

Debugging Python Like a Boss

zapier.com

61–70 of 100 posts

Re: Debugging Python Like a Boss

#61

Debuggers are cool and often necessary, but I disagree with this often-expressed sentiment that print-debugging is a primitive hack for people who don't know any better. Debugging is determining the point at which the program's expected behavior diverges from its actual behavior. You often don't know where where/when this is happening. Print-debugging can give you a transcript of the program's execution, which you ca…

I find logging/tracing (that can be enabled/disabled at run time) to be very valuable for debugging, both during development and in production. I blogged about it here: http://henrikwarne.com/2013/05/05/great-programmers-write-de...

Re: Debugging Python Like a Boss

#62
post #52

Debuggers are cool and often necessary, but I disagree with this often-expressed sentiment that print-debugging is a primitive hack for people who don't know any better. Debugging is determining the point at which the program's expected behavior diverges from its actual behavior. You often don't know where where/when this is happening. Print-debugging can give you a transcript of the program's execution, which you ca…

>Debuggers are cool and often necessary, but I disagree with this often-expressed sentiment that print-debugging is a primitive hack for people who don't know any better. Yes. >Stepping through a program's execution line-by-line and checking your assumptions can be a lot slower in some cases. Yes again, particularly when the code is in a loop. Whereas, if you use print-debugging, even though the output will be repeat…

Um most debuggers allow you set variables and execute a loop n times

Re: Debugging Python Like a Boss

#63
post #52

Earlier quoted context omitted.

>Debuggers are cool and often necessary, but I disagree with this often-expressed sentiment that print-debugging is a primitive hack for people who don't know any better. Yes. >Stepping through a program's execution line-by-line and checking your assumptions can be a lot slower in some cases. Yes again, particularly when the code is in a loop. Whereas, if you use print-debugging, even though the output will be repeat…

Um most debuggers allow you set variables and execute a loop n times

And what if your bug is after the nth iteration of the loop, where the exact value of n is unknown (it's a bug, after all)? How many times are you going to set the variable and run the loop n times? Whereas with print-debugging and a grep you can filter for only unexpected/unusual output, and so narrow down to the likely cause, faster. Generalizing, of course. There are definitely cases where a debugger can be more effective, as others in this thread have said. It depends on the case.

Re: Debugging Python Like a Boss

#64
post #42

The most frustrating thing (experienced in both Javascript and Python) is the "oh uncaught exception? let me just quit everything" model. Most of the time, if I were just given an interactive prompt right then, I could spend 1 minute looking at local variables, maybe get a special stack trace variable to look at that, then be over with it. Instead I have to stick in some print statements and start everything over aga…

There's a nice trick to enable this behavior for standard Python code run at the command line. Write the body of your code inside a main() function, then call it using the following toplevel block:

  if __name__ == "__main__":
      try:
          main()
      except KeyboardInterrupt: # allow ctrl-C
          raise
      except Exception as e:
          import sys, traceback, pdb
          print e
          type, value, tb = sys.exc_info()
          traceback.print_exc()
          pdb.post_mortem(tb)
This will catch any exceptions and throw you into PDB in the context where the exception was raised. You probably don't want to leave it in production code, but it's super useful for development.

Re: Debugging Python Like a Boss

#66

While in the subject of debugging Python you should look into Bugjar (gui) http://pybee.org/bugjar/ . Looks really good.

(BugJar core developer here) Thanks for the compliment. Bugjar is a work in progress; it's got some rough edges, and definitely some missing features -- but I'm excited for what it can become.

Re: Debugging Python Like a Boss

#67
One feature that all these tools share is that they're console based. This is nice, but there's a reason we all use graphical environments for our daily computing -- rich graphical user interfaces are a powerful tool for visualising complex data. However, you don't have to adopt a full IDE to get a graphical UI. Bugjar (http://pybee.org/bugjar) is a graphical debugger -- not an IDE, just a debugger. It uses Tkinter, so it's cross platform, and can be installed using "pip install bugjar".

It's an early stage project, but hopefully demonstrates that there is a middle ground between "Everything in an 80x25 text window" and "500lb IDE gorilla".

(Full disclosure: I'm the core developer of Bugjar)

Re: Debugging Python Like a Boss

#69
I would say that the real boss debugging starts with finding a way to make it first a unittest debugging.

Once the weird behavior is covered, usually print debugging is even better than pdb because it will encourage you to extend the test suite.

What I would like though is a special monkey patching in python that allow my to write print like "p this that" and will prettyprint this and that on stdout

Post reply on HN