Python中pip安裝非PyPI官網(wǎng)第三方庫的方法
在python中安裝非自帶python模塊,有三種方式:
1.easy_install
2.pip
3.下載壓縮包(.zip, .tar, .tar.gz)后解壓, 進入解壓縮的目錄后執(zhí)行python setup.py install命令
本文主要針對pip安裝時可能會碰到的一種情況,及解決辦法:
假如我要安裝pylint模塊,該模塊非python自帶模塊,用import肯定不能導(dǎo)入,需要額外安裝
>>> import pylint
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pylint
【現(xiàn)象】
執(zhí)行pip install <modulename>命令,報錯如下:
D:\>pip install pylint --allow-external pylint
Downloading/unpacking pylint
Requirement already satisfied (use --upgrade to upgrade): six in c:\python27\lib\site-packages\six-1
.8.0-py2.7.egg (from pylint)
Downloading/unpacking astroid>=1.3.6 (from pylint)
Real name of requirement astroid is astroid
Could not find any downloads that satisfy the requirement astroid>=1.3.6 (from pylint)
Some insecure and unverifiable files were ignored (use --allow-unverified astroid to allow).
Cleaning up...
No distributions at all found for astroid>=1.3.6 (from pylint)
Storing debug log for failure in C:\Users\aaa\pip\pip.log
【分析】
在Perl中安裝新模塊,一般可以用PPM圖形化工具,也可以用CPAN來安裝,比如說: cpan>install Test::Class, 非常方便,不會碰到這種情況,這種情況主要是因為pip版本問題: pip最新的版本(1.5以上的版本), 出于安全的考
慮,pip不允許安裝非PyPI的URL,因為該安裝文件實際上來自pylint.org,因而導(dǎo)致上面的錯誤!
NOTE:
1. 可以在官方changelog里面查看更改的信息
2. 可以用pip --version來查看pip的版本信息
C:\>pip --version
pip 1.5.6 from C:\Python27\lib\site-packages (python 2.7)
【辦法】
針對上面的情況,既然這個問題是因為pip版本的原因,可以改用pip低一點的版本
方法一: 用pip 1.4版本,再執(zhí)行pip install pylint命令來安裝
方法二: 執(zhí)行命令時,加上--allow-all-external, --allow-unverified及依賴包版本(astroid==1.3.6)
pip install pylint --allow-all-external pylint astroid==1.3.6 --allow-unverified pylint
NOTE:
1. --allow-all-external # 允許所有外部地址的標(biāo)簽,只有打上該標(biāo)簽pip方可下載外部地址模塊
2. --allow-unverified # pip沒有辦法校驗外部模塊的有效性,所以必須同時打上該標(biāo)簽
3. astroid==1.3.6 # 依賴包必須要添加上,并賦予其版本號,pip方能從列表下載
方法三: 在當(dāng)前目錄下,新增requirements.txt,內(nèi)容如下:
# requirements.txt
--allow-all-external pylint
--allow-unverified pylint
pylint
--allow-all-external astroid==1.3.6
再執(zhí)行: pip install -r requirements.txt
【結(jié)論】
1. pip這個設(shè)計不夠友好,使用也很不方便,遠不如Perl中的PPM,期待Python中也有這么個工具。
2. 如果碰到這種錯,導(dǎo)致不能安裝模塊的話: 直接下載壓縮包安裝好了。 >>>下載包地址<<<
3. 執(zhí)行pip -h命令查看更新pip相關(guān)的幫助信息
Usage:
pip <command> [options]
Commands:
install Install packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
search Search PyPI for packages.
wheel Build wheels from your requirements.
zip DEPRECATED. Zip individual packages.
unzip DEPRECATED. Unzip individual packages.
bundle DEPRECATED. Create pybundles.
help Show help for commands.
General Options:
-h, --help Show help.
-v, --verbose Give more output. Option is additive, and can be used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output.
--log-file <path> Path to a verbose non-appending log, that only logs failures. This log is active by default at pip.log.
--log <path> Path to a verbose appending log. This log is inactive by default.
--proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port.
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
--cert <path> Path to alternate CA bundle.
相關(guān)文章
Python openpyxl庫處理Excel文件高級應(yīng)用技巧實例
openpyxl是一個用于處理Excel文件的Python庫,它提供了豐富的功能,使得用戶能夠輕松地讀取、寫入和操作Excel文件,而不需要依賴于Microsoft Excel軟件,作為一個開源項目,openpyxl在Python生態(tài)系統(tǒng)中得到了廣泛的應(yīng)用,成為處理Excel數(shù)據(jù)的首選工具之一2024-01-01Python3.9.0 a1安裝pygame出錯解決全過程(小結(jié))
這篇文章主要介紹了Python3.9.0 a1安裝pygame出錯解決全過程(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Python中的魔術(shù)方法Magic?Methods使用實例全面指南
在Python中,魔術(shù)方法Magic?Methods是一種特殊的方法,它們以雙下劃線開頭和結(jié)尾,如__init__、__str__等,這些方法允許定制類的行為,使得對象更具有靈活性和可定制性,本文將深入探討Python中一些常用的魔術(shù)方法,以及如何使用它們來定制類與對象2024-01-01Python使用matplotlib繪制多個圖形單獨顯示的方法示例
這篇文章主要介紹了Python使用matplotlib繪制多個圖形單獨顯示的方法,結(jié)合實例形式分析了matplotlib實現(xiàn)繪制多個圖形單獨顯示的具體操作技巧與注意事項,代碼備有較為詳盡的注釋便于理解,需要的朋友可以參考下2018-03-03