如何在Apache Airflow 2.2.5镜像中安装PyTorch 1.13.1?
问题:在apache/airflow:2.2.5-python3.9镜像中安装PyTorch 1.13.1失败
我尝试通过以下Dockerfile基于apache/airflow:2.2.5-python3.9镜像安装PyTorch 1.13.1:
FROM apache/airflow:2.2.5-python3.9 ARG AIRFLOW_HOME_ARG=/opt/airflow ENV AIRFLOW_HOME=${AIRFLOW_HOME_ARG} # 适配macOS及其他Unix系统的镜像构建技巧 # 以root用户创建airflow用户组,再切换到airflow用户 USER root RUN groupadd -r airflow USER airflow # 将AIRFLOW_HOME下的所有目录加入Python导入路径 ENV PYTHONPATH ${AIRFLOW_HOME}:$PYTHONPATH # 安装额外依赖 COPY requirements.txt / RUN pip install --user --no-cache-dir -r /requirements.txt
requirements.txt内容:
pytorch==1.13.1
构建镜像时出现错误:
#15 1.263 ERROR: Could not find a version that satisfies the requirement pytorch~=1.13.1 (from versions: 0.1.2, 1.0.2)
#15 1.263 ERROR: No matching distribution found for pytorch~=1.13.1
我还尝试过以下方案:
- 使用airflow-2.7.1-python3.9镜像
- 使用airflow-2.7.1-python3.11镜像
我确认PyTorch官方仓库存在对应版本的wheel包,但无论如何尝试,pip仅能找到0.1.2、1.0.2版本。请问如何在apache/airflow:2.2.5-python3.9镜像中安装PyTorch 1.13.1?
解决方案
问题根源
- 包名错误:PyTorch官方在PyPI上的包名是
torch,而非pytorch——你看到的0.1.2、1.0.2版本是第三方上传的旧包,并非官方发布的PyTorch 1.13.1。 - 源的问题:PyTorch从1.2.0版本开始,官方不再将完整版本包上传至默认PyPI源,需要指定官方的wheel仓库地址才能获取对应版本。
具体操作步骤
1. 修改requirements.txt
将内容替换为(以CPU版本为例,若需CUDA版本请替换对应源地址):
torch==1.13.1 --index-url https://download.pytorch.org/whl/cpu
- 若需要CUDA 11.6版本的PyTorch,源地址改为
https://download.pytorch.org/whl/cu116 - 确保包名是
torch,而非pytorch
2. 调整Dockerfile中的pip安装命令(可选,若不想修改requirements.txt)
直接在pip命令中指定官方源:
RUN pip install --user --no-cache-dir torch==1.13.1 --index-url https://download.pytorch.org/whl/cpu
3. 确认架构匹配
- 如果你在Apple Silicon Mac上构建镜像,默认架构为aarch64,与你找到的对应wheel包架构匹配
- 若为x86_64架构,官方源会自动匹配对应版本的wheel,无需额外指定文件名
内容的提问来源于stack exchange,提问作者dmayilyan
相关产品推荐
相关产品推荐

