howto get function from module, known by string names?

D

dmitrey

hi all,
can anyone explain howto get function from module, known by string
names?
I.e. something like

def myfunc(module_string1, func_string2, *args):
eval('from ' + module_string1 + 'import ' + func_string2')
return func_string2(*args)

or, if it's impossible, suppose I have some modules
module1 module2 module3 ... etc
each module has it own funcs 'alfa', 'beta' and class module1,
module2,... with same string name as module name.
can I somehow pass module as function param and then import function
'alfa' from (for example) module8? and/or import class moduleN from
moduleN?

something like
def myfunc(moduleK, *args):
return moduleK.moduleK(*args)
or return moduleK.alfa(*args)

Thx, D
 
C

Carsten Haese

On 15 May 2007 04:29:56 -0700, dmitrey wrote
hi all,
can anyone explain howto get function from module, known by string
names?
I.e. something like

def myfunc(module_string1, func_string2, *args):
eval('from ' + module_string1 + 'import ' + func_string2')
return func_string2(*args)

To find a module by its name in a string, use __import__. To find an object's
attribute by its name in a string, use getattr.

Together, that becomes something like this:
.... func = getattr(__import__(module_string1), func_string2)
.... return func(*args)
....5

Hope this helps,
 
D

dmitrey

And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
Thx, D.
 
D

dmitrey

And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
Thx, D.
 
D

dmitrey

And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)
Thx, D.
 
G

Gabriel Genellina

And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)

What's wrong with this:

try: import asdf
except ImportError: asdf = None

....later...

if asdf:
asdf.zxcv(1)
...
 
C

Carsten Haese

And howto check does module 'asdf' exist (is available for import) or
no? (without try/cache of course)

Why would you need to do that? What are you planning to do when you have
determined that the module doesn't exist? Surely you're not planning on
swallowing the error silently, because that would make your program very
hard to debug. The Pythonic response is to raise an exception, but then
you might as well just propagate the exception that __import__ already
raises:
.... func = getattr(__import__(module_string1), func_string2)
.... return func(*args)
.... Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in myfunc
ImportError: No module named asdf

Hope this helps,
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top