I have two python files and both at same directory level like this
project
|> tasks.py
|> queue.py
I have a function like this in tasks.py
file
def foo(message):
# perform some functions on message data
# blah blah
and another function in queue.py
file like this
import project.tasks
def run_task(function_name, data):
# function name is a string which tells which function to call in tasks.py file
return getattr(project.tasks, function_name)(data)
I do not want to hard code my function name. I want to access them dynamically on the basis of function_name
parameter. My tasks.py
has become very large therefore I want to divide it in parts. The thing which I did is I moved tasks.py
file in a python package like this.
project
|> tasks
|> __init__.py
|> tasks.py
|> queue.py
The question which I want to ask is that how I can modify my run_task
function in order to work for this struction. As getattr
function task first argument an object. Is there any other option avilable which I can used to access function inside task
package.?
Answer from @jonrsharpe comment.
Put following in in the __init__.py
from .tasks import *
to expose all names in tasks.py
from the tasks module. Do it for all files you have in tasks
package.