Skip to content

Instantly share code, notes, and snippets.

@justinfay
Last active December 28, 2015 14:49
Show Gist options
  • Save justinfay/7517584 to your computer and use it in GitHub Desktop.
Save justinfay/7517584 to your computer and use it in GitHub Desktop.
import importlib
IMPORT_CACHE = {}
def cache_import(path):
"""Import an object from a specified `path`."""
mod, imp = path.rsplit('.', 1)
if mod in IMPORT_CACHE:
return getattr(IMPORT_CACHE[mod], imp)
mod_ = importlib.import_module(mod)
IMPORT_CACHE[mod] = mod_
return cache_import(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment