pip install -r requirements.txt触发源码构建

问题

前几天基于dockerfile/requirementst.txt构建还是正常的,今天构建提示有源码构建动作。

  • debian11

分析

核心原因不是 requirements.txt 里的 numpy==2.3.5 没生效,而是 scikit_learn==1.9.0 触发了 scipy 的构建隔离环境,pip在这个临时环境里重新解析了 NumPy 依赖

日志已经把这个过程暴露得很清楚:

1
2
3
4
scikit_learn==1.9.0
└── scipy>=1.10.0
└── numpy<2.8,>=2.0.0
└── 下载 numpy-2.5.3.tar.gz

这里的 numpy-2.5.3.tar.gz 不是在安装你的主环境 NumPy,而是在:

1
Installing build dependencies ...

阶段,为 构建 scipy==1.18.1 准备的隔离 build environment。


解决

方案A: 预先numpy安装好,再禁止build isolation

1
2
3
4
5
6
7
8
9
10
11
12
13
# 1.预先安装numpy
pip install -i http://pypi.xxx.internal/root/public/ \
--trusted-host pypi.xxx.internal \
"numpy==2.3.5"

# 2.确认
python -c "import numpy; print(numpy.__version__)"

# 3.取消隔离构建
pip install -i http://pypi.zzz.internal/root/public/ \
--trusted-host pypi.zzz.internal \
--no-build-isolation \
-r requirements.txt

这样 scipy 如果仍然需要从源码构建,它就不会再创建那个独立的 build environment,也不会在那里偷偷装:

1
numpy==2.5.3

方案B:协议依赖版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 新增写死版本 scipy==1.17.1
numpy==2.4.6
transformers==4.53.1
litserve==0.2.16
onnxruntime-gpu==1.19.2
openpyxl==3.1.5
pydantic==2.13.4
PyYAML==6.0.3
Requests==2.34.2
scikit_learn==1.9.0
tensorboardx==2.6.5
tqdm==4.66.2
uvicorn==0.52.1
loguru==0.7.3
1
2
3
pip install -r requirements.txt \
-i http://pypi.xxx.internal/root/public/ \
--trusted-host pypi.xxx.internal