@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++ 編譯器在工作。