How can I get a list of all the Python standard library modules

saltycrane picture saltycrane · Jun 24, 2011 · Viewed 12.9k times · Source

I want something like sys.builtin_module_names except for the standard library. Other things that didn't work:

  • sys.modules - only shows modules that have already been loaded
  • sys.prefix - a path that would include non-standard library modules EDIT: and doesn't seem to work inside a virtualenv.

The reason I want this list is so that I can pass it to the --ignore-module or --ignore-dir command line options of trace http://docs.python.org/library/trace.html

So ultimately, I want to know how to ignore all the standard library modules when using trace or sys.settrace.

EDIT: I want it to work inside a virtualenv. http://pypi.python.org/pypi/virtualenv

EDIT2: I want it to work for all environments (i.e. across operating systems, inside and outside of a virtualenv.)

Answer

user554546 picture user554546 · Mar 5, 2015

If anyone's still reading this in 2015, I came across the same issue, and didn't like any of the existing solutions. So, I brute forced it by writing some code to scrape the TOC of the Standard Library page in the official Python docs. I also built a simple API for getting a list of standard libraries (for Python version 2.6, 2.7, 3.2, 3.3, and 3.4).

The package is here, and its usage is fairly simple:

>>> from stdlib_list import stdlib_list
>>> libraries = stdlib_list("2.7")
>>> libraries[:10]
['AL', 'BaseHTTPServer', 'Bastion', 'CGIHTTPServer', 'ColorPicker', 'ConfigParser', 'Cookie', 'DEVICE', 'DocXMLRPCServer', 'EasyDialogs']