creating a list of all imported modules
sjmachin at lexicon.net
Tue Mar 10 23:02:18 CET 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
> substr = '\site-packages'
> if module_path.find(substr) != -1:
> Unfortunately, it does include more than the actually included module.
> if I only import pytz
> the list also shows:
> 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