creating a list of all imported modules

Timmie timmichelsen at gmx-topmail.de
Tue Mar 10 15:29:28 CET 2009


> 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?





More information about the Python-list mailing list