You cannot rely on the introspection of loaded modules because subpackages may not have been loaded. You will need to look at the file system, assuming that the top-level package in question is not an egg, zip file, extension module, or loaded from memory.
def get_subpackages(module): dir = os.path.dirname(module.__file__) def is_package(d): d = os.path.join(dir, d) return os.path.isdir(d) and glob.glob(os.path.join(d, '__init__.py*')) return filter(is_package, os.listdir(dir))
James Emerton
source share