Introspect imports from module

Discussion in 'Python' started by Bastian Ballmann, Jun 21, 2012.

  1. 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/
    Bastian Ballmann, Jun 21, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davy
    Replies:
    1
    Views:
    943
    Steven D'Aprano
    Nov 22, 2007
  2. Albert
    Replies:
    4
    Views:
    10,860
    Albert
    Jul 10, 2008
  3. Gavin Kistner
    Replies:
    0
    Views:
    190
    Gavin Kistner
    Apr 4, 2005
  4. monojohnny
    Replies:
    3
    Views:
    78
    monojohnny
    Nov 4, 2008
  5. Bastian Ballmann

    Re: Introspect imports from module

    Bastian Ballmann, Jun 21, 2012, in forum: Python
    Replies:
    0
    Views:
    204
    Bastian Ballmann
    Jun 21, 2012
Loading...

Share This Page