Skip to content

Instantly share code, notes, and snippets.

@lad1337
Forked from mdomke/globimport.py
Created March 23, 2018 08:43
Show Gist options
  • Save lad1337/ce7f3c132f563ff9bba231f0a8243d29 to your computer and use it in GitHub Desktop.
Save lad1337/ce7f3c132f563ff9bba231f0a8243d29 to your computer and use it in GitHub Desktop.
Python glob import
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