我似乎不明白如何将模块导入 apache airflow DAG 定义文件。我想这样做是为了能够创建一个库,例如,它可以使具有类似设置的任务声明变得不那么冗长。
这是我能想到的最简单的例子,它重现了这个问题:我修改了气流教程 ( 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
按预期打印到控制台。一切都很好。
然后我进入网络用户界面,并受到 Broken DAG: [/home/airflow/airflow/dags/tutorial.py] No module named 'lib'
的欢迎。取消暂停 dag 并尝试使用 UI 手动运行会导致“正在运行”状态,但它永远不会成功或失败。它只是永远处于“运行”状态。我想排队多少就排队多少,但他们都只会处于“正在运行”状态。
我检查了气流日志,没有看到任何有用的调试信息。
那我错过了什么?
原文由 fildred13 发布,翻译遵循 CC BY-SA 4.0 许可协议
再次添加 sys 路径对我有用,