You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache Airflow Docker镜像Python库依赖冲突解决(dbt-bigquery导致)

解决Apache Airflow镜像构建中的依赖冲突问题

问题背景

执行docker-compose build构建Apache Airflow镜像时,出现以下依赖冲突错误:

#15 ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.

#15 google-cloud-aiplatform 1.16.1 requires google-cloud-bigquery<3.0.0dev,>=1.15.0, but you have google-cloud-bigquery 3.10.0 which is incompatible.

#15 google-ads 18.0.0 requires protobuf!=3.18.*,!=3.19.*,<=3.20.0,>=3.12.0, but you have protobuf 3.20.3 which is incompatible.

当前使用的环境配置:

Dockerfile

FROM --platform=linux/amd64 apache/airflow:2.5.3

# install mongodb-org-tools
USER root
RUN apt-get update && apt-get install -y gnupg software-properties-common && \
    curl -fsSL https://www.mongodb.org/static/pgp/server-4.2.asc | apt-key add - && \
    add-apt-repository 'deb https://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main' && \
    apt-get update && apt-get install -y mongodb-org-tools
USER airflow

ADD requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

requirements.txt

gcsfs==0.6.1                        # Google Cloud Storage file system interface
ndjson==0.3.1                       # Newline delimited JSON parsing and serialization
pymongo==3.12.1                     # MongoDB driver for Python
dbt-bigquery==1.5.0                 # dbt adapter for Google BigQuery
numpy==1.21.1                       # Numerical computing in Python
pandas==1.3.1                       # Data manipulation and analysis library
billiard                            # Multiprocessing replacement, to avoid "daemonic processes are not allowed to have children" error using Pool

疑问:如何解决这些依赖冲突?如何确定requirements.txt中各库的依赖关系?猜测google-cloud-aiplatform和google-cloud-bigquery都是dbt-bigquery的依赖,但同一库的依赖不应出现冲突。

补充:构建日志中未出现google-cloud-aiplatform和google-ads的安装记录,仅显示dbt-bigquery安装了google-cloud-bigquery~=3.0和protobuf>=3.18.3。

冲突原因分析

  1. google-cloud-bigquery版本冲突:dbt-bigquery==1.5.0依赖google-cloud-bigquery~=3.0,会安装3.10.0版本;但Airflow基础镜像预装的google-cloud-aiplatform 1.16.1要求该包版本必须在1.15.0至3.0.0dev之间,二者版本不兼容。
  2. protobuf版本冲突:Airflow基础镜像预装的google-ads 18.0.0要求protobuf版本在3.12.0至3.20.0之间(排除3.18.、3.19.),但后续安装过程中protobuf被升级到3.20.3,超出版本限制。
  3. 关键:google-cloud-aiplatform和google-ads并非来自自定义requirements.txt或dbt-bigquery的依赖,而是Apache Airflow官方镜像apache/airflow:2.5.3预先安装的包。

解决方案

方案一:固定兼容版本约束

修改requirements.txt,添加冲突包的兼容版本规则,让pip优先选择满足所有依赖的版本:

gcsfs==0.6.1                        # Google Cloud Storage file system interface
ndjson==0.3.1                       # Newline delimited JSON parsing and serialization
pymongo==3.12.1                     # MongoDB driver for Python
dbt-bigquery==1.5.0                 # dbt adapter for Google BigQuery
numpy==1.21.1                       # Numerical computing in Python
pandas==1.3.1                       # Data manipulation and analysis library
billiard                            # Multiprocessing replacement, to avoid "daemonic processes are not allowed to have children" error using Pool
# 解决依赖冲突
google-cloud-bigquery<3.0.0dev,>=1.15.0
protobuf<=3.20.0,!=3.18.*,!=3.19.*

重新执行docker-compose build即可,该方案会限制google-cloud-bigquery到2.x版本,同时固定protobuf到3.20.0,同时满足dbt-bigquery、google-cloud-aiplatform和google-ads的要求。

方案二:升级预装冲突包

如果需要保留google-cloud-bigquery 3.x版本,可以升级Airflow镜像中预装的google-cloud-aiplatform和google-ads到兼容新版本的版本:
修改Dockerfile,在安装自定义依赖前升级冲突包:

FROM --platform=linux/amd64 apache/airflow:2.5.3

# install mongodb-org-tools
USER root
RUN apt-get update && apt-get install -y gnupg software-properties-common && \
    curl -fsSL https://www.mongodb.org/static/pgp/server-4.2.asc | apt-key add - && \
    add-apt-repository 'deb https://repo.mongodb.org/apt/debian buster/mongodb-org/4.2 main' && \
    apt-get update && apt-get install -y mongodb-org-tools
USER airflow

# 升级预装的冲突包到兼容版本
RUN pip install --upgrade google-cloud-aiplatform>=1.30.0 google-ads>=21.0.0

ADD requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

说明:google-cloud-aiplatform从1.30.0版本开始支持google-cloud-bigquery>=3.0.0;google-ads从21.0.0版本开始支持protobuf>=3.20.0。

方案三:使用旧版依赖解析器(临时方案)

在pip安装命令中添加--use-deprecated=legacy-resolver,强制使用旧版依赖解析逻辑,可能会自动兼容冲突:
修改Dockerfile中的安装命令:

RUN pip install --use-deprecated=legacy-resolver -r /requirements.txt

注意:该方案仅作为临时绕过手段,可能隐藏其他依赖问题,不推荐长期使用。

如何查看依赖关系

1. 使用pip show查看直接依赖

执行以下命令查看单个包的直接依赖:

pip show dbt-bigquery==1.5.0

输出中的Requires字段会列出该包的直接依赖,可递归查看依赖的依赖关系。

2. 使用pipdeptree生成完整依赖树

先安装pipdeptree工具:

pip install pipdeptree

然后执行命令生成完整的依赖树:

pipdeptree

该命令会展示所有已安装包的层级依赖关系,清晰看到间接依赖的来源。

3. 查看包的PyPI元数据

查看对应包的PyPI官方页面,在Dependencies部分可获取官方声明的依赖版本要求。

内容的提问来源于stack exchange,提问作者Canovice

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 11:04:57