Differences between revisions 2 and 3
Revision 2 as of 2009-08-06 20:56:50
Size: 572
Editor: CarlNobile
Comment:
Revision 3 as of 2009-08-06 20:57:18
Size: 579
Editor: CarlNobile
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
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__}}}. If you ever need to dynamically import Python modules this short function will do the trick. The modules will be imported into the importing module's {{{__main__}}} scope.

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 module's __main__ scope.

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)

DynamicallyImportingPythonModules (last edited 2009-12-14 19:29:39 by CarlNobile)