How can I access the list of modules that Python help displays (โ€œmodulesโ€)?

How can I access the list of modules displayed by Python help('modules') ? 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 output list, but would like to access it as a list from a Python program. How can i do this?

+4
source share
4 answers

You can simulate all the help yourself. The built-in help uses pydoc , which uses the ModuleScanner class to get information about all available libs - see line 1873 in pydoc.py .

Here is a slightly modified version of the 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 
+4
source

There are more than one way. You can try:

 import sys mod_dict = sys.modules for k,v in mod_dict.iteritems(): print k,v 
+2
source

Those will only list modules not included in the standard library, but it may be useful,

subprocess.call ( pip freeze)

subprocess.call ( yolk -l)

+1
source

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 when creating the list there is an assumption that all modules will be imported (you can easily check this for yourself by checking the length of sys.modules before and after calling help('modules') ).

Another note: the output of walk_packages depends on the current state of sys.path - therefore, the results may not always correspond to the output of help .

+1
source

Source: https://habr.com/ru/post/1390293/


All Articles