License Public Domain
Lines 6
Keywords
import (3) __import__ (1) module (1) python (33)
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Know what you're getting – Unlike many sites, all our code is clearly licensed. Join Siafoo Now or Learn More

Import an arbitrarily named module Atom Feed 0

In Brief Just doing an __import__ only gets you the top level module (importing os.path would only get you os). Use this function to drill down.... more
# 's
1def import_module(name):
2 module = __import__(name)
3 if '.' in name:
4 for segment in name.split('.')[1:]:
5 module = getattr(module, segment)
6 return module

Just doing an __import__ only gets you the top level module (importing os.path would only get you os). Use this function to drill down.

Usage:

# 's
1>>> import_module('os.path')
2<module 'posixpath' from '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/posixpath.pyc'>