/ dag目录外部的气流相对导入 [英] Airflow Relative Importing Outside /dag Directory
问题描述
我无法将通用代码移出气流使用的dag目录之外。我查看了气流源和找到 imp.load_source 。
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.
是否可以使用 imp.load_source
加载dag目录之外的模块?在下面的示例中,这将是从公共目录导入foo或bar。
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
推荐答案
只需添加 __ init __。py
个文件。它应该工作。
在我的文件夹结构中包含 __ init __。py
的每个文件夹中使用。我可以运行代码并查看输出。
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
和 dag_1.py 代码可以是:
from stackoverflow.src.questions.airflow_home.common.bar import bar_test
def main():
bar_test()
main()
我从此运行这段宁静的代码我的 pycharm
。
在我的pycharm中,您的 airflow_home
的文件夹路径是 stackoverflow / src / questions / airflow_home /
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/
而 bar.py 代码是
def bar_test():
print "bar hara"
这篇关于/ dag目录外部的气流相对导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!