iT邦幫忙

0

解決venv中無法安裝numpy的問題(Could not build wheels for numpy which use PEP 517)

懶人包

在venv下,執行

pip install --upgrade pip

當上述指令失敗時,執行

easy_install -U pip

事發經過

在venv下安裝numpy的時候,出現Could not build wheels for numpy which use PEP 517 and cannot be installed directly。
https://ithelp.ithome.com.tw/upload/images/20210714/20139482LTnje3cI81.jpg

爬文後得知是pip版本的問題,原先pip的版本是19.0.3
https://ithelp.ithome.com.tw/upload/images/20210714/201394823thjYJBnzv.jpg

於是更新pip,執行

pip install --upgrade pip

但執行完卻出現Could not install packages due to an EnvironmentError: [WinError 5] 存取被拒。
https://ithelp.ithome.com.tw/upload/images/20210714/20139482SrLKcXypar.jpg

爬文後得知在pip install --upgrade pip 後面加上--user可以解決此狀況,所以執行

pip install --upgrade pip --user

這時候卻出現Cannot open D:\python projects\install_numpy\venv\Scripts\pip3-script.py
https://ithelp.ithome.com.tw/upload/images/20210714/20139482cULSNtL0Q0.jpg
原來是在執行pip install --upgrade pip時,原本的pip被刪出了,導致目前的venv中沒有pip。

這時只好重新刪除原先的venv,再建立一個新的venv,再執行一次

pip install --upgrade pip --user

這時出現Successfully installed pip-21.1.3,看似是更新成功了
https://ithelp.ithome.com.tw/upload/images/20210714/201394826jDbmynldD.jpg

這時再度嘗試安裝numpy,執行

pip install numpy

但又再次出現Could not build wheels for numpy which use PEP 517 and cannot be installed directly。
https://ithelp.ithome.com.tw/upload/images/20210714/20139482LTnje3cI81.jpg

奇怪,怎麼pip已經更新了,還是出現一樣的錯誤?

這時再確認一次pip的版本,發現還是19.0.30
https://ithelp.ithome.com.tw/upload/images/20210714/201394822ALQaJOexX.jpg
原來--user會讓pip將package安裝在user目錄下的資料夾,不會安裝在venv內,這時候只好想別的辦法來更新pip。

統整一下,到目前為止我們試過了

pip install --upgrade pip
pip install --upgrade pip --user

兩者都無法更新venv內的pip

爬文後得知也可以用easy_install更新pip
於是執行

easy_install -U pip

https://ithelp.ithome.com.tw/upload/images/20210714/20139482wUfit4v8mP.jpg
沒有任何紅字出現,應該是成功了

這時執行

pip -V

確認pip版本更新到21.1.3
https://ithelp.ithome.com.tw/upload/images/20210714/20139482Yf4iHq36D8.jpg

再次嘗試安裝numpy,執行

pip install numpy

https://ithelp.ithome.com.tw/upload/images/20210714/20139482USZHAXp5Oj.jpg

終於安裝成功了,謝天謝地

結論

  1. Could not build wheels for package which use PEP 517 and cannot be installed directly原因出在pip版本太舊。
  2. 透過pip install --update pip可以更新pip,但如果出現Access Denied的時候,可以透過easy_install -U pip 更新pip。
  3. --user會將package安裝在user目錄底下,導致venv下的package沒有變化。

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言