这篇文章主要介绍了python中pip安装非pypi官网第三方库的方法,pip最新的版本(1.5以上的版本), 出于安全的考虑,pip不允许安装非pypi的url,本文就给出两种解决方法,需要的朋友可以参考下1 Y9 ~% s" [) \. D; U+ Y) @# u
在python中安装非自带python模块,有三种方式:5 J* v' I( ^5 q9 F# f1 o6 P
1.easy_install4 V( ^# Y$ F* ^% h/ Q* J
2.pip
# o5 c8 |; r3 b# E; A- h2 r* d+ G 3.下载压缩包(.zip, .tar, .tar.gz)后解压, 进入解压缩的目录后执行python setup.py install命令
7 E( ]; R6 a2 ` \5 D 本文主要针对pip安装时可能会碰到的一种情况,及解决办法:
& J( |2 d) a! _; @! d 假如我要安装pylint模块,该模块非python自带模块,用import肯定不能导入,需要额外安装7 s$ u/ w" ^5 n! F3 w
代码如下:' c% ]+ B1 F5 b2 m8 G5 {$ J- B2 }1 w
>>> import pylint8 C7 a1 K- }% w9 h
traceback (most recent call last):
, ~1 j( [- M( H+ s$ b5 b file "", line 1, in
% o7 d/ Q5 Y) B/ O3 v. Z importerror: no module named pylint& }6 }4 y% i( N- [
【现象】& F! V* U4 l# A* J Q% r
执行pip install 命令,报错如下:
6 e7 |% G- t- F9 K3 H: C- B 代码如下:
) N1 f, @% I4 M d:\>pip install pylint --allow-external pylint
6 o( s! J: X9 Z2 ^. V' w4 m$ M8 H downloading/unpacking pylint& P# }, P8 t! K" r
requirement already satisfied (use --upgrade to upgrade): six in c:\python27\lib\site-packages\six-1
% z/ [$ d, j! x' Z .8.0-py2.7.egg (from pylint)
" f; l$ P/ B7 p downloading/unpacking astroid>=1.3.6 (from pylint)0 N% \) M0 N- ]! Y: E& q
real name of requirement astroid is astroid9 ^9 _& V) z$ ]: r
could not find any downloads that satisfy the requirement astroid>=1.3.6 (from pylint)
- [% P4 T- [+ e8 z some insecure and unverifiable files were ignored (use --allow-unverified astroid to allow).
9 w2 R5 `* Q" F) w, Y+ X3 Y cleaning up...
+ I2 ?0 |0 M( T) G1 L5 G7 O no distributions at all found for astroid>=1.3.6 (from pylint)7 \ K0 \" I" _% \" m% \% S
storing debug log for failure in c:\users\aaa\pip\pip.log8 `9 R1 Q+ {9 y# q0 b
【分析】: z. K4 W; n6 u0 H" u4 S/ r6 L
在perl中安装新模块,一般可以用ppm图形化工具,也可以用cpan来安装,比如说: cpan>install test::class, 非常方便,不会碰到这种情况,这种情况主要是因为pip版本问题: pip最新的版本(1.5以上的版本), 出于安全的考
% m; P3 @. v- n+ L2 P 虑,pip不允许安装非pypi的url,因为该安装文件实际上来自pylint.org,因而导致上面的错误!
$ D! {+ B& N3 `# V note:
- y9 x" L% t( l0 ` 1. 可以在官方changelog里面查看更改的信息
- g8 ]5 j( y' C" J: h' \" u& i2 e% ~ 2. 可以用pip --version来查看pip的版本信息
! t. ^! r X; f" m 代码如下:
; X4 b+ a/ y7 C# x- a8 n c:\>pip --version% x" a/ T8 A" x8 H8 t! s
pip 1.5.6 from c:\python27\lib\site-packages (python 2.7)' r) M" `; y& k* ?; m% d
【办法】+ j5 Z6 O) V! x$ b% S
针对上面的情况,既然这个问题是因为pip版本的原因,可以改用pip低一点的版本; O# @8 h$ b$ k8 [8 J
方法一: 用pip 1.4版本,再执行pip install pylint命令来安装
2 Y; Z k2 ~' b) o: z 方法二: 执行命令时,加上--allow-all-external, --allow-unverified及依赖包版本(astroid==1.3.6)
V5 u8 z* m: A* R. D: C 代码如下:, H _# }/ s. U( |' u) _3 o( ^
pip install pylint --allow-all-external pylint astroid==1.3.6 --allow-unverified pylint! l) Y' Q$ E1 B3 {: j5 o
note:; J3 r3 k6 s- H0 L" Q
1. --allow-all-external # 允许所有外部地址的标签,只有打上该标签pip方可下载外部地址模块
: F6 P! O0 K/ M* d 2. --allow-unverified # pip没有办法校验外部模块的有效性,所以必须同时打上该标签
) h$ ]7 j: @- I z2 z& q$ R$ o 3. astroid==1.3.6 # 依赖包必须要添加上,并赋予其版本号,pip方能从列表下载1 d2 s; D. s& ~% ~
方法三: 在当前目录下,新增requirements.txt,内容如下:
, K+ Y% n9 u& j* D/ o$ z$ C8 i5 z 代码如下:
" \: Z7 S- E D1 }1 h- x # requirements.txt
. l* l" z% B$ H0 n6 ~ --allow-all-external pylint
3 ]" p; v# m) e4 D0 a --allow-unverified pylint
' @! v% W* e) |& L) y/ p$ a pylint
+ c' h) [; b1 s: r2 N9 ~+ y5 H3 y --allow-all-external astroid==1.3.6- n& [9 D0 g% L, C x% z" R: Y
再执行: pip install -r requirements.txt
3 ^$ q: Y3 y$ A$ y' `7 r" n 【结论】/ R' ~) Q8 Z% \/ N2 C7 o
1. pip这个设计不够友好,使用也很不方便,远不如perl中的ppm,期待python中也有这么个工具。! _# L! \$ [1 h& l$ \/ x" V9 y
2. 如果碰到这种错,导致不能安装模块的话: 直接下载压缩包安装好了。 >>>下载包地址<<<- i4 e7 w- n: M: K
3. 执行pip -h命令查看更新pip相关的帮助信息
1 K' P/ A" X6 R/ i1 @" S- ~ 代码如下: H6 d6 ?* v) s/ D P
usage:. ]2 [& I4 x) h8 J9 a5 ]& d0 s, S
pip [options]
- r1 x7 ~/ ~8 F3 u9 j% S commands:4 C+ i7 p4 s( s- L: S: a
install install packages.
& b) H. @9 x) V' V1 Q4 g5 f uninstall uninstall packages.; J( I Y% N' ?: \* v
freeze output installed packages in requirements format.
' \. v+ j* G: A7 m list list installed packages., V8 S0 L; @1 \/ C$ s/ Y7 j
show show information about installed packages.- E! O; M' C. A3 a- |/ P
search search pypi for packages.1 F) J0 ]$ Z; S: j: |8 E) S6 l
wheel build wheels from your requirements.
6 _/ U: y- W. Q1 n zip deprecated. zip individual packages.
/ x7 u& a7 W8 Q6 K; P# | unzip deprecated. unzip individual packages.0 h3 T4 g, M' J4 A9 j2 z
bundle deprecated. create pybundles.
6 D& z# W1 O9 r5 D4 b- X help show help for commands.
' F8 C; i* O/ T$ v9 R5 z general options:" a4 b& z* ` \, w2 z
-h, --help show help.) Y8 s: I$ ]1 p5 c1 i. D* m% D
-v, --verbose give more output. option is additive, and can be used up to 3 times.
9 s5 Q( u5 Q% S* c" h0 o -v, --version show version and exit.; d) {9 E9 y( {- l! _. G% y: {* U
-q, --quiet give less output.
9 \3 l. g- @6 q --log-filepath to a verbose non-appending log, that only logs failures. this log is active by default at pip.log.% _- R+ z* |" R0 b5 J+ z* D% C
--logpath to a verbose appending log. this log is inactive by default.
: Q. e1 j' p- k4 f) J --proxyspecify a proxy in the form [user:passwd@]proxy.server:port.
7 c Y& {% M& P1 t, m3 q8 y --timeout set the socket timeout (default 15 seconds).
* l. L, I: W9 v- x$ R6 Y! B --exists-action default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
. f% ^8 M: o1 R( G --certpath to alternate ca bundle., h+ C( j. ]- g( M+ x, a& f$ @* \4 H7 C
更多技术文章信息请查看: 技术文章 |
|