How to list imported modules?

Posted on

Question :

How to list imported modules?

How to enumerate all imported modules?

E.g. I would like to get ['os', 'sys'] from this code:

import os
import sys
Asked By: alex2k8


Answer #1:

import sys

An approximation of getting all imports for the current module only would be to inspect globals() for modules:

import types
def imports():
    for name, val in globals().items():
        if isinstance(val, types.ModuleType):
            yield val.__name__

This won’t return local imports, or non-module imports like from x import y. Note that this returns val.__name__ so you get the original module name if you used import module as alias; yield name instead if you want the alias.

Answered By: Glenn Maynard

Answer #2:

Find the intersection of sys.modules with globals:

import sys
modulenames = set(sys.modules) & set(globals())
allmodules = [sys.modules[name] for name in modulenames]
Answered By: Marcin

Answer #3:

If you want to do this from outside the script:

Python 2

from modulefinder import ModuleFinder
finder = ModuleFinder()
for name, mod in finder.modules.iteritems():
    print name

Python 3

from modulefinder import ModuleFinder
finder = ModuleFinder()
for name, mod in finder.modules.items():

This will print all modules loaded by

Answered By: Lila

Answer #4:

print [key for key in locals().keys()
       if isinstance(locals()[key], type(sys)) and not key.startswith('__')]
Answered By: Mike Axiak

Answer #5:

let say you’ve imported math and re:

>>import math,re

now to see the same use


If you run it before the import and after the import, one can see the difference.

Answered By: mukundha reddy

Answer #6:

It’s actually working quite good with:

import sys
mods = [m.__name__ for m in sys.modules.values() if m]

This will create a list with importable module names.

Answered By: fabiand

Answer #7:

This code lists modules imported by your module:

import sys
before = [str(m) for m in sys.modules]
import my_module
after = [str(m) for m in sys.modules]
print [m for m in after if not m in before]

It should be useful if you want to know what external modules to install on a new system to run your code, without the need to try again and again.

It won’t list the sys module or modules imported from it.

Answered By: Ohad Cohen

Answer #8:

Stealing from @Lila (couldn’t make a comment because of no formatting), this shows the module’s /path/, as well:

#!/usr/bin/env python
import sys
from modulefinder import ModuleFinder
finder = ModuleFinder()
# Pass the name of the python file of interest
# This is what's different from @Lila's script

which produces:

Name                      File
----                      ----

m token                     /opt/rh/rh-python35/root/usr/lib64/python3.5/
m tokenize                  /opt/rh/rh-python35/root/usr/lib64/python3.5/
m traceback                 /opt/rh/rh-python35/root/usr/lib64/python3.5/

.. suitable for grepping or what have you. Be warned, it’s long!

Answered By: JDonner

Leave a Reply

Your email address will not be published.