编译时找不到满足torch要求的版本

0 投票
1 回答
43 浏览
提问于 2025-04-13 19:27

我在一台Mac mini M2上运行了一段代码,这段代码需要用到torch和torch audio。在VSCode中测试时一切正常,但当我把它放到Docker里编译时却出现了错误。有没有人知道怎么解决这个问题?我在网上找到了一些关于Python版本的帖子,我尝试过更改版本(3.10、3.11),甚至还使用了一个较低版本的torch(1.8.0),但这些都没有任何改善。

谢谢。

Dockerfile

FROM --platform=linux/arm64 python:3.9-alpine3.15

# Upgrade pip
RUN python -m pip install --upgrade pip

# Install system dependencies
RUN apk add --no-cache build-base \
    && apk add --no-cache ffmpeg-dev ffmpeg \
    && apk add --no-cache pkgconfig

# Copy the project code
ADD . /code
WORKDIR /code

# Install Python dependencies from requirements.txt
RUN pip install -r requirements.txt

# Expose the necessary port
EXPOSE 80

# Define the command to run the application
CMD python app.py

requirements.txt

flask
requests
torch==2.0.0
torchaudio==2.0.1

错误:

Building 25.2s (11/11) FINISHED                                                                                                                     docker:desktop-linux
 => [web internal] load build definition from Dockerfile                                                                                                                0.0s
 => => transferring dockerfile: 559B                                                                                                                                    0.0s
 => [web internal] load metadata for docker.io/library/python:3.9-alpine3.15                                                                                            0.7s
 => [web auth] library/python:pull token for registry-1.docker.io                                                                                                       0.0s
 => [web internal] load .dockerignore                                                                                                                                   0.0s
 => => transferring context: 2B                                                                                                                                         0.0s
 => [web 1/6] FROM docker.io/library/python:3.9-alpine3.15@sha256:86d5546236a8f8dd6505a92c40db8a01f8c22b7425f32396b6bfe6ef4bd18230                                      0.0s
 => [web internal] load build context                                                                                                                                   3.5s
 => => transferring context: 8.42MB                                                                                                                                     3.2s
 => CACHED [web 2/6] RUN python -m pip install --upgrade pip                                                                                                            0.0s
 => CACHED [web 3/6] RUN apk add --no-cache build-base     && apk add --no-cache ffmpeg-dev ffmpeg     && apk add --no-cache pkgconfig                                  0.0s
 => [web 4/6] ADD . /code                                                                                                                                              19.1s
 => [web 5/6] WORKDIR /code                                                                                                                                             0.1s
 => ERROR [web 6/6] RUN pip install -r requirements.txt                                                                                                                 1.7s
------                                                                                                                                                                       
 > [web 6/6] RUN pip install -r requirements.txt:                                                                                                                            
1.239 Collecting flask (from -r requirements.txt (line 1))                                                                                                                   
1.416   Downloading flask-3.0.2-py3-none-any.whl.metadata (3.6 kB)                                                                                                           
1.461 Collecting requests (from -r requirements.txt (line 4))                                                                                                                
1.490   Downloading requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)

1.610 ERROR: Could not find a version that satisfies the requirement torch==2.0.0 (from versions: none)
1.610 ERROR: No matching distribution found for torch==2.0.0

1 个回答

1

问题在于你使用了Alpine这个基础镜像,这个镜像不太适合安装Torch。

有几个选择:

选择1:使用Debian基础镜像

FROM python:3.9

WORKDIR /code
ADD . .

RUN pip install -r requirements.txt

EXPOSE 80

CMD python app.py

最简单的 requirements.txt 文件:

torch==2.0.0
torchaudio==2.0.1

选择2:使用PyTorch基础镜像

在这种情况下,requirements.txt 文件里不需要包含 torchtorchaudio,因为它们已经包含在基础镜像里了。

FROM pytorch/pytorch:2.0.0-cuda11.7-cudnn8-runtime

WORKDIR /code
ADD . .

RUN pip install -r requirements.txt

EXPOSE 80

CMD python app.py

撰写回答