This is similar to what you want:
def import_path(name):
modname, _, attr = name.rpartition('.')
if not modname:
return __import__(attr)
m = __import__(modname, fromlist=[attr])
return getattr(m, attr)
To make it work with Python 2.5 and earlier, where it __import__does not accept keyword arguments, you will need to use:
m = __import__(modname, {}, globals(), [attr])
source
share