Introspect imports from module

Bastian Ballmann bastian.ballmann at notch-interactive.com
Thu Jun 21 04:03:35 EDT 2012


Hi all,

I am trying to write a function that returns a list of imports a given
module is doing. The "problem" is I dont want to get the imports of the
imports, but that's the case with my current solution.

import __builtin__
old_import = __builtin__.__import__

def import_hook(name, globals=None, locals=None, fromlist=None):
    if fromlist:
        for symbol in fromlist:
            print name + "." + symbol
    else:
        print name

    return old_import(name, globals, locals, fromlist)

__builtin__.__import__ = import_hook
import module.to.inspect

Any suggestions how I could just get the import of module.to.inspect?
Thanks && have a nice day!

Basti
-- 
Bastian Ballmann / Web Developer
Notch Interactive GmbH / Badenerstrasse 571 / 8048 Zürich
Phone Phone +41 44 297 17 17 / www.notch-interactive.com

CHECK OUR LATEST WORK: http://www.notch-interactive.com/projekte/



More information about the Python-list mailing list