Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error to create a flow in prefect for python using with and object Flow()

Hi everyone I was trying to create a flow using prefect for python but I get the error TypeError: 'fn' must be callable, I already install all the packages and I have tried in different IDE like vs code and colab but the result is the same.

the code is:

from prefect import task, Flow


@task()
def load():
    print("Loading data")

with Flow("Hello-world") as flow:
    load()

flow.run() 


#ERROR
TypeError: 'fn' must be callable

I tried changing the code to this but I get a different error:

from prefect import task, Flow


@task()
def load():
    print("Loading data")

with Flow(name = "Hello-world") as flow:
    load()

flow.run() 

#ERROR
TypeError: Flow.__init__() missing 1 required positional argument: 'fn'
like image 486
Hardroco Avatar asked Sep 18 '25 10:09

Hardroco


2 Answers

In Prefect 2.0 or major, we need to create a flow how in the next example:

import pandas as pd
import numpy as np

from prefect import flow, task, Flow

@task
def extract():
    pass

@task
def transform():
    pass

@task
def load():
    pass

"""
with Flow("ETL Test") as flow:
    extract()
    transform()
    load()

flow.run()
"""

@flow
def flow_caso():
    extract()
    transform()
    load()

flow_caso()

In the comments you can see how we can create a flow in Prefect with version minor to 2.0.0, but in versions major you need to create a function with the decorator @flow.

like image 55
albertusortiz Avatar answered Sep 20 '25 00:09

albertusortiz


I believe you are trying to run a Prefect 1.0 flow using the Prefect 2.0 package.

I was able to run your example fine using prefect==1.2.4 and I was able to reproduce your error using prefect==2.0.0

Here's what your example could look like using prefect>=2.0.0:

from prefect import flow, task

@task
def load():
   print("Loading data")

@flow(name="Hello-world")
def my_flow():
   load()

if __name__ == "__main__":
   my_flow()

Here are some docs on running prefect 2.0 flows

like image 26
Stoat Avatar answered Sep 20 '25 01:09

Stoat