tags:

views:

153

answers:

4

I remember when I was developing in C++ or Java, the compiler usually complains for unused methods, functions or imports. In my Django project, I have a bunch of Python files which have gone through a number of iterations. Some of those files have a few lines of import statement at the top of the page and some of those imports are not used anymore. Is there a way to locate those unused imports besides eyeballing each one of them in each file?

All my imports are explicit, I don't usually write from blah import *

+3  A: 

Use a tool like pylint which will signal these code defects (among a lot of others).

Doing these kinds of 'pre-runtime' checks is hard in a language with dynamic typing, but pylint does a terrific job at catching these typos / leftovers from refactoring etc ...

ChristopheD
+4  A: 

PyFlakes (similar to lint) will give you this information.

doug
+2  A: 

Have a look at PyChecker. It is a debugging tool and able to find unused variables and modules.

Felix Kling
+1  A: 

If you use the eclipse IDE with pydev and mylyn, it provides automatic checking and highlighting for unused imports, among other things. It integrates with pylint as well.

Brendan Abel