mad_
mad_

Reputation: 8273

Difference between DAG import in two ways?

I am trying to create dynamic dag but seems to be failing at the minute. I came across creating the DAG object in two different:

Upvotes: 0

Views: 391

Answers (1)

cwurtz
cwurtz

Reputation: 3257

Both are importing the same DAG class. Just an attribute of how python imports works.

When you do from airflow.models import DAG python is importing the models file and assigning the variable DAG to the DAG class defined in the models file.

When you do from airflow import DAG python is importing the variable DAG defined in init.py, which is in fact just from airflow.models import DAG.

A minimal version being:

models.py

class DAG():
    pass

init.py

from airflow.models import DAG

dags/dag_file.py

# import __init__.py which imports models.py which contains DAG
from airflow import DAG

# or this which just imports models.py which contains DAG
from airflow.models import DAG

All that being said, if your dynamic DAG is failing, I doubt it's related to this import

Upvotes: 5

Related Questions