creating a list of all imported modules

T

Tim Michelsen

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
 
M

mataap

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..... t = eval( 'type(' + i + ')' )
.... if re.match('.*module.*',str(t)) : print i, t
....
__builtins__ <type 'module'>
 
J

John Machin

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
 
T

Timmie

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

Gabriel Genellina

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

John Machin

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?

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top