creating a list of all imported modules

mataap at gmail.com mataap at gmail.com
Mon Mar 9 22:04:58 EDT 2009


On Mar 10, 10:01 am, Tim Michelsen <timmichel... at gmx-topmail.de>
wrote:
> Hello,
>
> 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'>
>>>



More information about the Python-list mailing list