Dynamically getting a list of standard library python packages names

Viewed 158

I'd like to get a list of names of all standard lib packages.

By this, I mean those listed on I thought of parsing https://docs.python.org/3/library/: strings name for which

__import__(name)

sys.builtin_module_names looked promising, but that's not it.

I thought of parsing ~/.pyenv/versions/3.8.6/lib/python3.8/ or https://docs.python.org/3/library/, but surely there's a better way!

Addendum

For those who need an X for a Y: I'm statically navigating the imports of packages to analyze them -- namely, to see what third party packages are used, what standard libs, with what frequency, etc.

1 Answers

I will post my solution here, but will wait to see if there's better before accepting it as the answer.

Install unbox and do this:

from unbox import builtin_module_names

This should give you a set a names for the python version of your environment (2.7 and 3.5-3.9 supported).

To get these, I parsed the list out of https://docs.python.org/{version}/library/ html pages and filtered out those that were not importable (from 3.8). You can verify that all the names are importable by doing:

for name in builtin_module_names:
    _ = importlib.import_module(name)

These names are contained in the package's data folder (as .csv files) and can be found here on github.

Note that you won't find all modules there -- only those that are (1) documented on said page and importable. For example, easter eggs such as this and antigravity won't be listed. You can find them in the larger scanned_standard_lib_names set, obtained by scanning local files:

from unbox import scanned_standard_lib_names
assert scanned_standard_lib_names.issuperset({'this', 'antigravity'})
Related