views:

111

answers:

2

We're having a real problem with people checking in code that doesn't work because something's been refactored. Admittedly, this is partly because our developers don't really have any good tools for finding these kinds of mistakes easily.

Are there any tools to help find ImportErrors in Python? Of course, the correct answer here is "you should use your unit tests for that." But, I'm in legacy code land (at least by Michael Feathers's definition), so our unit tests are something we're working on heavily.

In the meantime, it would be nice to have some sort of tool that will walk through each directory and import each file within it just to find any scripts that have ImportErrors (like say if a file or class has been renamed recently). I suppose this wouldn't be terribly difficult to write myself, but are there any programs that are already written to do this?

+1  A: 

Something like this?

import os
for x in os.listdir("some/path"):
    execfile( x )

Is that enough, or would you need more?

Note that any module that lacks if __name__ == "__main__": will -- obviously -- take off and start doing stuff.

S.Lott
There's more that I would want to add. But I suppose that's a good enough start if I do decide to write my own. :-)
Jason Baker
+2  A: 

Pychecker is for you. It imports the modules and will find these errors.

http://pychecker.sourceforge.net/

Oh, and "pylint <modulename>" will import the module, but I guess you would have to call it once for every module you want, where pychecker at least supports *.py. (Pylint also support *.py but won't import the modules in that situation).

Lennart Regebro