-
-
Save lad1337/ce7f3c132f563ff9bba231f0a8243d29 to your computer and use it in GitHub Desktop.
Python glob import
This file contains 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('*') | |
if not (found or rest): | |
try: | |
importlib.import_module(base.rstrip('.')) | |
except ImportError: | |
pass | |
return | |
base_path = base.replace('.', '/') | |
for _importer, name, ispkg in pkgutil.walk_packages([base_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