PyInvoke execution task from other python script

Viewed 433

I'm reading pyinvoke docs and I'm searching any easy way to execute invoke tasks from other python script. Tasks don't have method run so I can't import them and simply .run(). I found that there is Executor Class but how I understand I need to first declare Collection of tasks and then I can run one of tasks from script. Maybe there is another way to do it easiest from other python script which isn't task?

1 Answers

The point is "task is also a python function", so you can invoke task by calling task function as following:

Define a task function test in a.py.

#a.py
from invoke import task
@task
def test(c):
    print("hello, I'm a-test!")

Import module a and execute task by calling a.test() function.

#tasks.py
from invoke import task
import a
@task
def executeatest(c):
    print('executing [a-test] task defined in "a.py"')
    a.test(c)

When you execute inv executeatest, it results:

executing [a-test] task defined in "a.py"
hello, I'm a-test!
Related