Created
September 15, 2015 10:52
-
-
Save carlos-jenkins/1319c878c448a9eff284 to your computer and use it in GitHub Desktop.
Find all submodules given a module root name
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_modules(name): | |
""" | |
Get a list of all submodules given a module root name. | |
""" | |
from pkgutil import walk_packages | |
module = __import__(name) | |
# Handle modules installed in top-level | |
if not hasattr(module, '__path__'): | |
return [(name, False)] | |
submodules = [(name, True)] | |
for _, name, ispkg in walk_packages( | |
path=module.__path__, prefix=module.__name__ + '.'): | |
submodules.append((name, ispkg)) | |
return submodules |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment