【发布时间】:2018-12-24 13:56:37
【问题描述】:
简介:我可以通过 $ python my_dag.py 运行 dag,但通过 Airflow UI 它声称错误 No module named 'my_file_to_be_imported'。
我有一个容器,其中包含 dags 和 lutils(我的自定义文件夹)git 文件夹映射为气流主页内的卷:
── airflow_home
|──── dags
│ ├── __init__.py
| ├── my_dag.py
├──── lutils
├── __init__.py
├── my_file_to_be_imported.py
dags文件夹内的my_dag.py文件需要从lutils文件夹中读取内容。
my_dag.py(简体)定义如下:
import sys
sys.path.append('../')
from lutils import my_file_to_be_imported
def do_something():
my_file_to_be_imported.beauty_imported_method()
t1 = PythonOperator(
task_id='test_generate',
python_callable=do_something,
dag=dag)
my_file_to_be_imported.beauty_imported_method() #to check if python runs
print (my_file_to_be_imported.var) #to check if python runs
my_file_to_be_imported 文件夹内的my_file_to_be_imported 文件为:
def beauty_imported_method():
with open('text.txt', 'a') as f:
f.write("test")
var = "my test var"
如果我通过 bash $ python my_dag.py(作为 python 脚本)运行,它确实会执行 beauty_imported_method 并打印 var 变量。
但是在气流内部,有一个红色警告说:Broken DAG: [path_to_airflow_home/dags/my_dag.py] No module named 'my_file_to_be_imported'
我怎样才能解决这个问题,airflow 像 python run 那样理解我的导入?
我在 stackoverflow 中阅读了 this 非常封闭的问题,但没有用。
P.s.:这个 docker 设置可以很好地运行其他不依赖于相对导入的 dag。
【问题讨论】:
-
PYTHONPATH、AIRFLOW_HOME在airflow.cfg和path中的设置如何?