1

I am new to airflow and I need to execute a python script using PythonOperator.

I am aware of defining a function in the DAG and then call it using python_callable. However, I have my code in a .py file and I want to run the whole script as it would be done from command line. The .py file currently accepts command line arguments, but I will modify it to get the values from the DAG op_kwargs.

If the only way to use the .py file is BashOperator, then do I need to change the way the script currently accepts command line arguments, as I guess the passed arguments will be treated as command line args?

Can you kindly help me with the DAG code for this?

1 Answer 1

1

Let's say you save your code in a file named filename. Just write from filename import function, and then call the function using function(command, **context) from your DAG file. So in your DAG you'll have:

task = PythonOperator(
                task_id=task_id,
                python_callable=function,
                op_kwargs={'command': command},
                provide_context=True,
                dag=dag,
        )
Sign up to request clarification or add additional context in comments.

3 Comments

He wants to run the entire file as a script. Not only a particular function from that file. Can this be done with python operator? This can obviously be achieved with bash operator, but we lose out some convenience of the python operator, for example the requirements parameter.
We can still create a wrapper function for all of the components located on the py file and then run it as a callable python function iirc
Its a script which is not segregated into functions. There isn't any function to call. I would have to modify that py file to keep all the code into a function like main, and then use it. Anyways, seems like I have to make do with bash operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.