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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With