Finding Module Dependancies

John Machin sjmachin at lexicon.net
Fri Apr 21 19:59:15 EDT 2006


On 22/04/2006 8:18 AM, mwt wrote:
> When I'm rewriting code (cutting and pasting pieces from earlier
> modules)

Instead of propagating multiple copies of source code, consider 
refactoring those modules so that top-level functions and classes can be 
used in other modules.

> is there a quick way to determine if I have imported all the
> necessary modules? I am used to working in Java, where the compiler
> will tell you if you haven't imported everything,

That's a clever design. It would be even better if it just shut up and 
did the imports for you :-)

> and also Eclipse,
> which has the handy "organize imports" feature. This is not the case in
> Python, since it's not compiled, of course, and also running it might
> not insure you've got all the imports, unless you go through every
> possible usage scenario -- which in some cases is quite a few, and
> could take a long time.

This is called "testing". Yes, it could take a long time.

> 
> So what I'm looking for is a command, like "check dependencies" or
> something, which will list all the modules needed for a source module
> to run.
> 

Consider pychecker and pylint. I haven't tried pylint, but pychecker 
does what you want and a whole lot more:

C:\junk>type miss_import.py
# need to import re, but forgot
def my_isdigit(s):
     return bool(re.match(r"\d+", s))

C:\junk>pychecker miss_import

C:\junk>c:\python24\python.exe 
c:\python24\Lib\site-packages\pychecker\checker.py miss_import
Processing miss_import...

Warnings...

miss_import.py:3: No global (re) found

C:\junk>



More information about the Python-list mailing list