You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Prefect创建Python Flow时遭遇TypeError错误求助

解决Prefect中TypeError: 'fn' must be callable的问题

你的问题出在Prefect版本不匹配:你写的是Prefect 1.x的代码,但实际安装的是Prefect 2.x,两个版本的Flow定义语法完全不同,所以触发了类型错误。

两种解决方法:

方法1:改用Prefect 2.x的正确写法

Prefect 2.x不再使用with Flow()的上下文语法,而是用@flow装饰器来定义流程,修改后的代码如下:

from prefect import task, flow

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

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

# 运行流程
hello_world_flow()

方法2:安装Prefect 1.x版本

如果你想继续用原来的1.x语法,需要指定安装1.x系列的版本,执行以下命令:

pip install prefect==1.4.0  # 可根据需求选择合适的1.x版本号

安装完成后,你最初的代码就能正常运行了。

错误原因解释

  • 在Prefect 2.x中,Flow类的构造函数要求必须传入一个可调用的函数(即fn参数),你直接传入字符串名称或者仅指定name参数,不符合要求,所以报错TypeError: 'fn' must be callable或missing 1 required positional argument: 'fn'。
  • Prefect 1.x才支持with Flow("流程名")这种上下文方式来构建流程。

内容的提问来源于stack exchange,提问作者Hardroco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 16:54:52