Last active
April 4, 2018 15:25
-
-
Save mdomke/508f2359177d578021aefe0297010b8a to your computer and use it in GitHub Desktop.
Python glob import
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
import importlib | |
import pkgutil | |
def globimport(pattern): | |
base, found, rest = pattern.partition('*') | |
try: | |
module = importlib.import_module(base.rstrip('.')) | |
except ImportError: | |
pass | |
if not (found or rest): | |
return | |
for _importer, name, ispkg in pkgutil.walk_packages(module.__path__): | |
if bool(rest) ^ ispkg: | |
continue | |
globimport(base + name + rest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment