Skip to content

Instantly share code, notes, and snippets.

@struys
Created June 20, 2014 02:17
Show Gist options
  • Save struys/3077f8a2a6c1c3cef096 to your computer and use it in GitHub Desktop.
Save struys/3077f8a2a6c1c3cef096 to your computer and use it in GitHub Desktop.
class _ImporterProxy(object):
def __init__(self, mod):
self.__mod = mod
def __getattr__(self, meth):
try:
ret = getattr(self.__mod, meth)
except AttributeError:
ret = _ImporterProxy(__import__(self.__mod.__name__ + '.' + meth, fromlist=['__trash']))
self.__dict__[meth] = ret
return ret
def __repr__(self):
return repr(self.__mod)
class Importer(object):
def __getattr__(self, meth):
ret = _ImporterProxy(__import__(meth))
self.__dict__[meth] = ret
return ret
i = Importer()
if __name__ != '__main__':
import sys
self = sys.modules[__name__]
i.__self__ = self
sys.modules[__name__] = i
import importer as i
i.whatever_you_want
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment