Search code examples
pythonpython-3.xpipairflowairflow-2.x

Broken DAG after Airflow Update v.2.2.2 -> v.2.7.2


I am new to python and I am having issues with my Airflow DAG which is importing modules inside my package.

Broken DAG: [/opt/airflow/dags/foo_export/util.py] Traceback (most recent call last):   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed   File "/opt/airflow/dags/foo_export/util.py", line 12, in <module>     from .constants import ERROR_LOG ImportError: attempted relative import with no known parent package

I am using Python 3.9.18.

My Project/DAG Directory:

airflow
├── dags
│   ├── foo_export
│       ├── __init__.py
│       ├── util.py
│       └── config.py
│       └── export.py
│       └── dags.py
│       └── constants.py
├── airflow.cfg
├── logs
├── plugins

Snippet of the util.py:

import io
import logging
from contextlib import contextmanager
from datetime import datetime

from airflow.models import TaskInstance

from .constants import ERROR_LOG, PUBLICATION_DATE
....

Snippet of constants.py:

PUBLICATION_DATE = "publication_date"
ERROR_LOG = "error_log"

Strangely: This module worked on Airflow v.2.2.2 before without breaking any DAG this issue happens only after the update to v.2.7.2

According to https://airflow.apache.org/docs/apache-airflow/2.7.2/installation/prerequisites.html I am using a Python version which was also tested already.


Solution

  • In Modules Management docs section it says:

    Never use relative imports (starting with .) that were added in Python 3. This is tempting to do something like that it in my_dag1.py:

    from .base_dag import BaseDag  # NEVER DO THAT!!!!
    

    You should import such shared DAG using full path (starting from the directory which is added to PYTHONPATH)

    In your case you - as dags folder is under PYTHONPATH should have:

    from foo_export.constants import ERROR_LOG, PUBLICATION_DATE