https://prefect.io logo
Title
a

Alexandru Anghel

11/09/2022, 5:33 PM
Hi guys, Is there a way to import a function form another file as a task? I was trying something like this:
from tasks.date import get_start_date
@task(name='Generate start date', retries=2, retry_delay_seconds=10)(get_start_date)
It fails with `RuntimeError: Tasks cannot be run outside of a flow. To call the underlying task function outside of a flow use
task.fn()
.` Thanks!
1
r

Ryan Peden

11/09/2022, 5:39 PM
Hi Alexandru, would something like this do what you need?
from prefect import flow, task
from tasks.date import get_start_date

get_start_date_task = task(get_start_date, name='Generate start date', retries=2, retry_delay_seconds=10)

# you can now call get_start_date_task inside a flow

@flow
def my_flow():
    start_date = get_start_date_task()
a

Alexandru Anghel

11/09/2022, 6:25 PM
Yes, works beautifully! Thanks, Ryan!
😀 1
👍 1