airflow.operators.python_operator

Module Contents

class airflow.operators.python_operator.PythonOperator(python_callable, op_args=None, op_kwargs=None, provide_context=False, templates_dict=None, templates_exts=None, *args, **kwargs)[source]

Bases: airflow.models.BaseOperator

Executes a Python callable

See also

For more information on how to use this operator, take a look at the guide: PythonOperator

Parameters
  • python_callable (python callable) – A reference to an object that is callable

  • op_kwargs (dict (templated)) – a dictionary of keyword arguments that will get unpacked in your function

  • op_args (list (templated)) – a list of positional arguments that will get unpacked when calling your callable

  • provide_context (bool) – if set to true, Airflow will pass a set of keyword arguments that can be used in your function. This set of kwargs correspond exactly to what you can use in your jinja templates. For this to work, you need to define **kwargs in your function header.

  • templates_dict (dict[str]) – a dictionary where the values are templates that will get templated by the Airflow engine sometime between __init__ and execute takes place and are made available in your callable’s context after the template has been applied. (templated)

  • templates_exts (list[str]) – a list of file extensions to resolve while processing templated fields, for examples ['.sql', '.hql']

template_fields = ['templates_dict', 'op_args', 'op_kwargs'][source]
ui_color = #ffefeb[source]
shallow_copy_attrs = ['python_callable', 'op_kwargs'][source]
execute(self, context)[source]
execute_callable(self)[source]
class airflow.operators.python_operator.BranchPythonOperator[source]

Bases: airflow.operators.python_operator.PythonOperator, airflow.models.SkipMixin

Allows a workflow to “branch” or follow a path following the execution of this task.

It derives the PythonOperator and expects a Python function that returns a single task_id or list of task_ids to follow. The task_id(s) returned should point to a task directly downstream from {self}. All other “branches” or directly downstream tasks are marked with a state of skipped so that these paths can’t move forward. The skipped states are propagated downstream to allow for the DAG state to fill up and the DAG run’s state to be inferred.

Note that using tasks with depends_on_past=True downstream from BranchPythonOperator is logically unsound as skipped status will invariably lead to block tasks that depend on their past successes. skipped states propagates where all directly upstream tasks are skipped.

execute(self, context)[source]
class airflow.operators.python_operator.ShortCircuitOperator[source]

Bases: airflow.operators.python_operator.PythonOperator, airflow.models.SkipMixin

Allows a workflow to continue only if a condition is met. Otherwise, the workflow “short-circuits” and downstream tasks are skipped.

The ShortCircuitOperator is derived from the PythonOperator. It evaluates a condition and short-circuits the workflow if the condition is False. Any downstream tasks are marked with a state of “skipped”. If the condition is True, downstream tasks proceed as normal.

The condition is determined by the result of python_callable.

execute(self, context)[source]
class airflow.operators.python_operator.PythonVirtualenvOperator(python_callable, requirements=None, python_version=None, use_dill=False, system_site_packages=True, op_args=None, op_kwargs=None, provide_context=False, string_args=None, templates_dict=None, templates_exts=None, *args, **kwargs)[source]

Bases: airflow.operators.python_operator.PythonOperator

Allows one to run a function in a virtualenv that is created and destroyed automatically (with certain caveats).

The function must be defined using def, and not be part of a class. All imports must happen inside the function and no variables outside of the scope may be referenced. A global scope variable named virtualenv_string_args will be available (populated by string_args). In addition, one can pass stuff through op_args and op_kwargs, and one can use a return value. Note that if your virtualenv runs in a different Python major version than Airflow, you cannot use return values, op_args, or op_kwargs. You can use string_args though.

Parameters
  • python_callable (function) – A python function with no references to outside variables, defined with def, which will be run in a virtualenv

  • requirements (list[str]) – A list of requirements as specified in a pip install command

  • python_version (str) – The Python version to run the virtualenv with. Note that both 2 and 2.7 are acceptable forms.

  • use_dill (bool) – Whether to use dill to serialize the args and result (pickle is default). This allow more complex types but requires you to include dill in your requirements.

  • system_site_packages (bool) – Whether to include system_site_packages in your virtualenv. See virtualenv documentation for more information.

  • op_args – A list of positional arguments to pass to python_callable.

  • op_kwargs (dict) – A dict of keyword arguments to pass to python_callable.

  • provide_context (bool) – if set to true, Airflow will pass a set of keyword arguments that can be used in your function. This set of kwargs correspond exactly to what you can use in your jinja templates. For this to work, you need to define **kwargs in your function header.

  • string_args (list[str]) – Strings that are present in the global var virtualenv_string_args, available to python_callable at runtime as a list[str]. Note that args are split by newline.

  • templates_dict (dict of str) – a dictionary where the values are templates that will get templated by the Airflow engine sometime between __init__ and execute takes place and are made available in your callable’s context after the template has been applied

  • templates_exts (list[str]) – a list of file extensions to resolve while processing templated fields, for examples ['.sql', '.hql']

execute_callable(self)[source]
_pass_op_args(self)[source]
_execute_in_subprocess(self, cmd)[source]
_write_string_args(self, filename)[source]
_write_args(self, input_filename)[source]
_read_result(self, output_filename)[source]
_write_script(self, script_filename)[source]
_generate_virtualenv_cmd(self, tmp_dir)[source]
_generate_pip_install_cmd(self, tmp_dir)[source]
static _generate_python_cmd(tmp_dir, script_filename, input_filename, output_filename, string_args_filename)[source]
_generate_python_code(self)[source]