Set Python version of a single DAG

Use the PythonVirtualenvOperator to select an explicit Python version for a DAG.

Code sample


import datetime

from airflow import models
from airflow.operators import python_operator

def python2_function():
    """A function which has not been converted to Python 3."""
    # Use the global variable virtualenv_string_args to pass in values when the
    # Python version differs from that used by the Airflow process.
    global virtualenv_string_args

    # Imports must happen within the function when run with the
    # PythonVirtualenvOperator.
    import cStringIO
    import logging

    arg0 = virtualenv_string_args[0]
    buffer = cStringIO.StringIO()
    buffer.write('Wrote an ASCII string to buffer:\n')

yesterday = datetime.datetime.combine( - datetime.timedelta(1),

default_dag_args = {
    # Setting start date as yesterday starts the DAG immediately when it is
    # detected in the Cloud Storage bucket.
    'start_date': yesterday,

with models.DAG(
        default_args=default_dag_args) as dag:

    # Use the PythonVirtualenvOperator to select an explicit python_version.
    run_python2 = python_operator.PythonVirtualenvOperator(
        string_args=['An example input string'],

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser