Getting a list of all modules
steve at pearwood.info
Wed Jul 30 09:43:10 CEST 2014
I'm looking for a programmatic way to get a list of all Python modules
and packages. Not just those already imported, but all those which
*could* be imported.
I have a quick-and-dirty function which half does the job:
extensions = ('.py', '.pyc', '.pyo', '.so', '.dll')
matches = set()
for location in sys.path:
if location == '': location = '.'
for name in os.listdir(location):
base, ext = os.path.splitext(name)
if ext in extensions:
but I know it's wrong (it doesn't handle packages correctly, or zip
files, doesn't follow .pth files, has a very naive understanding of cross-
platform issues, fails to include built-in modules that don't live in the
file system, and probably more).
Is this problem already solved? Can anyone make any suggestions?
More information about the Python-list