gpt4 book ai didi

python - Apache Airflow DAG 无法导入本地模块

转载 作者:太空狗 更新时间:2023-10-29 17:33:21 24 4
gpt4 key购买 nike

我似乎不明白如何将模块导入 apache airflow DAG 定义文件。例如,我想这样做是为了能够创建一个库,该库可以使具有类似设置的任务声明变得不那么冗长。

这是我能想到的最简单的例子,它重现了这个问题:我修改了 Airflow 教程 ( https://airflow.apache.org/tutorial.html#recap ) 以简单地导入一个模块并从该模块运行一个定义。像这样:

目录结构:

- dags/
-- __init__.py
-- lib.py
-- tutorial.py

教程.py:

"""
Code that goes along with the Airflow located at:
http://airflow.readthedocs.org/en/latest/tutorial.html
"""
from airflow import DAG
from airflow.operators.bash_operator import BashOperator
from datetime import datetime, timedelta

# Here is my added import
from lib import print_double

# And my usage of the imported def
print_double(2)

## -- snip, because this is just the tutorial code,
## i.e., some standard DAG defintion stuff --

print_double 只是一个简单的 def,它将您给它的任何输入乘以 2,然后打印结果,但显然这并不重要,因为这是一个导入问题。

我能够根据教程文档成功运行 airflow test tutorial print_date 2015-06-01 - dag 运行,而且 print_double 成功。 4 按预期打印到控制台。一切都很好。

然后我进入 Web UI,看到 Broken DAG: [/home/airflow/airflow/dags/tutorial.py] No module named 'lib'。取消暂停 dag 并尝试使用 UI 手动运行会导致“正在运行”状态,但它永远不会成功或失败。它只是永远处于“运行”状态。我可以根据需要排队,但他们都只会处于“正在运行”状态。

我检查了 Airflow 日志,没有看到任何有用的调试信息。

那我错过了什么?

最佳答案

再次添加 sys 路径对我有用,

import sys
sys.path.insert(0,os.path.abspath(os.path.dirname(__file__)))

关于python - Apache Airflow DAG 无法导入本地模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47998552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com