Python開發:部分第三方庫無法在線安裝解決方法


前言:Python開發:Python2和Python3的共存和切換使用

 

一、問題如下:

  1、截圖:

  

  2、錯誤信息:  

  Could not find a version that satisfies the requirement re (from versions: )

  No matching distribution found for re

  3、翻譯:

  找不到滿足re要求的版本(來自版本: ) 找不到re的匹配分布

二、解決方法:

  1、采用國內鏡像則能夠提高安裝成功率並提速:

        
http://mirrors.aliyun.com/pypi/simple/   阿里雲
https://pypi.mirrors.ustc.edu.cn/simple/   中國科技大學
http://pypi.douban.com/simple/   豆瓣
https://pypi.tuna.tsinghua.edu.cn/simple/   清華大學
 

  使用方法:

         
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple re
 

  可能會出現如下問題:

  

  The repository located at pypi.tuna.tsinghua.edu.cn is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host pypi.tuna.tsinghua.edu.cn'.

  Could not find a version that satisfies the requirement re (from versions: )
  No matching distribution found for re

  linux 系統:

  在~/.pip/pip.conf (若沒有此文件自行創建文件夾要加“.”,表示是隱藏文件夾)中設置以下內容:

         
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
 

  Windows系統:

  直接在user目錄中創建一個pip目錄,如:C:\Users\lenovo\pip,新建文件pip.ini,設置以下內容:

        
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
   這種方法對一部分人有效,但是有一些還是無法解決。

  2、在網站上下載第三方庫的離線包,離線安裝:

  網上收集的集合網站:

    LINUX(主要提供Linux版本的后綴是".whl"和“.tar.gz”):

      https://pypi.org/

    Windows(主要提供Windows版本的后綴是".whl"):

      https://www.lfd.uci.edu/~gohlke/pythonlibs/#tensorflow

      https://pypi.org/simple/

  python的離線安裝,有時候由於不同模塊有很多依賴包,所以很容易出錯,在線安裝會自動安裝依賴包,所以一般不會出現安裝問題。

  離線安裝方法,".whl"文件安裝如下:

  此處以ujson為例:
    linux版本的安裝(默認文件在當前目錄下)

         
pip3 install ujson的whl文件名
 

    我沒有linux版本,這里只列一下代碼。

  Windows版本的安裝(默認文件在當前目錄下)

         
pip3 install ujson‑1.35‑cp36‑cp36m‑win32.whl
 

  

  在windows下安裝時,32位不能使用64位版本的:

  

  ujson-1.35-cp36-cp36m-win_amd64.whl is not a supported wheel on this platform.

  意思就是說whl名的命名不符合它給的規范

  在python中使用以下命令:

  32位:

         
import pip
print(pip.pep425tags.get_supported())

  

 

  64位:

         
import pip._internal
print(pip._internal.pep425tags.get_supported())
 

  結果如下:

    

  支持里有:('cp36', 'cp36m', 'win32'),我安裝的是32位的python。
  下載的whl名字是:ujson-1.35-cp36-cp36m-win_amd64.whl,這是無法安裝的,若改為:ujson-1.35-cp36-cp36m-win32.whl則可以安裝。如果是python2.7的,很可能庫里存在兼容性問題。

  將下載的文件重命名為:

  

  之后就是安裝成功

   

  可以看到在自己python路徑下的Lib\site-packages文件夾下,看到ujson文件夾已經存在:

  

  但是調用import ujson命令則會發現:

  

  報錯,無法import ujson,這說明即便將64位版本的whl改了命名規范安裝成功,也依舊無法正常使用。

  在這里這所以將這種方法列出來,是因為網上有一些帖子里說的可以將win_amd64改為win32使用,這樣是不行的!之前被網上的某些帖子帶到了錯路,在開發時才發現有問題。

  因此,要使用正確的與python版本對應的原裝版本。

  先使用命令卸載之前錯誤的ujson:

pip3 uninstall ujson

  卸載時選擇y:

  

  下載正確的win32版本:

  

  使用命令進行安裝:

pip3 install ujson-1.35-cp36-cp36m-win32.whl

  

  安裝完成后再測試:

import ujson

  

  此時,才是真正安裝成功。

  當然,如果自己電腦上使用的是64位的python,則要使用win_amd64的whl。

  還是推薦使用64位的,如果只用一些基本的操作,32位還夠用。但是比如說要做機器學習,所用到的tensorflow等,必須要64位才行:

  

  推薦使用離線的方式,在線的不一定靠譜,畢竟是國外網站,有時候一直連不上也是常事。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM