Docker构建Django应用时安装依赖出现numpy相关警告求助
解决Docker构建Django应用时的numpy依赖警告/安装问题
错误信息
Running from numpy source directory.
#7 83.21 setup.py:480: UserWarning: Unrecognized setuptools command, proceeding with generating Cython sources and expanding templates
#7 83.21 run_build = parse_setuppy_commands()
你的依赖与配置
requirements.txt
-f https://download.pytorch.org/whl/torch_stable.html Django==4.0.6 fastbook==0.0.26 gunicorn==20.1.0 Pillow==9.2.0 torch==1.12.0+cpu torchvision==0.13.0+cpu
Dockerfile
#Python and Linux Version FROM python:3.9-alpine3.15 COPY requirements.txt /app/requirements.txt #Configure server RUN set -ex \ && pip install --upgrade pip \ && pip install --no-cache-dir -r /app/requirements.txt #Working directory WORKDIR /app ADD . . EXPOSE 8000 CMD ["gunicorn", "--bind", ":8000","--workers", "3", "hongOS_project.wsgi:application"]
问题根源
Alpine镜像采用musl libc而非常见的glibc,绝大多数PyPI包的预编译轮子是针对glibc构建的。fastbook依赖numpy,而numpy在Alpine上无法直接使用预编译包,必须从源码编译——但你的Dockerfile里缺少编译numpy、Pillow等包所需的系统工具与依赖库,因此出现上述警告,甚至可能导致后续安装失败。
修复方案
修改Dockerfile,先安装Alpine系统级编译依赖,再执行pip安装:
#Python and Linux Version FROM python:3.9-alpine3.15 # 安装编译numpy、Pillow等所需的系统依赖 RUN apk add --no-cache gcc musl-dev linux-headers python3-dev jpeg-dev zlib-dev COPY requirements.txt /app/requirements.txt #Configure server RUN set -ex \ && pip install --upgrade pip \ && pip install --no-cache-dir -r /app/requirements.txt #Working directory WORKDIR /app ADD . . EXPOSE 8000 CMD ["gunicorn", "--bind", ":8000","--workers", "3", "hongOS_project.wsgi:application"]
说明
gcc musl-dev linux-headers:提供编译C/C++扩展所需的工具链,是numpy源码编译的必要条件python3-dev:提供Python开发头文件,用于编译Python扩展jpeg-dev zlib-dev:满足Pillow处理图片格式的依赖需求
添加这些依赖后,numpy等包就能正常从源码编译安装,警告会消失,构建过程也能顺利完成。
内容的提问来源于stack exchange,提问作者pabsanort2
相关产品推荐
相关产品推荐

