首页 > 解决方案 > AttributeError: 'datetime.timezone' 对象在尝试运行 Apache Airflow 调度程序时没有属性'name'

问题描述

我刚刚设置了一台新的 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

标签: pythonairflowpendulum

解决方案


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"), 
    ...
    )

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


推荐阅读