@TOC
1 问题描述
使用 pip
安装numpy
卡在下面最后一行的阶段:
Collecting numpy==1.26.4 (from -r requirements.txt (line 2))Using cached https://mirrors.aliyun.com/pypi/packages/65/6e/09db70a523a96d25e115e71cc56a6f9031e7b8cd166c1ac8438307c14058/numpy-1.26.4.tar.gz (15.8 MB)Installing build dependencies ... doneGetting requirements to build wheel ... doneInstalling backend dependencies ... donePreparing metadata (pyproject.toml) ./
2 问题分析
各行输出的含义见下面的注释:
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/ # 使用阿里云镜像源加速下载
Collecting numpy==1.26.4 (from -r requirements.txt (line 2)) # 开始下载 numpy 库(指定版本1.26.4)Using cached https://mirrors.aliyun.com/pypi/packages/.../numpy-1.26.4.tar.gz (15.8 MB) # 从镜像源获取库文件Installing build dependencies ... done # 安装编译所需的依赖(如C/C++工具链)Getting requirements to build wheel ... done # 准备构建 wheel 包(二进制安装包)Installing backend dependencies ... done # 安装构建工具的依赖(如 setuptools、wheel)Preparing metadata (pyproject.toml) ... \ # 正在解析库的元数据(可能处于等待或处理中)
从终端日志来看,安装numpy时出现了编译错误,这是因为系统中缺少C编译器。错误信息显示无法找到任何C编译器(如cl、gcc等),导致numpy无法完成编译安装。
3 解决方法
-
安装Microsoft Visual C++ Build Tools,这是Windows系统推荐的Python C扩展编译工具
-
下载地址: Microsoft Visual C++ Build Tools
-
安装Visual Studio,并选择相应加载项
参考https://learn.microsoft.com/zh-cn/cpp/build/vscpp-step-0-installation?view=msvc-170
-
安装完成后,重新运行setup_virtual_env.bat脚本即可正常安装numpy依赖
注意,编译过程较长,需要在
Preparing metadata
阶段等待数分钟,可以在任务管理器看到 C++ 编译器在工作。