Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

python - Acquire a list of standard library modules?

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...