【问题标题】:AttributeError: 'datetime.timezone' object has no attribute 'name' when trying to run Apache Airflow schedulerAttributeError: 'datetime.timezone' 对象在尝试运行 Apache Airflow 调度程序时没有属性'name'
【发布时间】:2019-10-22 08:25:08
【问题描述】:

我刚刚设置了一台新的 Ubuntu 机器,创建了一个 Python3.6 venv 并安装了气流。我可以启动网络服务器,但是当我尝试运行 airflow scheduler 时,我不断收到此错误:

File "/home/ubuntu/venv/airflow/lib/python3.6/site-packages/airflow/models/dag.py", line 398, in following_schedule
    tz = pendulum.timezone(self.timezone.name)
AttributeError: 'datetime.timezone' object has no attribute 'name'

这是我pip freeze的摘录:

apache-airflow==1.10.5
boto3==1.9.253
Pillow==6.2.1
selenium==3.141.0
slackclient==1.2.1

【问题讨论】:

    标签: python airflow pendulum


    【解决方案1】:

    您使用的datetime.datetime 对象有一个tzinfo,没有name 属性。例如,如果您使用 datetime.timezone.utc,则该实现没有 name 属性。

    from datetime import datetime,timezone,timedelta
    dag = DAG(
        dag_id="xxxx",
        default_args=args,
        start_date=datetime(2021, 5, 31, tzinfo=timezone.utc, # will raise AttributeError later
        schedule_interval="0 8 * * *",
        max_active_runs=1,
        dagrun_timeout=timedelta(minutes=60),
        catchup=False,
        description="xx",
    )
    

    因此您需要提供另一个时区对象,例如 pendulum 包提供的时区对象 (tzinfo=pendulum.timezone("UTC"))。

    import pendulum
    from datetime import datetime,timedelta
    dag = DAG(
        dag_id="xxxx",
        start_date=datetime(2021, 5, 31, tzinfo=pendulum.timezone("UTC"), 
        ...
        )
    

    我向气流项目提交了一个问题,以便为这种特殊情况提供更好的错误消息here

    【讨论】:

      【解决方案2】:

      看起来您可能只需要使用 Airflow 的特殊形式的 datetime 对象(请参阅文档和 sn-p here)。如果您将所有当前使用的datetime.datetime 替换为airflow.utils.timezone.datetime 的调用,则生成的对象将具有tzinfos 和name 字段。

      【讨论】:

        猜你喜欢
        • 2019-05-24
        • 2019-10-28
        • 1970-01-01
        • 2017-04-15
        • 2021-10-22
        • 2021-05-06
        • 1970-01-01
        • 2021-09-25
        • 2020-04-14
        相关资源
        最近更新 更多