35
votes

I'm trying to import a local module (a python script) to my DAG.

Directory structure:

airflow/
├── dag
│   ├── __init__.py
│   └── my_DAG.py
└── script
    └── subfolder
        ├── __init__.py
        └── local_module.py

Sample code in my_DAG.py:

#trying to import from local module
from script.subfolder import local_module  

#calling a function in local_module.py  
a = some_function()  

I get an error in Airflow saying 'Broken DAG: my_DAG. No module named 'local_module'.

I've updated Airflow to 1.9.0 but this doesn't fix the issue.

  • What is the solution here?
  • I also read somewhere that I could solve this by creating a plugin. Can anyone point to how I can do this?

Thanks.

3

3 Answers

7
votes

This usually has to do with how Airflow is configured.

In airflow.cfg, make sure the path in airflow_home is correctly set to the path the Airflow directory strucure is in.

Then Airflow scans all subfolders and populates them so that modules can be found.

Otherwise, just make sure the folder you are trying to import is in the Python path: How to use PYTHONPATH

3
votes

The way I do it is as following:

  1. create a Python script in your sub-folder with a main() function.
  2. in your dag file include a path declaration for the sub-folder and the file

Now you can use this script in your PythonOperator

import sys
sys.path.insert(0,"/root/airflow/dags/subfolder"))
import subfolder.script_name as script
...    
t1=PythonOperator(
    task_id='python_script',
    python_callable=script.main,
    dag=dag
)
0
votes

If you run Airlow in a docker then you need to do it as following:

  1. Create a folder for your modules in dags folder. For example programs
  2. Use it as following (this is the correct path for docker):
import sys
sys.path.append('/opt/airflow/dags/programs/my_module')
import my_module
task1 = PythonOperator(
        task_id='my_task_name',
        python_callable=my_module.my_func,
        dag=dag,
    )