9

How do I get a list of all python modules available?

I don't want modules of modules. Just a basic list of all modules available in sys.path.

help('modules') is not the solution, because I want it available as a variable and it imports those modules, which has side effects.

Edit: With side effects I mean libraries like kivy of http://kivy.org/, which make use of the fact, that code is executed once you import it.

Dave Halter
  • 15,556
  • 13
  • 76
  • 103
  • 1
    I don't see how this is a duplicate question, since `pkgutil` wasn't even mentioned in the other thread and there's a clear limitation here: **No side effects!** – Dave Halter Dec 19 '15 at 13:21

2 Answers2

8

pkgutil - Utilities to support packages

this will yield a tuple for all submodules on sys.path:

pkgutil.iter_modules()

to see what's loaded, look at:

sys.modules

"This is a dictionary that maps module names to modules which have already been loaded"

a list of loaded modules:

sys.modules.keys() 
jsbueno
  • 99,910
  • 10
  • 151
  • 209
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
5

Use the external script "pydoc" that comes along with a Python install: from the command shell, type:

$ pydoc modules

Pydoc can be used from within Python as well, one way of having it walk everything available is:

all_mod = []
pydoc.ModuleScanner().run(callback=(lambda *a: all_mod.append(a[1])), onerror=lambda *a:None)
print all_mod
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • A good idea, but it imports those modules (can have side effects if you have things like `kivy` installed. – Dave Halter Mar 30 '12 at 19:18
  • What pydoc does is manually check for all available files and then import them - if you don't want the import part, I think you will have to look at pydoc code and copy from it to see all available modules (including those in zip files and eggs). – jsbueno Mar 30 '12 at 20:27