Detecting imported objects
Question: Given the source code of a Python file, I would like to detect all imported
objects. For example, given this source:

    
    
    import mymod
    from mymod2 import obj1, obj2, obj3
    from mymod3 import aobj
    

I want to get:

    
    
    [('mymod2', 'obj1', 'obj2', 'obj3'), ('mymod3', 'aobj')]
    

I have already tried this regex:

    
    
    r'from (?P<mod>[_\w\d]+) import (?:(?P<obj>[_\w\d]+)[,\s]?)+'
    

But I only get the first imported object:

    
    
    [('mymod2', 'obj1'), ('mymod3', 'aobj')]
    


Answer: A better tool than regular expressions is the `ast` module that comes with
Python. To find all `from ... import` statements in the outermost scope of
`a.py` and print all imported names, you could use

    
    
    import ast
    code = open("a.py").read()
    for node in ast.parse(code).body:
        if isinstance(node, ast.ImportFrom):
            for name in node.names:
                print name.name
    

Note that this simple code will miss any statements that are not directly at
module level, such as import statements inside a try-block. This can easily be
fixed by using `ast.walk()` to walk over all nodes.

