这篇文章主要介绍了python中pip安装非pypi官网第三方库的方法,pip最新的版本(1.5以上的版本), 出于安全的考虑,pip不允许安装非pypi的url,本文就给出两种解决方法,需要的朋友可以参考下
& ~4 d2 y$ \# {2 E4 K 在python中安装非自带python模块,有三种方式:0 P5 i! Y' m! N D4 m2 f
1.easy_install0 Y! D( n$ j4 H9 w7 h
2.pip
5 {) k, c% P5 [) ?0 C 3.下载压缩包(.zip, .tar, .tar.gz)后解压, 进入解压缩的目录后执行python setup.py install命令
) j1 P: G, c2 ~* o/ [* p# f 本文主要针对pip安装时可能会碰到的一种情况,及解决办法:# c1 f# _* M, M- `
假如我要安装pylint模块,该模块非python自带模块,用import肯定不能导入,需要额外安装+ K$ D# l1 {8 h+ s
代码如下:) [ l& q. t+ ~, x$ b. T l, t( k
>>> import pylint" M9 k, ~& @6 a0 I# F' E# m$ v
traceback (most recent call last):1 c, w3 @- J$ |" |- s4 p
file "", line 1, in
% b: i) r) \% x7 M7 H0 X$ x( [; Z importerror: no module named pylint
9 V* M$ ]5 F, J; U8 Q ]' {" l% k 【现象】
1 l; Y( H8 c; r( p; w7 H 执行pip install 命令,报错如下:
2 w# d: P! I/ G. C U 代码如下:; V. m9 J) P8 p; P5 C7 d
d:\>pip install pylint --allow-external pylint
6 y. G5 |, t2 y- P( u% \4 s0 O8 C downloading/unpacking pylint5 l3 y7 K0 y) D3 `( z% G* i& u2 X
requirement already satisfied (use --upgrade to upgrade): six in c:\python27\lib\site-packages\six-14 n0 v/ P) n: b
.8.0-py2.7.egg (from pylint)( B/ V5 F/ I5 j! s
downloading/unpacking astroid>=1.3.6 (from pylint). q1 z* N' Q6 t% b$ D* q( s0 f" d' S
real name of requirement astroid is astroid/ w* Q% k; D4 p; l
could not find any downloads that satisfy the requirement astroid>=1.3.6 (from pylint)
3 l) t, i! M! i$ j1 ?( T2 l some insecure and unverifiable files were ignored (use --allow-unverified astroid to allow).
& P- }) W8 X1 v" [ cleaning up..." W3 V( V7 t9 q- W; Y: h. l
no distributions at all found for astroid>=1.3.6 (from pylint)1 [; \9 e0 g( f* y
storing debug log for failure in c:\users\aaa\pip\pip.log, u6 y- e# Q( J. \# {
【分析】: S; c3 q) @+ D+ i* t1 q% X3 n2 p
在perl中安装新模块,一般可以用ppm图形化工具,也可以用cpan来安装,比如说: cpan>install test::class, 非常方便,不会碰到这种情况,这种情况主要是因为pip版本问题: pip最新的版本(1.5以上的版本), 出于安全的考
; d2 t* \ \4 L( c 虑,pip不允许安装非pypi的url,因为该安装文件实际上来自pylint.org,因而导致上面的错误!- w* M0 C+ C7 u& {: {$ n0 L O) E
note:% _1 V0 l7 W' V1 H
1. 可以在官方changelog里面查看更改的信息2 y" G! i6 t0 V1 g% v4 M s0 Z
2. 可以用pip --version来查看pip的版本信息
$ `- _$ K- w; m 代码如下:
6 c3 M* i* J/ T/ x c:\>pip --version
# `9 j9 v3 O" V9 K- ? pip 1.5.6 from c:\python27\lib\site-packages (python 2.7)
* ^" G$ l5 N- |2 y# c 【办法】
# _9 Q1 [1 y& R' d+ k7 L: g 针对上面的情况,既然这个问题是因为pip版本的原因,可以改用pip低一点的版本' a8 X& W% w' J: K( u
方法一: 用pip 1.4版本,再执行pip install pylint命令来安装
2 u0 c% I( C* j# n+ o. V) Z' q 方法二: 执行命令时,加上--allow-all-external, --allow-unverified及依赖包版本(astroid==1.3.6); d, @# I- ?) u- h" j
代码如下:2 ~' _. C$ k0 o) M: v2 _3 I
pip install pylint --allow-all-external pylint astroid==1.3.6 --allow-unverified pylint4 o/ e) Z2 h% X) N4 a. W _
note:* {4 y2 r% x, v; V) ~3 b5 J
1. --allow-all-external # 允许所有外部地址的标签,只有打上该标签pip方可下载外部地址模块) v& {9 }* O; E& F6 O. \# [
2. --allow-unverified # pip没有办法校验外部模块的有效性,所以必须同时打上该标签
' N( k( R3 V# T! b6 T 3. astroid==1.3.6 # 依赖包必须要添加上,并赋予其版本号,pip方能从列表下载1 E8 Z! ~+ l' m% Z0 h
方法三: 在当前目录下,新增requirements.txt,内容如下:0 Q5 j$ j. ?4 I$ j. s$ |- e8 i+ d/ {
代码如下:% \- R" S4 a3 e' r4 z
# requirements.txt2 @ f5 S" @( U# d+ e5 p( k
--allow-all-external pylint
, e% V$ m) B. N' S `) }0 a. [' f --allow-unverified pylint2 r: b; W# _! a; S; G) C
pylint% H: [/ Y$ I* ?
--allow-all-external astroid==1.3.6" Z& |8 e% a2 L: N
再执行: pip install -r requirements.txt0 M( S: h6 | B' l
【结论】
) N; t5 o& F" {, K 1. pip这个设计不够友好,使用也很不方便,远不如perl中的ppm,期待python中也有这么个工具。4 f2 W0 u; r4 l1 Y
2. 如果碰到这种错,导致不能安装模块的话: 直接下载压缩包安装好了。 >>>下载包地址<<<6 f, }, j( X2 \6 `3 c9 S; P2 h
3. 执行pip -h命令查看更新pip相关的帮助信息
6 H1 s& D+ u# _) P3 J 代码如下:
5 s0 ~# ?$ A7 |5 m+ P/ G usage:
* T. J* Q' d0 [ pip [options]
1 ~5 r" V* A- l+ R commands:0 ~' c0 ^0 p6 r4 Q7 b1 Z
install install packages.2 ]# N8 M- e; M& I
uninstall uninstall packages.
" J" q: } J9 b7 B# h5 \ freeze output installed packages in requirements format.
5 C9 I: y4 d$ J+ U9 v0 B" w list list installed packages.
' E$ D" I2 s1 @2 J show show information about installed packages.' k& I3 V4 i% Y0 _
search search pypi for packages.( J" F( r. l( o# U; X7 A
wheel build wheels from your requirements.: c+ @1 | n$ R( _
zip deprecated. zip individual packages.5 \4 H* E# Z3 U" R
unzip deprecated. unzip individual packages., B% l# o: _; r' X2 R& ~
bundle deprecated. create pybundles.
" W; a- \% D: e7 }: r7 |& K& A/ q help show help for commands.' U r) ~* G7 E
general options:8 H$ a- R+ k# i6 _
-h, --help show help.: _3 @7 x1 W& ]0 s5 H9 U
-v, --verbose give more output. option is additive, and can be used up to 3 times.& m# o+ z: p, d' E8 R3 _) r9 `
-v, --version show version and exit.
9 Z u1 s1 a8 ] -q, --quiet give less output./ x# s9 {2 d/ R' Z$ A
--log-filepath to a verbose non-appending log, that only logs failures. this log is active by default at pip.log.
# ?- B. c) R! l+ o0 Q, _6 C --logpath to a verbose appending log. this log is inactive by default.
/ Z) G& g' `# ?5 |- c --proxyspecify a proxy in the form [user:passwd@]proxy.server:port.' B5 ^8 A8 F6 Y% V0 E8 k
--timeout set the socket timeout (default 15 seconds).# o, e7 Y$ U8 V" ^) U3 c$ J2 T s
--exists-action default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
1 t7 K% h7 [3 Z. B8 o: m( b) Q --certpath to alternate ca bundle.( s) [8 |: b+ c) z4 O5 c7 \- u
更多技术文章信息请查看: 技术文章 |
|