Is there a standard way to list names of Python modules in a package? -


Any easy way to list the names of all modules without using __all___ is ?

For example, this package has been given:

  / testpkg /testpkg/__init__.py /testpkg/modulea.py/testpkg/moduleb.py  

I'm thinking that there is a standard or underlying way to do something like this:

  & gt; & Gt; & Gt; Package_contents ("testpkg") ['module', 'module']  

A manual approach will be to redo through the modules search path to find the package directory. One can list all the files in that directory, filter specific denominated py / pyc / pyo files, can strip the extension, and return that list, but it seems that the module import mechanism already Internally doing something is a fair job for some. Does this functionality expose anywhere?

Maybe it will work what you are looking for?

  import import os MODULE_EXTENSIONS = ('.py', '.pyc', '.pyo') def package_contents (package_name): file, pathname, description = imp.find_module (package_name) if File: raise ImportError ('package no:% r', package_name) # Use one set because some sources can both be compiled and if module.ws (MODULE_EXTENSIONS)])  
OS path.splitext (module) [0] set to module in oslistdir (pathname)

Comments