Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

creating a list of all imported modules

4 views
Skip to first unread message

Tim Michelsen

unread,
Mar 9, 2009, 8:01:10 PM3/9/09
to pytho...@python.org
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

mat...@gmail.com

unread,
Mar 9, 2009, 10:04:58 PM3/9/09
to
On Mar 10, 10:01 am, Tim Michelsen <timmichel...@gmx-topmail.de>
wrote:

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

Duncan Booth

unread,
Mar 10, 2009, 4:56:47 AM3/10/09
to
mat...@gmail.com wrote:

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

John Machin

unread,
Mar 10, 2009, 6:43:08 AM3/10/09
to
On Mar 10, 11:01 am, Tim Michelsen <timmichel...@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.

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

Timmie

unread,
Mar 10, 2009, 10:29:28 AM3/10/09
to pytho...@python.org
> 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?


Gabriel Genellina

unread,
Mar 10, 2009, 10:50:39 AM3/10/09
to pytho...@python.org
En Tue, 10 Mar 2009 12:29:28 -0200, Timmie <timmic...@gmx-topmail.de>
escribió:

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

John Machin

unread,
Mar 10, 2009, 6:02:18 PM3/10/09
to

"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.

0 new messages