Airflow Relative Importing Outside /dag Directory

自闭症网瘾萝莉.ら 提交于 2019-12-04 21:23:49

问题


I haven't been able to move common code outside of the dag directory that airflow uses. I've looked in the airflow source and found imp.load_source.

Is it possible to use imp.load_source to load modules that exist outside of the dag directory? In the example below this would be importing either foo or bar from the common directory.

── airflow_home
     |──── dags
     │   ├── dag_1.py
     │   └── dag_2.py
     ├── common
         ├── foo.py
         └── bar.py

回答1:


Just add __init__.py files in all 3 folders. it should work. Infact every folder in my folder structure having __init__.py. I could run the code and see output.

Example folder structure could be as:

── airflow_home
     ├── __init__.py
     |──── dags
     │   ├── __init__.py
     │   ├── dag_1.py
     │   └── dag_2.py
     ├── common
         ├── __init__.py
         ├── foo.py
         └── bar.py

and dag_1.py code can be as:

from stackoverflow.src.questions.airflow_home.common.bar import bar_test

def main():
    bar_test()

main()

This peace of code i am running from my pycharm. Your airflow_home's folder path in my pycharm is stackoverflow/src/questions/airflow_home/

And bar.py code is

def bar_test():
    print "bar hara"


来源:https://stackoverflow.com/questions/45070705/airflow-relative-importing-outside-dag-directory

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