这篇文章主要介绍了python中pip安装非pypi官网第三方库的方法,pip最新的版本(1.5以上的版本), 出于安全的考虑,pip不允许安装非pypi的url,本文就给出两种解决方法,需要的朋友可以参考下
) ]& \/ q# E- b! g9 S3 l/ @; ^ 在python中安装非自带python模块,有三种方式:% H& T* ^' d) l3 v. I$ m6 I- M
1.easy_install% [& r1 j! W* o: L$ a
2.pip* X: M f3 ]& z3 n
3.下载压缩包(.zip, .tar, .tar.gz)后解压, 进入解压缩的目录后执行python setup.py install命令8 {( V9 y9 O5 G. c1 D! E( G1 p, }+ c
本文主要针对pip安装时可能会碰到的一种情况,及解决办法:4 X7 |6 ~# F* C% h5 E3 D
假如我要安装pylint模块,该模块非python自带模块,用import肯定不能导入,需要额外安装; l$ q$ U$ c/ D/ r- F
代码如下:: y, g' Z, r. I" V0 S b) k2 M
>>> import pylint d- f) ~' J) J$ W
traceback (most recent call last):' f( h/ r7 g0 V1 e2 U' x' X( F& j. L
file "", line 1, in
+ a; G! q/ v3 B; T importerror: no module named pylint
# o: ^- P0 _( ^) P3 U" b- o 【现象】2 I# h8 ?* i( ~. ^6 C F4 N+ {
执行pip install 命令,报错如下:
# e8 `1 x& b6 ~4 B 代码如下:
8 _, {9 T* Z! T4 i0 v+ P4 m/ M d:\>pip install pylint --allow-external pylint
* G8 m1 v' o6 i1 V h5 r4 v5 }: k downloading/unpacking pylint6 A6 D' u. y4 @: Y7 V. F" X
requirement already satisfied (use --upgrade to upgrade): six in c:\python27\lib\site-packages\six-1
% Z3 P; Z+ v$ q' w/ r .8.0-py2.7.egg (from pylint)
3 d* {6 P* z @, }7 s n downloading/unpacking astroid>=1.3.6 (from pylint)
6 M$ F1 s x! ?: @ real name of requirement astroid is astroid" y5 j! u7 Y% T8 N2 z
could not find any downloads that satisfy the requirement astroid>=1.3.6 (from pylint)
) M; Z- ^% c) c9 P$ A/ S% l some insecure and unverifiable files were ignored (use --allow-unverified astroid to allow).
# n3 _- T7 G* g3 |* \ cleaning up...3 j7 \1 M/ L9 @2 Z1 v& u$ N
no distributions at all found for astroid>=1.3.6 (from pylint)8 @ N+ t8 {$ w3 v, r
storing debug log for failure in c:\users\aaa\pip\pip.log/ _( l0 j P$ o, t3 Q7 p9 x. ~$ O
【分析】
# Y" P6 X& ~1 a' G! U1 m( _ 在perl中安装新模块,一般可以用ppm图形化工具,也可以用cpan来安装,比如说: cpan>install test::class, 非常方便,不会碰到这种情况,这种情况主要是因为pip版本问题: pip最新的版本(1.5以上的版本), 出于安全的考
1 J5 b2 x, k2 ?- E$ Y2 J1 p# Q( j/ U 虑,pip不允许安装非pypi的url,因为该安装文件实际上来自pylint.org,因而导致上面的错误!" A8 l; J5 D/ E5 M3 ], m3 z
note:9 }7 A3 l# ~1 A
1. 可以在官方changelog里面查看更改的信息
$ ]5 U! B# O' Q+ Y 2. 可以用pip --version来查看pip的版本信息/ }8 K: Z+ q; V. \
代码如下:7 j: o6 _% B( h# ] }5 }$ |1 ~
c:\>pip --version$ N. g- U) h2 b
pip 1.5.6 from c:\python27\lib\site-packages (python 2.7)5 d) [! U. j0 e- \ k
【办法】
0 l% l+ [- q, Q 针对上面的情况,既然这个问题是因为pip版本的原因,可以改用pip低一点的版本
7 [6 w! ?1 |: R7 @ 方法一: 用pip 1.4版本,再执行pip install pylint命令来安装
% O% N9 ~8 u% C. x) i* {' H: I 方法二: 执行命令时,加上--allow-all-external, --allow-unverified及依赖包版本(astroid==1.3.6)# {5 c3 Y9 V) B8 G: h) y/ T
代码如下:
. g! w, Q# w- A) v9 D( e+ H1 a pip install pylint --allow-all-external pylint astroid==1.3.6 --allow-unverified pylint4 g7 K% `. _3 t
note:
0 z+ F5 g' X' X0 Q7 g% J 1. --allow-all-external # 允许所有外部地址的标签,只有打上该标签pip方可下载外部地址模块
0 ~2 d. r3 ^! B- h 2. --allow-unverified # pip没有办法校验外部模块的有效性,所以必须同时打上该标签
$ Y- t, o; M7 L5 s0 n+ P) Z 3. astroid==1.3.6 # 依赖包必须要添加上,并赋予其版本号,pip方能从列表下载+ j, w, y7 A2 H3 G- ^! o
方法三: 在当前目录下,新增requirements.txt,内容如下:. V- U2 ^" j( z) {
代码如下:7 @. ?/ y; O% }( I! Z" g( a; U
# requirements.txt: F1 V. G, R! [8 {- `5 C6 w" Y
--allow-all-external pylint, M( W" {0 x+ }7 X; \2 r3 v+ q! z
--allow-unverified pylint2 i B H5 g( k# Y
pylint$ d& K4 }3 }2 Y3 U
--allow-all-external astroid==1.3.6: |: \! `0 A1 s( k: t* _
再执行: pip install -r requirements.txt
1 E& B, W$ B N, G, ~ 【结论】
1 y6 N/ Q. s! j) S 1. pip这个设计不够友好,使用也很不方便,远不如perl中的ppm,期待python中也有这么个工具。
2 D4 r8 @) d9 |7 X: Y% [: E 2. 如果碰到这种错,导致不能安装模块的话: 直接下载压缩包安装好了。 >>>下载包地址<<<! H( h* Y7 y. h
3. 执行pip -h命令查看更新pip相关的帮助信息
0 N( ]+ G& E6 J2 N" \ 代码如下:6 M- W. T8 x7 D' e) o
usage:
2 O L9 V; k; G% n% j- I pip [options]- a+ K4 K7 ]; U8 a0 Y" Q7 W
commands: D2 A) z n6 E0 R8 m/ {
install install packages.
0 D6 B% U. T6 w1 t2 l- b+ a uninstall uninstall packages.
+ K: y$ N7 ^8 ]( s# o, R! M7 U+ g9 t/ Y freeze output installed packages in requirements format.9 i! _+ v6 N w* P# K' z0 K
list list installed packages./ D6 X3 p3 U1 Z& ^
show show information about installed packages.( s9 Y( R1 R8 F; v8 R5 C7 M
search search pypi for packages.& O3 a% A' s2 m* I+ d# i
wheel build wheels from your requirements.; T1 Y# O/ D$ @( j
zip deprecated. zip individual packages.' e# I" _% W- B" U
unzip deprecated. unzip individual packages.
1 i5 i9 m7 v7 x1 R8 h% X bundle deprecated. create pybundles.3 L6 Q2 P$ f0 D9 u. a
help show help for commands.
* s) [: t, g5 D general options:- ]! G: J- p: v+ E
-h, --help show help.+ M0 n" `4 o' _# n/ f
-v, --verbose give more output. option is additive, and can be used up to 3 times.* }5 T! R0 r6 z$ }2 w
-v, --version show version and exit.# f* M5 D- S2 t
-q, --quiet give less output.0 c& {1 {( K, X# Z
--log-filepath to a verbose non-appending log, that only logs failures. this log is active by default at pip.log.( `) J# H/ s! Y0 ]% `$ i; x
--logpath to a verbose appending log. this log is inactive by default.' l/ Q* b) ]. O5 \- [
--proxyspecify a proxy in the form [user:passwd@]proxy.server:port.
$ `- y" g* E: u8 a, B* t --timeout set the socket timeout (default 15 seconds).# n/ x) L* q9 `1 _ K! Z) z
--exists-action default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
6 A, H/ D3 X* `' A. b' V( ` --certpath to alternate ca bundle.
) F- R7 {. ]7 c- l6 o* U更多技术文章信息请查看: 技术文章 |
|