Acquire a list of standard library modules?

2020-04-14 16:33发布

I want a list of all modules in the standard library.

As to the keywords, I grab them by:

import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

For builtins:

>>> dir(__builtins__)
['ArithmeticError', ..., 'super', 'tuple', 'type', 'vars', 'zip']

How to apply the same operation to other standard library names found in Python official documentation.

# my desired result is this
['colletions', 'datetime', 'os', ... ]
# So will check them for reference anytime and anywhere.

标签: python
1条回答
▲ chillily
2楼-- · 2020-04-14 17:30

Unfortunately, there is no stdlib way to get the stdlib list. But there's a 3rd-party module for this. pip install stdlib_list and then:

>>> from stdlib_list import stdlib_list
>>> libs = stdlib_list()
>>> libs[-10:]
['xml.sax',
 'xml.sax.handler',
 'xml.sax.saxutils',
 'xml.sax.xmlreader',
 'xmlrpc.client',
 'xmlrpc.server',
 'zipapp',
 'zipfile',
 'zipimport',
 'zlib']

It works by scraping the Sphinx docs of Python, so that's pretty reliable. Note that the standard library contents is changing for different releases of Python, so you could specify the Python version when using this function. If unspecified, it defaults to using the current interpreter version.

查看更多
登录 后发表回答