安裝 d2l 踩坑全記錄|d2l0.17.6 /d2l1.0.3 依賴沖突、pandas 編譯失敗、numpy 與 OpenCV 版本不兼容一站式解決)
最近在 Windows 環(huán)境安裝《動手學(xué)深度學(xué)習(xí)》配套庫 d2l先后嘗試教材配套穩(wěn)定版d2l0.17.6和新版d2l1.0.3踩了大量 Windows 平臺獨有的坑離線高版本 whl 依賴連鎖沖突、pandas 源碼編譯 ssize_t 未定義、VC 編譯工具失效、numpy 與 opencv 版本互斥、pip 參數(shù)報錯、conda 安裝仍觸發(fā)編譯等。本文整合所有報錯現(xiàn)象、成因、分層解決方案新手可直接對照排查。一、環(huán)境基礎(chǔ)說明系統(tǒng)Windows11Python 推薦版本3.93.10 及以上低版本庫預(yù)編譯包極少極易編譯失敗工具Anaconda Prompt、Visual C Build Tools 2022兩個 d2l 版本區(qū)別d2l0.17.6紙質(zhì)課本配套推薦學(xué)習(xí)使用硬性鎖定numpy1.21.5d2l1.0.3官網(wǎng)新版重構(gòu)版紙質(zhì)教材代碼大量失效鎖定numpy1.23.??????二、前置避坑準(zhǔn)則必看減少 90% 報錯不要混用項目環(huán)境YOLO、深度學(xué)習(xí)訓(xùn)練項目單獨創(chuàng)建虛擬環(huán)境不要和 d2l 學(xué)習(xí)環(huán)境共用優(yōu)先 conda 安裝 numpy/pandas/opencv 等帶 C 底層依賴庫pip 僅安裝純 Python 庫 d2l禁止離線手動下載 d2l 1.0 whl 本地安裝高版本依賴鎖死極易觸發(fā)連鎖沖突Windows 盡量避免源碼編譯 pandas老版本 pandas 與新版 MSVC 編譯器存在底層兼容 bug。三、踩坑問題合集 完整解決方案問題 1離線安裝 d2l-1.0.3 whl出現(xiàn) opencv 與 numpy 版本沖突報錯日志opencv-python 5.0.0.93 requires numpy2; python_version 3.9, but you have numpy 1.23.5 which is incompatible.原因d2l 1.0.3 強(qiáng)制numpy1.23.5opencv5.x 強(qiáng)制要求numpy2二者版本硬性互斥離線高版本 d2l 會自動拉取最新 opencv觸發(fā)依賴矛盾。解決辦法二選一方案使用 d2l1.0.3 則安裝 opencv4 兼容版放棄 opencv5pip uninstall d2l opencv-python numpy pandas -y pip cache purge pip install d2l1.0.3 opencv-python4.8.1.78 --only-binary :all: -i https://pypi.tuna.tsinghua.edu.cn/simple想用 opencv5 則不固定 d2l 版本自動適配 numpy2缺點是不兼容紙質(zhì)教材pip uninstall d2l opencv-python numpy pandas -y pip cache purge pip install opencv-python5.0.0.93 d2l --only-binary :all: -i https://pypi.tuna.tsinghua.edu.cn/simple問題 2安裝 d2l0.17.6 時報錯Failed building wheel for pandas報錯日志ERROR: Failed building wheel for pandas error: failed-wheel-build-for-install ╰─ pandas note: This error originates from a subprocess, and is likely not a problem with pip.原因Windows 缺少 C 編譯工具pip 無法編譯 pandas 源碼pip 緩存殘留之前 d2l1.0.3 高版本依賴強(qiáng)制拉取新版 pandas 源碼Python 版本過高低版本 pandas 無 Windows 預(yù)編譯二進(jìn)制包。最優(yōu)解決方案無需編譯工具刪除舊環(huán)境重建純凈環(huán)境conda 安裝預(yù)編譯依賴全程無編譯# 刪除損壞舊環(huán)境 conda remove -n d2l-learn --all -y # 新建python3.9專用環(huán)境 conda create -n d2l-learn python3.9 -y conda activate d2l-learn # 配置清華conda源 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --set show_channel_urls yes # conda預(yù)編譯依賴規(guī)避編譯 conda install numpy1.21.5 pandas1.3.5 opencv -y # 安裝教材配套d2l pip install d2l0.17.6 -i https://pypi.tuna.tsinghua.edu.cn/simple四、兩套完整可直接復(fù)制的純凈環(huán)境一鍵部署命令方案 A適配紙質(zhì)課本 d2l0.17.6推薦學(xué)習(xí)使用# 清理舊環(huán)境 conda remove -n d2l-book --all -y # 創(chuàng)建環(huán)境 conda create -n d2l-book python3.9 -y conda activate d2l-book # 配置conda國內(nèi)源 conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --set show_channel_urls yes # conda安裝預(yù)編譯底層依賴無編譯報錯 conda install numpy1.21.5 pandas1.3.5 opencv -y # 安裝教材d2l pip install d2l0.17.6 -i https://pypi.tuna.tsinghua.edu.cn/simple # 驗證環(huán)境 python import d2l,numpy,pandas,cv2 print(d2l.__version__) print(numpy.__version__) print(pandas.__version__) print(cv2.__version__)方案 B新版 d2l1.0.3最終自己使用了這個方法conda remove -n d2l-new --all -y conda create -n d2l-new python3.9 -y conda activate d2l-new pip uninstall d2l numpy pandas opencv-python -y pip cache purge pip install d2l1.0.3 opencv-python4.8.1.78 --only-binary :all: -i https://pypi.tuna.tsinghua.edu.cn/simple # 驗證 python import d2l,numpy,cv2 print(d2l.__version__,numpy.__version__,cv2.__version__)成功安裝