Loaded module list and attributes

Steve Holden sholden at holdenweb.com
Mon Aug 6 13:16:37 EDT 2001


"Dale Strickland-Clark" <dale at riverhall.NOSPAMco.uk> wrote in message
news:o2mamt4qtis8r77maqnsmjg7h193aa80t4 at 4ax.com...
> How do I itterate through all the loaded modules and find the name of the
module and a value of a
> local attribute (__version__ in this case)?
>
> I'm writing a small debugging routine and Python internals are still a bit
of a black box to me.
>
> Thanks.
> --

You need to be a little careful, as the modules table contains None for some
modules and so won't have the attribute you are looking for. With that
caveat, try something along the lines of this, which prints the length of
each module's directory:

>>> for n, md in sys.modules.items():
...        if md:
...                print n, len(dir(md))

Obviously you can use hasattr() to check whether the particular module has
the attribute you seek.

regards
 Steve
--
http://www.holdenweb.com/





More information about the Python-list mailing list