views:

2128

answers:

13

I'm getting quite a few errors in my code. Consequently, I would like to be able to minimize them at the outset or see as many errors as possible before code execution. Is this possible and if so, how can I do this?

+7  A: 

Python provides a debugger which allows you to step through your code, inspect variables and manipulate them. Refer to http://www.ferg.org/papers/debugging_in_python.html which can take you over the steps...

Also check the python standard library reference for pdb http://www.python.org/doc/2.5.2/lib/module-pdb.html

--Amit

Amit
+5  A: 

Test early and test often.

This doesn't necessarily mean to jump into the test driven design pool head first (though that's not such a bad idea). It just means, test your objects and methods as soon as you have something that works. Don't wait until you have a huge pile of code before doing testing.

Invest some time in learning a testing framework. If it's trivial for you to type in a test case you'll more likely do it. If you don't have any sort of framework testing can be a pain so you'll avoid it. So, establish some good habits early on and you'll have fewer problems down the road.

Bryan Oakley
+1  A: 

python -m pdb yourcode.py should do it. Alternatively you can "import pdb" in your code and use pdb.set_trace() to set break points. Refer the manual for more info: http://www.python.org/doc/2.5.2/lib/module-pdb.html

Sridhar Iyer
+3  A: 

Using assert statement liberally.

+13  A: 

If you're having problems with syntax, you could try an editor with syntax highlighting. Until you get the feel for a language, simple errors won't just pop out at you.

The simplest form of debugging is just to insert some print statements. A more advanced (and extensible) form of this would be to use the logging module form the std lib.

If actually want to step through your code, the python debugger is called pdb, which can be called from the command line, or embedded in your code.

If you're used to a fully integrated IDE, I would recommend using Eclipse with pydev.

JimB
What about PyLint?
detly
+10  A: 

All the really cool stuff is easily demonstrated in the interactive interpreter. I think this might be the "gold standard" for good design:

Can you exercise your class interactively?

If you can do stuff interactively, then you can write unittests and doctests with confidence that it's testable, simple, reliable.

And, more important, you can explore it interactively. There's nothing better than the instant gratification that comes from typing code and seeing exactly what happens.

The compiled language habits (write a bunch of stuff, debug a bunch of stuff, test a bunch of stuff) can be left behind. Instead, you can write a little bit of stuff, explore it, write a formal test and then attach your little bit of stuff to your growing final project.

You still do overall design. But you don't squander time writing code that may or may not work. In Python you write code that works. If you're not sure, you play with it interactively until you're sure. Then you write code that works.

S.Lott
That's a lot of stuff.
e-satis
@e-satis: too much stuff. Write test stuff. Test less stuff. Do less stuff. Get to the beer part of the day with a better attitude.
S.Lott
+3  A: 

Identifing errors before execution is the domain of static checking/analysis. I've had good luck using PyChecker for basic static checking of Python code.

The pycheesecake site has a very good summary of testing tools for Python.

Andrew Beyer
+10  A: 

Here is some techniques to facilitate debugging in Python:

  • use interactive shell e.g., ipython. Python is a dynamic language you can explore your code as you type. The shell is running in the second window in my editor at all times.

  • copy-paste from the shell into docstrings a code that illustrates a dominant usage and corner cases of the function (class, module). doctest.testmod() placed in a if __name__=="__main__" section allows to test all docstrings in the module. doctest can be easily integrated with unittest.

  • use assert for stuff that can never happen.

  • print() can solve many debugging problems; logging module is suitable for long-living python processes.

  • write tests (not necessarily before your code), run them often (automatically or with one keystroke at most); nose provides extended test discovery and running features for unittest.

  • run pylint occasionally (though it is treaky to install on non-debian systems).

At this point there is a little use for a formal python debugger. Winpdb is an external multi-platform GPL'ed GUI python debugger if you need one.

J.F. Sebastian
+2  A: 

I set up Python to automatically start the debugger when there's an uncaught exception, using this trick. That way, you can easily examine the state of the program without too much logging code. (Plus, to send me a Growl notification.)

Oh, and this way you can just create a break point in the code by adding

if answer == 42:
    1/0
biocs
Using 'raise Exception' or 'raise Exception("blah should not be 42, it is %s" % (blah))' would be a bit more Python'y..
dbr
I agree :) but 1/0 does the same in just three keystrokes.
biocs
+4  A: 

More often than not, I just use a bunch of print statements.

page = grabpage(url)
print "Page content:", page
print "page type():", type(page)

It's sometimes useful to do something like:

debug = True
if debug: print "page content", page

..with this you can quickly disable all your debugging print statements by changing the debug variable to False.

While print-debugging will get you very far in most cases, sometimes it's difficult to debug things like loops, or a series of if/else/try/except/etc. For this a debugger that allows stepping though your code, and setting break-points is useful.

pdb is included with Python. Here is a good simple tutorial on it. You can even do things like changing variables during run-time (which the tutorial covers). A longer tutorial can be found here

There is a very nice GUI equivalent pdb - Winpdb

Basically you run winpdb myscript --arg 4 -b 4 then it loads the command in a terminal, shows you your code on the left, with the current, a list of local/global variables (and their values) and the current call-stack.

Then you can step though the code by clicking the Step (or F6). F5 runs the code. If you click next to the line numbers, it sets a break point, where the code will automatically step (when you press run).

I find it far easier to use, and it has lots of addition enhancements (like remote debugging, so you can run the backend portion (rpdb2) in the to-be-debugged application, and connect Winpdb to it (encrypted). It also has support for threading and other useful things not in PDB. You have access to a pdb-like console too.

dbr
Good info about Winpdb, thx
hopla
+2  A: 

The PyDev plugin for eclipse is my tool of choice. It recognizes simple syntax mistakes and indentation errors and underlines the error with a red line. It has a powerful debugger and even has a plugin called PyLint which warns you about dangerous code.

Edit: It also has a user friendly stack trace on runtime errors, partial auto complete and some other nifty features.

Edit again: I didn't notice that pydev was mentioned in the top post. I hope I brought something else to the table.

Nailer
+1  A: 

There is very nice GUI debugger for Python called Winpdb. Try it out.Built on wxWidgets library and multiplatform.

RobertVa
A: 

Eric4 IDE also has a great built-in debugger.

fccoelho