Skip to content

Instantly share code, notes, and snippets.

@jdunck
Created April 23, 2015 17:44
Show Gist options
  • Save jdunck/d899e2de218a85f154cc to your computer and use it in GitHub Desktop.
Save jdunck/d899e2de218a85f154cc to your computer and use it in GitHub Desktop.
List all stdlib packages
def get_stdlib_names():
import distutils.sysconfig as sysconfig
import os
import sys
std_lib = sysconfig.get_python_lib(standard_lib=True)
pathed_dirs = sorted(sys.path[:], key=lambda s: len(s), reverse=True)
prefixes = filter(lambda path: path.startswith(std_lib), pathed_dirs)
for top, dirs, files in os.walk(std_lib):
for maybe_prefix in prefixes:
if top.startswith(maybe_prefix):
prefix = top[len(maybe_prefix)+1:]
break
for nm in files:
if prefix[:13] == 'site-packages':
continue
if nm[-3:] == '.so' and top[-11:] == 'lib-dynload':
yield nm[0:-3]
elif nm == '__init__.py':
yield top[len(std_lib)+1:].replace(os.path.sep,'.')
elif nm[-3:] == '.py':
yield os.path.join(prefix, nm)[:-3].replace(os.path.sep,'.')
for builtin in sys.builtin_module_names:
yield builtin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment