import module and execute function at runtime

Giovanni Bajo noway at sorry.com
Fri Jan 13 20:18:04 EST 2006


denny at sopris.net wrote:

> I'm trying to import a module at runtime using variables to specify
> which module, and which functions to execute. for example:
>
> mStr = "sys"
> fStr = "exit"
>
> # load mod
> mod = __import__(mStr)
> # call function
> mod.fStr()


getattr(mod, fStr)()
-- 
Giovanni Bajo





More information about the Python-list mailing list