[prog] Getting all modules used in a Python script

cafeine cafeine at no-log.org
Wed May 11 03:07:28 EST 2005


Yaroslav Fedevych <jaroslaw at linux.org.ua> writes:

> Hello,
>
> are there any means in Python for finding out all modules used by a 
> certain script and all modules it uses? Too bad if it requires 
> 'try and error' approach...
>
> In other words, something like ldd(1) but for Python would be
> perfectly okay.
>
> Thanks.

the sys module might be what you're looking for.

excerpt from an ipython session:

In [1]:import pygame
In [2]:import sys 
In [3]:sys.modules

Out[3]:
{'ArrayPrinter': <module 'ArrayPrinter' from '/usr/lib/python2.3/site-packages/Numeric/ArrayPrinter.pyc'>,
 'IPython': <module 'IPython' from '/usr/lib/python2.3/site-packages/IPython/__init__.pyc'>,
 'IPython.ColorANSI': <module 'IPython.ColorANSI' from '/usr/lib/python2.3/site-packages/IPython/ColorANSI.pyc'>,
[SNIP]

you get a dict of imported modules (as well as built-in ones, though
it's easy to filter them out)

HTH

-- 
"Men get defensive when women critique their oppressive and sexist
behaviors. Rather than listening and benefiting from criticism, a
defensive stance is taken and women's voices are ignored once again."
-Angela Beallor



More information about the Programming mailing list