获取标准库模块列表?

发布于 2021-01-29 15:05:21

我想要标准库中所有模块的列表。

至于关键字,我通过以下方法来获取它们:

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']

对于内置函数:

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

如何将相同的操作应用于Python官方文档中的其他标准库名称。

# my desired result is this
['colletions', 'datetime', 'os', ... ]
# So will check them for reference anytime and anywhere.
关注者
0
被浏览
90
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    不幸的是,没有stdlib方法来获取stdlib列表。但这有一个第三方模块pip install stdlib_list接着:

    >>> 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']
    

    它通过抓取Python的Sphinx文档来工作,因此非常可靠。请注意,对于不同版本的Python,标准库的内容正在更改,因此您可以在使用此功能时指定Python版本。如果未指定,则默认使用当前的解释器版本。



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看