5

I have the following structure:

enter image description here

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:

enter image description here

Any idea?

Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
  • if you are using pycharm and add the package in project dependency than you can import it so , else you have to add the desired package in path to run it. – sahasrara62 May 14 '19 at 16:24
  • Sounds like to need to add the package to your path. See [How to import a Python class that is in a directory above?](https://stackoverflow.com/a/11096846/8150685) as an example. – Error - Syntactical Remorse May 14 '19 at 16:28

3 Answers3

4

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.
Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
AC at CA
  • 695
  • 6
  • 13
4

Airflow adds dags/, plugins/, and config/ directories in the Airflow home to PYTHONPATH by default so you can for example create folder commons under dags folder, create file there (scriptFileName ). Assuming that script has some class (GetJobDoneClass) you want to import in your DAG you can do it like this:

from common.scriptFileName import GetJobDoneClass
Hrvoje
  • 13,566
  • 7
  • 90
  • 104
  • 2
    That's work. What happen if we don't want to have it under dags? and just another folder next to dags. How does it work? – pm1359 May 18 '21 at 18:01
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.

Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73