这篇文章主要介绍了python中pip安装非pypi官网第三方库的方法,pip最新的版本(1.5以上的版本), 出于安全的考虑,pip不允许安装非pypi的url,本文就给出两种解决方法,需要的朋友可以参考下5 u" X, s2 a. F' J% o9 l
在python中安装非自带python模块,有三种方式:
) ^( A \4 }& E. b3 m2 I 1.easy_install8 {$ U0 B$ n2 I1 [# b) W& k4 h
2.pip& k T; K) f* `* P% X
3.下载压缩包(.zip, .tar, .tar.gz)后解压, 进入解压缩的目录后执行python setup.py install命令
' [6 Z0 z i& `! i 本文主要针对pip安装时可能会碰到的一种情况,及解决办法:
2 f& m) _$ K8 K1 g 假如我要安装pylint模块,该模块非python自带模块,用import肯定不能导入,需要额外安装7 u [5 T) h: B! o( s
代码如下:
3 e) @- A- n0 E >>> import pylint
_/ I7 k( a3 A1 W9 t3 P traceback (most recent call last):5 x" C2 H6 t% V9 i' \" l7 a
file "", line 1, in
2 \! S7 N& {0 A( D( r/ `/ l importerror: no module named pylint
& b# b- ]- z4 g# B 【现象】. g0 |9 P1 l7 I
执行pip install 命令,报错如下:+ w$ b+ |& u- l9 [$ M
代码如下:
2 ~5 v( Y! C- i! S4 i" R, D d:\>pip install pylint --allow-external pylint) a; @0 r) e4 p
downloading/unpacking pylint( f+ @$ D2 n8 E6 ~3 [; v: W" @6 j4 ] ?% o/ c
requirement already satisfied (use --upgrade to upgrade): six in c:\python27\lib\site-packages\six-1
, P5 B. ?& g- v P g" R; L$ E .8.0-py2.7.egg (from pylint)
3 F2 d2 y/ y8 C! Q0 _ downloading/unpacking astroid>=1.3.6 (from pylint)
7 _$ o. C8 K) f3 [: ]$ I2 S/ S real name of requirement astroid is astroid( h; \' _! u+ ^5 `" h
could not find any downloads that satisfy the requirement astroid>=1.3.6 (from pylint)- i: e, C6 @# K* v9 C9 R2 c7 V
some insecure and unverifiable files were ignored (use --allow-unverified astroid to allow).) ^) [* H/ Z1 Q3 h
cleaning up...6 m2 _8 e* s, e8 R( w n
no distributions at all found for astroid>=1.3.6 (from pylint)
h: E6 h9 `7 V0 o storing debug log for failure in c:\users\aaa\pip\pip.log+ r3 }! K5 R: ]- a" c Y4 t* `
【分析】) g& X" v( U2 \( T {" z1 W! O9 e
在perl中安装新模块,一般可以用ppm图形化工具,也可以用cpan来安装,比如说: cpan>install test::class, 非常方便,不会碰到这种情况,这种情况主要是因为pip版本问题: pip最新的版本(1.5以上的版本), 出于安全的考
/ K, Z$ G! ], \$ Z- f5 m 虑,pip不允许安装非pypi的url,因为该安装文件实际上来自pylint.org,因而导致上面的错误!+ b& | c! _; t8 n
note:4 d, ]4 q+ k) d' }) | ~) `
1. 可以在官方changelog里面查看更改的信息
* E4 S* Z/ l% K; e6 u* F 2. 可以用pip --version来查看pip的版本信息
# d. R: d4 f' x$ C" U8 l 代码如下:8 ^) ^# a' v1 U+ b
c:\>pip --version
; Z6 h- A" R, o* h) T5 ^ pip 1.5.6 from c:\python27\lib\site-packages (python 2.7), t* E( k/ L0 v$ _' ~: \9 N
【办法】
$ T) b0 U! K8 f% s3 N 针对上面的情况,既然这个问题是因为pip版本的原因,可以改用pip低一点的版本
8 W: H* N5 s2 ? 方法一: 用pip 1.4版本,再执行pip install pylint命令来安装
: x. d+ M, n% a4 b5 R+ f 方法二: 执行命令时,加上--allow-all-external, --allow-unverified及依赖包版本(astroid==1.3.6)2 V. N# Z0 O9 b. [* j
代码如下:
! x9 ?: l0 r. W) G1 n1 O pip install pylint --allow-all-external pylint astroid==1.3.6 --allow-unverified pylint
! E% G7 W S3 T9 x note: ]0 B/ { A" w$ r$ _
1. --allow-all-external # 允许所有外部地址的标签,只有打上该标签pip方可下载外部地址模块
% M4 B5 @: z; {: S6 g 2. --allow-unverified # pip没有办法校验外部模块的有效性,所以必须同时打上该标签
' l3 v0 q4 l7 M3 } U9 L# y 3. astroid==1.3.6 # 依赖包必须要添加上,并赋予其版本号,pip方能从列表下载- J1 X. g5 J) a+ h4 B9 y
方法三: 在当前目录下,新增requirements.txt,内容如下:
. y6 c( `/ t' N+ H$ l 代码如下:5 v/ H$ N, c; |/ E% N" E1 N2 {
# requirements.txt {5 P( l! `0 ?3 t9 i& ~0 F4 u
--allow-all-external pylint
4 _; D" @0 W1 J' W --allow-unverified pylint
$ K: t5 ] @5 ] pylint
q3 S, V+ F5 B) ]4 J! m' E --allow-all-external astroid==1.3.6( r% C' |9 P( F5 O
再执行: pip install -r requirements.txt1 Z6 o8 K6 u& N9 `
【结论】
2 j6 i2 Y1 _) ^- ~+ r; z2 U 1. pip这个设计不够友好,使用也很不方便,远不如perl中的ppm,期待python中也有这么个工具。
2 v" [4 Z( n) {2 _ 2. 如果碰到这种错,导致不能安装模块的话: 直接下载压缩包安装好了。 >>>下载包地址<<</ e7 H! C; k" I- Z+ T- j% }
3. 执行pip -h命令查看更新pip相关的帮助信息
: v7 Y# X/ l: M0 P. L 代码如下:
9 r" r& p0 k* ~* _8 Q0 u usage:2 b) o% i% q. W* @9 s9 T: |
pip [options]
, l2 \& _& u& M& g: t2 ~ commands:
7 q5 C7 D+ {0 k. g5 R( f install install packages.: {- b0 E) x$ M, F7 Q) e5 Y/ f
uninstall uninstall packages., n& D3 P) z% `# l
freeze output installed packages in requirements format.
! p3 b. b8 N' ]1 s$ e list list installed packages. s9 `! k' O* X6 ^6 B; z) J& X8 L0 E
show show information about installed packages.
3 A5 j& R. h* K' f6 c3 e search search pypi for packages.
# l4 a( \) S8 P* U, C5 ? wheel build wheels from your requirements.2 B/ w- y, x* J$ l+ F" w. a/ w( k
zip deprecated. zip individual packages.
& q+ W8 }/ u* O unzip deprecated. unzip individual packages.
Q( O) p1 J* F3 O" O, x0 s bundle deprecated. create pybundles.
9 r% A+ ^* y% ~8 W* d help show help for commands./ Y/ p7 _! \$ |* g9 v
general options:
1 V( V2 q" k3 i$ b j -h, --help show help.
8 \5 [: d/ ^$ q* ?# @& Q5 b+ D -v, --verbose give more output. option is additive, and can be used up to 3 times.
- [* ?( Z" Q, I -v, --version show version and exit.- `; ?/ w0 `! ~; b& `) ?
-q, --quiet give less output.
" N5 d: t- Q2 I& Y! q: }) @3 s7 w --log-filepath to a verbose non-appending log, that only logs failures. this log is active by default at pip.log. ?: m0 s/ O! G$ a/ g5 A
--logpath to a verbose appending log. this log is inactive by default.
3 v8 H1 u6 \3 N$ M. m' t --proxyspecify a proxy in the form [user:passwd@]proxy.server:port.
3 v0 ?) U3 r/ l9 W% v" _ --timeout set the socket timeout (default 15 seconds).
" K5 R* H3 |# }4 z9 t9 K --exists-action default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
0 x* {9 s( y/ s; x( g --certpath to alternate ca bundle.* ^- l% v. [$ r9 @. [& M
更多技术文章信息请查看: 技术文章 |
|