How to import external scripts in a Airflow DAG with Python?

我只是一个虾纸丫 提交于 2020-03-25 03:40:31

问题


I have the following structure:

And I try to import the script inside some files of the inbound_layer like so:

import calc

However I get the following error message on Airflow web:

Any idea?


回答1:


I needed insert the following script inside at the top of ren.py :

import sys, os
from airflow.models import Variable

DAGBAGS_DIR = Variable.get('DAGBAGS_DIR')
sys.path.append(DAGBAGS_DIR + '/bi/inbound_layer/')

This way I make available the current folder packages.




回答2:


For airflow DAG, when you import your own module, you need make sure 2 things:

  1. where is the module? You need to find where is the root path in you airflow folder. For example, in my dev box, the folders are:

    ~/projects/data/airflow/teams/team_name/projects/default/dags/dag_names/dag_files.py

The root is airflow, so if I put my modules my_module in

~/projects/data/airflow/teams/team_name/common

Then I need to use

from teams.team_name.common import my_module

In your case, if the root is the upper folder of bi, and you put the scripts of calc in bi/inbound_layer/test.py then you can use:

from bi.inbound_layer.test import calc
  1. And you must make sure you have \__init\__.py files in the directory structure for the imports to function properly. You should have an empty file \__init\__.py in each folder in the path. It indicates this directory is part of airflow packages. In your case, you can use touch \__init\__.py (cli) under bi and _inbound_layer_ folders to create the empty __init\__.py.


来源:https://stackoverflow.com/questions/56134683/how-to-import-external-scripts-in-a-airflow-dag-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!