how do I create a list of all modules imported by my module/script and
which are present in the namespace?
I am looking for something like %who in Ipython.
My aim is to create a file for the documentation that shows all
dependencies of my script on external (3rd party) libraries.
Thanks for your help in advance.
Regards,
Timmie
http://docs.python.org/library/modulefinder.html
Or:
you can iterate through the output of dir() (this is a little shell-
ish):
# -bash 3 > python
Python 2.5.2 (r252:60911, Jun 30 2008, 09:13:23)
[GCC 3.4.6] on irix6-64
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> import re
>>> for i in dir() :
... t = eval( 'type(' + i + ')' )
... if re.match('.*module.*',str(t)) : print i, t
...
__builtins__ <type 'module'>
numpy <type 'module'>
re <type 'module'>
>>>
>>>> for i in dir() :
> ... t = eval( 'type(' + i + ')' )
> ... if re.match('.*module.*',str(t)) : print i, t
> ...
If you think you need eval stop and think again. You don't.
If you think you need regular expressions stop and think again. You usually
don't.
>>> def imported(namespace=None):
from types import ModuleType
if namespace is None:
namespace = globals()
for n, v in namespace.iteritems():
if isinstance(v, ModuleType):
print n
>>> imported()
re
__builtins__
sys
os
types
--
Duncan Booth http://kupuguy.blogspot.com
To sort out which imported modules are supplied by Python, by you, and
by 3rd parties it would help a lot if you knew where the modules are
being loaded from.
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.
HTH,
John
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?
>> 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...
It is imported somewhere; look for site.py, sitecustomize.py,
PYTHONSTARTUP variable, .pth files...
--
Gabriel Genellina
"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.