Skip to content

Instantly share code, notes, and snippets.

@bellbind
Created July 26, 2010 14:33
Show Gist options
  • Save bellbind/490613 to your computer and use it in GitHub Desktop.
Save bellbind/490613 to your computer and use it in GitHub Desktop.
[python] __import__ by relative module style
def import_as(module_name, globals_):
"""__import__ for relative module style.
usage:
mod = import_as("..templates.jinja2", globals())
# it is same as: import ..templates.jinja2 as mod
"""
level = 0
for ch in module_name:
if ch != ".": break
level += 1
pass
return __import__(module_name[level:], globals_, locals(), ["*"], level)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment