import module and execute function at runtime

D

denny

Hello, rookie here.

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()

can i do this sort of thing? other suggestions?
 
G

Giovanni Bajo

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)()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top