Reputation: 3336
How can I access the list of modules that Python's help('modules')
displays? It shows the following:
>>> help('modules')
Please wait a moment while I gather a list of all available modules...
...[list of modules]...
MySQLdb codeop mailman_sf spwd
OpenSSL collections markupbase sqlite3
Queue colorsys marshal sre
...[list of modules]...
Enter any module name to get more help. Or, type "modules spam" to search
for modules whose descriptions contain the word "spam".
>>>
I can view the list in the output but would like to access this as a list from within a Python program. How can I do this?
Upvotes: 5
Views: 1654
Reputation: 3188
Those will only list modules not included in the standard library, but may be useful,
subprocess.call( pip freeze)
subprocess.call( yolk -l)
Upvotes: 1
Reputation: 120698
The list of modules comes ultimately from a combination of sys.builtin_module_names and the output of pkgutil.walk_packages:
import sys
from pkgutil import walk_packages
modules = set()
def callback(name, modules=modules):
if name.endswith('.__init__'):
name = name[:-9] + ' (package)'
if name.find('.') < 0:
modules.add(name)
for name in sys.builtin_module_names:
if name != '__main__':
callback(name)
for item in walk_packages(onerror=callback):
callback(item[1])
for name in sorted(modules, key=lambda n: n.lower()):
print name
It should be noted that creating the list has the consequnce that all the modules will be imported (you can easily verify this for yourself by checking the length of sys.modules
before and after calling help('modules')
).
Another thing to note is that the output of walk_packages
depends on the current state of sys.path
- so the results may not always match the output of help
.
Upvotes: 1
Reputation: 29737
You can mimic all the help
does by yourself. Built-in help
uses pydoc
, that uses ModuleScanner
class to get information about all available libs - see line 1873 in pydoc.py.
Here is a bit modified version of code from the link:
>>> modules = []
>>> def callback(path, modname, desc, modules=modules):
if modname and modname[-9:] == '.__init__':
modname = modname[:-9] + ' (package)'
if modname.find('.') < 0:
modules.append(modname)
>>> def onerror(modname):
callback(None, modname, None)
>>> from pydoc import ModuleScanner
>>> ModuleScanner().run(callback, onerror=onerror)
>>> len(modules)
379
>>> modules[:10]
['__builtin__', '_ast', '_bisect', '_codecs', '_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw']
>>> len(modules)
379
Upvotes: 4
Reputation: 11624
There are more then one way. You could try:
import sys
mod_dict = sys.modules
for k,v in mod_dict.iteritems():
print k,v
Upvotes: 2