creating a list of all imported modules

John Machin sjmachin at lexicon.net
Tue Mar 10 18:02:18 EDT 2009


On Mar 11, 1:29 am, Timmie <timmichel... at gmx-topmail.de> wrote:
> > Put this code at the end of your script:
> >     import sys
> >     info = [(module.__file__, name)
> >         for (name, module) in sys.modules.iteritems()
> >         if hasattr(module, '__file__')]
> >     info.sort()
> >     import pprint
> >     pprint.pprint(info)
>
> > AFAIK unless someone has been messing with sys.modules, this gives you
> > all modules that have been imported during the running of your script,
> > except for built-ins (no __file__ attribute). Warning: I've not tried
> > this with eggs and zips.
>
> Thanks, exactly what I was lokking for:
>
> I added this:
> additional_mods = []
> for i in info:
>     module_path = i[0]
>     substr = '\site-packages'
>     if module_path.find(substr) != -1:
>         additional_mods.append(module_path)
>
> pprint.pprint(additional_mods)
>
> Unfortunately, it does include more than the actually included module.
> Eg.
> if I only import pytz
> the list also shows:
> pastescript-1.7.3-py2.5.egg\\paste\\__init__.pyc',
> pbp.scripts-0.2.5-py2.5.egg\\pbp\\__init__.pyc',
> pytz-2009a-py2.5.egg\\pytz\\__init__.py',
> pytz-2009a-py2.5.egg\\pytz\\tzfile.py',
> pytz-2009a-py2.5.egg\\pytz\\tzinfo.py',
> rst2pdf-0.9-py2.5.egg\\rst2pdf\\__init__.pyc',
> traitsgui-3.0.3-py2.5.egg\\enthought\\__init__.pyc',
>
> I am sure that pytz does not need pastescript to work...
>
> Any idea?

"has imported" != "needed to import" ... I've see a module that
imported Tkinter and never used it.

BTW, what gives you the surety that third-party modules can only be
imported from a path that includes "site packages"? What gives you the
surety that you need to import all modules whose path does not include
"site-packages"? Perhaps you should scrutinise *all* of the loaded-
from-file entries in sys.modules.



More information about the Python-list mailing list