pythonairflow

How to use AirFlow to run a folder of python files?


I have a series of Python tasks inside a folder of python files: file1.py, file2.py, ...

I read the Airflow docs, but I don't see how to specify the folder and filename of the python files in the DAG?

I would like to execute those python files (not the Python function through Python Operator).

Task1: Execute file1.py (with some import package)

Task2: Execute file2.py (with some other import package)

It would be helpful. Thanks, regards


Solution

  • You can use BashOperator to execute python files as a task

        from airflow import DAG
        from airflow.operators import BashOperator,PythonOperator
        from datetime import datetime, timedelta
    
        seven_days_ago = datetime.combine(datetime.today() - timedelta(7),
                                          datetime.min.time())
    
        default_args = {
            'owner': 'airflow',
            'depends_on_past': False,
            'start_date': seven_days_ago,
            'email': ['airflow@airflow.com'],
            'email_on_failure': False,
            'email_on_retry': False,
            'retries': 1,
            'retry_delay': timedelta(minutes=5),
          )
    
        dag = DAG('simple', default_args=default_args)
    t1 = BashOperator(
        task_id='testairflow',
        bash_command='python /home/airflow/airflow/dags/scripts/file1.py',
        dag=dag)