9

Ive searched the web and this site and cant find an answer to this problem. Im sure its right in front of me somewhere but cant find it.

I need to be able to import a module based on a string. Then execute a function within that module while passing arguments.

I can import based on the string and then execute using eval() but I know this is not the best way to handle this. I also cant seem to pass arguments that way.

My current module that would be set based on a string is named TestAction.py and lives in a folder called Tasks. This is the content of TestAction.py:

def doSomething(var):
    print var

This is the code I am executing to import TestAction and execute.

module = "Tasks.TestAction"
import Tasks
mymod = __import__(module)
eval(module + ".doSomething()")

How can I make this code #1 not use eval() and #2 pass the var argument to doSomething()?

Thanks in advance!

5 Answers 5

4

Is the function name also variable? If not, just use your imported module:

mymod.doSomething('the var argument')

if it is, use getattr:

fun = 'doSomething'
getattr(mymod, fun)('the var argument')
Sign up to request clarification or add additional context in comments.

Comments

4

Thanks everyone for the help. it looks like importlib combined with getattr was what I needed. For future reference here is the exact code that is working for me.

module = "FarmTasks.TestAction"
mymod = importlib.import_module(module)

ds = getattr(mymod, "doSomething")
ds("stuff")

Comments

2

According to the documentation

it is better to use importlib.import_module() to programmatically import a module.

Using this you can retrieve your module like this:

import importlib
TestAction = importlib.import_module("TestAction", package="Tasks")

After that you can simply call functions normally or by name:

TestAction.some_known_function(arg1, arg2)
getattr(TestAction, "some_other_function_name")(arg1, arg2)

I hope this answered your question, feel free to elaborate if you are still missing something.

1 Comment

I have a use case restricted to Python2.6. What is the disadvantage to not using importlib? Since importlib doesn’t exist for 2.6, I don’t have a choice. But I’m wondering what issues might arise? I’m assuming there must be some possible issues, else importlib wouldn’t have been created.
0

If you use Python 2.7 or 3.1+ the easiest way to go is to use the importlib module in combination with getattr. Your code would look like that then:

import importlib

module = "Tasks.TestAction"
mymod = importlib.import_module(module)
myfunc = getattr(mymod, "doSomething")
myfunc()

Comments

0

I recently wrote a simple function that imports a given function, it seems to work for me:

def import_function(name):
    """Import a function by name: module.function or
    module.submodule.function, etc. Return the function object."""

    mod, f = name.rsplit('.', 1)
    return getattr(__import__(mod, fromlist=[f]), f)

You can use it as:

f = import_function('Tasks.TestAction.doSometing')
f()

or just

import_function('Tasks.TestAction.doSometing')()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.