Neat Trick for Loading Modules with unknown Name

Brian Quinlan brian at sweetapp.com
Mon Mar 22 04:59:22 CET 2004


> I've found an easy and clean way to load and use modules that are loaded
> after a program has started when the name of the module can not be
> pre-determined.
> 
> modname = 'usermodname'
> # load the module
> exec('import ' + modname)
> # alias the module
> usersmod = eval(modname)

Do you really think that is easier than:

usersmod = __import__(modname)

Cheers,
Brian





More information about the Python-list mailing list