Login

Dynamically Import a Module (and return it)

Author:
joshua
Posted:
February 28, 2007
Language:
Python
Version:
Pre .96
Tags:
dynamic module
Score:
3 (after 3 ratings)

foo = dynamic_import ( 'rawr.i.am.a.lion' )

Will import lion from rawr.i.am.a and return it. (This isn't really Django specific)

Props to Crast for the original.

1
2
3
4
5
def dynamic_import( path ):
    l = path.rfind('.')
    parent, child = path[:l], path[l+1:]
    base = __import__(parent, globals(), globals(), [child])
    return getattr(base, child, None)

More like this

  1. Dynamic import from an installed app by Archatas 6 years, 1 month ago
  2. module_from_path by simon 7 years ago
  3. Dynamic Template Loader by jgeewax 6 years, 11 months ago
  4. Lazily lookup dynamically for templates by axiak 7 years, 2 months ago
  5. Dynamic Backends by LorenDavie 6 years, 8 months ago

Comments

wiz (on March 1, 2007):

Looks great for plugins!

#

Please login first before commenting.