By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,310 Members | 1,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,310 IT Pros & Developers. It's quick & easy.

import module and execute function at runtime

P: n/a
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?

Jan 13 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,
if you want to import module dynamically, you can use __import__ ..
always remember modules are objects ..

mStr="sys"
mod=__import__(mStr)
# Now u can use mod.version .. but cant specify the attribute using
other variable, like u did
mod.version

should read it
http://diveintopython.org/functional...ic_import.html

Jan 13 '06 #2

P: n/a
de***@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
Jan 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.