【问题标题】:Airflow - Import from parent folder气流 - 从父文件夹导入
【发布时间】:2018-12-24 13:56:37
【问题描述】:

简介:我可以通过 $ python my_dag.py 运行 dag,但通过 Airflow UI 它声称错误 No module named 'my_file_to_be_imported'

我有一个容器,其中包含 dagslutils(我的自定义文件夹)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'

我怎样才能解决这个问题,airflowpython run 那样理解我的导入?

我在 stackoverflow 中阅读了 this 非常封闭的问题,但没有用。

P.s.:这个 docker 设置可以很好地运行其他不依赖于相对导入的 dag。

【问题讨论】:

  • PYTHONPATHAIRFLOW_HOMEairflow.cfgpath 中的设置如何?

标签: python airflow


【解决方案1】:

正如@tobi6 提到的,您应该将两个变量 PYTHONPATH 和 AIRFLOW_HOME 指向相应的文件夹。在您的情况下,我相信 PYTHONPATH 未设置或设置不正确。

应该是这样的:

export PYTHONPATH=/path/to/airflow_home

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    相关资源
    最近更新 更多