Analyse python project imports
<p>In general I would like to understand what exactly code the code in my projects is actually using from a big framework.</p>

<p>First I want to know what are all the imports (possibly with static analysis), and then if possible which of these imports are actually used.</p>

<p>For the first problem I could use a regexp of course, but I would like to find a cleaner way.
but I don't see how with ast/inspect/parser.</p>

<p>And about the second problem I should be able to find out automatically if some of the imports are actually unused, but how can I do that?</p>

<p>EDIT:
about the second issue maybe the best way is a simple import hook, which just records everything it was imported and then call the default importing mechanisms.</p>

<p>So I tried something like:</p>

<pre><code>class MyLoader(object):
"""
Loader object
"""

def __init__(self):
    self.loaded = set()

def find_module(self, module_name, package=None):
    print("requesting %s" % module_name)
    self.loaded.add(module_name)
    return self

def load_module(self, fullname):
    fp, pathname, stuff = imp.find_module(fullname)
    imp.load_module(fullname, fp, pathname, stuff)
</code></pre>

<p>But trying to import "random" I get
    from <strong>future</strong> import division
ImportError: No module named <strong>future</strong></p>

<p>Which I think means I'm missing something..
I haven't found any simple example of using imp to do some import introspection, any hints?</p>
