How to get a list of all the Python standard library modules?
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 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
.
So ultimately, I want to know how to ignore all the standard library modules when using trace
or sys.settrace
.
Solution 1:
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']
Solution 2:
Why not work out what's part of the standard library yourself?
import distutils.sysconfig as sysconfig
import os
std_lib = sysconfig.get_python_lib(standard_lib=True)
for top, dirs, files in os.walk(std_lib):
for nm in files:
if nm != '__init__.py' and nm[-3:] == '.py':
print os.path.join(top, nm)[len(std_lib)+1:-3].replace(os.sep, '.')
gives
abc
aifc
antigravity
--- a bunch of other files ----
xml.parsers.expat
xml.sax.expatreader
xml.sax.handler
xml.sax.saxutils
xml.sax.xmlreader
xml.sax._exceptions
Edit: You'll probably want to add a check to avoid site-packages
if you need to avoid non-standard library modules.
Solution 3:
Take a look at this, https://docs.python.org/3/py-modindex.html They made an index page for the standard modules.