import module and execute function at runtime

Giovanni Bajo noway at sorry.com
Sat Jan 14 02:18:04 CET 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