question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PythonVirtualenvOperator with provide_context=True does not have 'ti' keyword

See original GitHub issue

Apache Airflow version: 1.10.13

Environment: Docker (Ubuntu 18.4 - Python 3.7)

  • Cloud provider or hardware configuration: Azure
  • OS (e.g. from /etc/os-release): Docker (Ubuntu 18.4 - Python 3.7)
  • Kernel (e.g. uname -a): Docker (Ubuntu 18.4 - Python 3.7)
  • Install tools: N/A
  • Others: N/A

What happened:

When we enable provide_context=True for PythonVirtualenvOperator and try to use the xcom_push to pass a variable I get this error:

File "/tmp/venv0upgqome/script.py", line 13, in push\n kwargs[\'ti\'].xcom_push(key=\'value from pusher 1\', value=value_1)\nKeyError: \'ti\'\n'

How to reproduce it:

import airflow
from airflow import DAG
from airflow.operators.python_operator import PythonVirtualenvOperator

args = {
    'owner': 'Airflow',
    'start_date': airflow.utils.dates.days_ago(2),

}

dag = DAG('BAtatas', schedule_interval="@once", default_args=args)


def push(**kwargs):
    """Pushes an XCom without a specific target"""
    value_1 = [1, 2, 3]
    print("printing the kwargs!!!")
    print(kwargs)
    kwargs['ti'].xcom_push(key='value from pusher 1', value=value_1)


def push_by_returning(**kwargs):
    value_2 = {'a': 'b'}
    """Pushes an XCom without a specific target, just by returning it"""
    return value_2


def puller(**kwargs):
    value_2 = {'a': 'b'}
    value_1 = [1, 2, 3]
    """Pull all previously pushed XComs and check if the pushed values match the pulled values."""
    ti = kwargs['ti']

    # get value_1
    pulled_value_1 = ti.xcom_pull(key=None, task_ids='push')
    assert pulled_value_1 == value_1

    # get value_2
    pulled_value_2 = ti.xcom_pull(task_ids='push_by_returning')
    assert pulled_value_2 == value_2

    # get both value_1 and value_2
    pulled_value_1, pulled_value_2 = ti.xcom_pull(
        key=None, task_ids=['push', 'push_by_returning'])
    assert (pulled_value_1, pulled_value_2) == (value_1, value_2)


push1 = PythonVirtualenvOperator(
    task_id='push',
    dag=dag,
    python_callable=push,
    requirements=[],
    python_version='3.7',
    use_dill=False,
    provide_context=True,
    system_site_packages=True,

)

push2 = PythonVirtualenvOperator(
    task_id='push_by_returning',
    dag=dag,
    python_callable=push_by_returning,
    requirements=[],
    python_version='3.7',
    use_dill=False,
    provide_context=True,
    system_site_packages=True,

)

pull = PythonVirtualenvOperator(
    task_id='puller',
    dag=dag,
    python_callable=puller,
    requirements=[],
    python_version='3.7',
    use_dill=False,
    provide_context=True,
    system_site_packages=True,

)

pull << [push1, push2]

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
uranusjrcommented, May 23, 2022

Feel free to propose a PR to add ti to the list.

1reaction
xinbinhuangcommented, May 2, 2021

Also found this Jira story (https://issues.apache.org/jira/browse/AIRFLOW-2738) that’s still open that seems to be addressing the same problem.

Just a quick note: the community has migrated the issues tracking system from Jira to GitHub issues, so whatever left in Jira right now is likely outdated or inaccurate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PythonOperator — Airflow Documentation
Airflow passes in an additional set of keyword arguments: one for each of ... Otherwise you won't have access to the most context...
Read more >
python_operator - Apache Airflow Documentation
class PythonVirtualenvOperator(PythonOperator): """ Allows one to run a function in a virtualenv that is created and destroyed automatically (with certain ...
Read more >
python - How to use PythonVirtualenvOperator in airflow?
The problem is. provide_context=True,. Airflow cannot pickle the context because of all the unserializable stuff in it.
Read more >
PythonVirtualenvOperator givig PicklingError - Google Groups
I am trying to run a PythonVirtualenvOperator within a dag in a gcloud ... :param provide_context: if set to true, Airflow will pass...
Read more >
The Zen of Python and Apache Airflow - GoDataDriven
Option 2: use DAG in context manager, no need to reference the DAG ... The PythonOperator with provide_context=True passes the Airflow ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found