What are good ways to make my Python code run first time?

Question:

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?

Asked By: Amara

||

Answers:

Python provides a debugger which allows you to step through your code, inspect variables and manipulate them. Refer to http://pythonconquerstheuniverse.wordpress.com/category/python-debugger/ 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

Answered By: Amit

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.

Answered By: Bryan Oakley

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

Answered By: Sridhar Iyer

Using assert statement liberally.

Answered By: J S

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) way to do this would be to use the logging module from the std lib.

The interactive interpreter is a wonderful tool for working with python code, and IPython is a great improvement over the built-in REPL (Read Eval Print Loop).

If you 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, and PyCharm has a great commercial offering, with autocomplete, quick access to docs, and numerous shortcuts, among many other interesting features.

Answered By: JimB

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.

Answered By: S.Lott

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.

Answered By: Andrew Beyer

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.

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.

Answered By: jfs

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 
Answered By: Michael Kuhn

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.

Answered By: dbr

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.

Answered By: Nailer

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

Answered By: RobertVa

Eric4 IDE also has a great built-in debugger.

Answered By: fccoelho

The IPython Python shell has a %pdb command that automatically invokes the debugger in case of problem. You can then inspect variables, step through the code, etc.

You can run your programs from IPython with the %run command.

More generally, as as been noted in some other answers, a good Python shell is your friend. And IPython is your best friend. 🙂

Answered By: Eric O Lebigot

I am new to python, and have been trying several different debuggers. Here are the options I’ve come across so far:

  1. Eclipse with Pydev – If you’re already using eclipse, this is probably the way to go. The debugger works well, and is pretty featureful.
  2. Komodo IDE – A light-weight python IDE. Basically a text editor + a debugger. It was really easy to figure out and be productive with immediately.
  3. WinPDB – Trying this one next. Looks very featureful, and I get to use whichever editor I choose.
  4. PDB – Haven’t tried yet since I’ve read about how WinPDB is a better alternative.
  5. Ipython with %run command – I’ve used IPython, but not as a debugger like this. I need to try this out. (Thanks for the tip, EOL)
  6. Eric IDE – Also on the list to try.
  7. Old-school print, assert statements – Simple, useful, but not a full solution.

Memory debugging – To debug memory problems, I’ve come across a few tools:

  1. objgraph – Will generate png’s of reference graphs. Neat. There’s other functionality as well, like: import objgraph;print(objgraph.show_most_common_types(limit=10)) will print the top 10 most common types in memory.
  2. gc module – Interact directly with the garbage collector.
  3. heapy – Heap analyzer. For example: from guppy import hpy; hp = hpy(); print(hp.heap()) will print the most common types, their memory usage, etc.

This is a very incomplete list, but hopefully it’s a good start.

Answered By: scottm

For a most integrated experience you can use full blown IDE like PyCharm:

http://blog.jetbrains.com/pycharm/files/2011/05/template-debug2.pngenter image description here

Answered By: aaimnr
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.