標籤:python 靜態程式碼分析工具
在python中安裝非內建python模組,有三種方式:
- easy_install
- pip
- 下載壓縮包(.zip, .tar, .tar.gz)後解壓, 進入解壓縮的目錄後執行python setup.py install命令
本文主要針對pip安裝時可能會碰到的一種情況,及解決辦法:
假如我要安裝pylint模組,該模組非python內建模組,用import肯定不能匯入,需要額外安裝
>>> import pylintTraceback (most recent call last): File "<stdin>", line 1, in <module>ImportError: No module named pylint
【
現象】
執行pip install <modulename>命令,報錯如下:
D:\>pip install pylint --allow-external pylintDownloading/unpacking pylintRequirement 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,因而導致上面的錯誤!
NOTE:
1. 可以在官方changelog裡面查看更改的資訊
2. 可以用pip --version來查看pip的版本資訊
C:\>pip --versionpip 1.5.6 from C:\Python27\lib\site-packages (python 2.7)
【辦法】
針對上面的情況,既然這個問題是因為pip版本的原因,可以改用pip低一點的版本
方法一: 用pip 1.4版本,再執行pip install pylint命令來安裝
方法二: 執行命令時,加上--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 # 允許所有外部地址的標籤,只有打上該標籤pip方可下載外部地址模組
2. --allow-unverified # pip沒有辦法校正外部模組的有效性,所以必須同時打上該標籤
3. astroid==1.3.6 # 依賴包必須要添加上,並賦予其版本號碼,pip方能從列表下載
方法三: 在目前的目錄下,新增requirements.txt,內容如下:
# requirements.txt--allow-all-external pylint--allow-unverified pylintpylint--allow-all-external astroid==1.3.6
再執行: pip install -r requirements.txt
【結論】
1. pip這個設計不夠友好,使用也很不方便,遠不如Perl中的PPM,期待Python中也有這麼個工具。
2. 如果碰到這種錯,導致不能安裝模組的話: 直接下載壓縮包安裝好了。 >>>下載包地址<<<
3. 執行pip -h命令查看更新pip相關的協助資訊
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:[email protected]]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.
Python中如何用pip安裝外部主機檔案