How to use non-installable modules from DAG code?

試著忘記壹切 提交于 2019-11-27 08:26:29

问题


I have a Git repository which (among other things) holds Airflow DAGs in airflow directory. I have a clone of the repository besides an install directory of Airflow. airflow directory in Git is pointed to by AIRFLOW_HOME configuration variable.

I would like to allow imports from modules in the repository that are listed outside airflow folder (please see the structure below).

<repo root>
   |_airflow
      |_dags
         |_dag.py
   |_module1
   |_module2
   |_...

So that in dag.py I can do:

from module1 import Module1

Currently, it does not seem possible without tricks like editing sys.path explicitly which is not very elegant and has to be done in each of the dag source files...

Making an installable package out of the module1 is also out of the question.


回答1:


Re-writing conclusion from discussions here


Broadly, there are 2 possible ways

  1. Package your code into an Airflow plugin
  2. Make your code discoverable to dag-definition-file(s) parsing processes by updating PYTHONPATH. Here again we have following options

    (a) Update PYTHONPATH on system level using bashrc / equivalent (once-and-for-all) or just export the updated PYTHONPATH for current bash session

    (b) Programmatically update sys.path in the beginning of DAG-definition file



来源:https://stackoverflow.com/questions/57676017/how-to-use-non-installable-modules-from-dag-code

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