【问题标题】:ImportError: cannot import name 'ClassVar' after installing airflowImportError:安装气流后无法导入名称“ClassVar”
【发布时间】:2020-04-27 02:04:37
【问题描述】:

我已按照以下链接安装 Airflow 的步骤

Airflow installation 并得到以下错误

  Traceback (most recent call last):
  File "/usr/local/bin/airflow", line 25, in <module>
  from airflow.configuration import conf
  File "/usr/local/lib/python3.5/dist-packages/airflow/__init__.py", line 42, in <module>
  from airflow.models import DAG
  File "/usr/local/lib/python3.5/dist-packages/airflow/models/__init__.py", line 21, in 
  <module>
  from airflow.models.baseoperator import BaseOperator, BaseOperatorLink  # noqa: F401
  File "/usr/local/lib/python3.5/dist-packages/airflow/models/baseoperator.py", line 30, in 
  <module>
  from typing import Any, Callable, ClassVar, Dict, FrozenSet, Iterable, List, Optional, 
  Set, Type, Union
  ImportError: cannot import name 'ClassVar'

谁能帮帮我

【问题讨论】:

  • 我昨天遇到了同样的问题。快速修复是使用版本 1.10.6 安装气流
  • @P.Panayotov 你能知道上述错误的原因是什么
  • 因为我可以看到问题在于您的 Python 版本。库类型在 Python 3.5 上不是官方的,因此一种修复方法是更新您的 Python 版本。

标签: python python-3.x airflow


【解决方案1】:

我也遇到了同样的错误:

  • Ubuntu 16.04
  • Python 3.5
  • 气流 1.10.10

查了一下发现是python3.5版本不支持模块类型的问题。ClassVar:https://docs.python.org/3.5/library/typing.html

As introduced in PEP 526, a variable annotation wrapped in ClassVar indicates that a given attribute is intended to be used as a class variable and should not be set on instances of that class.

然后签到PEP 526发现这个功能只支持3.6,哦....

因此解决方案是将 Python 版本升级到 3.6+ 以安装 Airflow 1.10。

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.7
sudo apt install build-essential libssl-dev libffi-dev python3.7-dev
sudo apt-get install python3.7-venv

综上所述,进程成功执行。 为完善官方指南,安装环境要求为:

  • Ubuntu 16.04
  • Python 3.6+
  • 气流 1.10.10

【讨论】: