Dynamically Importing Python Modules

If you ever need to dynamically import Python modules this short function will do the trick. The modules will be imported into the importing modules __main__.

def importMods(path, fromList, asList=None):
    modules = __import__(path, globals(), locals(), fromList, -1)
    if not asList: asList = fromList
    if len(asList) != len(fromList): raise ValueError('asList != fromList')

    for asMod, mod in zip(asList, fromList):
        exec "importMods.func_globals['%s'] = modules.%s" % (asMod, mod)