Skip to main content
 首页 » 编程设计

python之获取标准库模块列表

2024年10月01日9lidabo

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

关于关键词,我是这样抓取的:

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. 

请您参考如下方法:

不幸的是,没有 stdlib 方法来获取 stdlib 列表。但是有一个 3rd-party module为了这。 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 版本。如果未指定,则默认使用当前解释器版本。