这篇文章主要介绍了python中pip安装非pypi官网第三方库的方法,pip最新的版本(1.5以上的版本), 出于安全的考虑,pip不允许安装非pypi的url,本文就给出两种解决方法,需要的朋友可以参考下' _/ }9 U- b) Z- A+ }8 K7 m- c
在python中安装非自带python模块,有三种方式:
! }$ T0 m o8 e* @ 1.easy_install8 s! ]# ^) Z$ ]7 D2 e5 }/ B' x& F
2.pip
" G8 n5 Y# Y5 S! X: D W 3.下载压缩包(.zip, .tar, .tar.gz)后解压, 进入解压缩的目录后执行python setup.py install命令 Y v1 `/ S; s
本文主要针对pip安装时可能会碰到的一种情况,及解决办法:
2 f3 M! e* _, u8 _ 假如我要安装pylint模块,该模块非python自带模块,用import肯定不能导入,需要额外安装
5 {" H* O8 b1 p5 N/ D7 b Q9 h! @ 代码如下:
+ ?. j, |) ?* G' B >>> import pylint/ c5 L: e# z7 `
traceback (most recent call last):8 b5 Y# f S/ f
file "", line 1, in . ~- k. a7 a" D& @- D, c$ q6 J
importerror: no module named pylint8 i1 t. x- H/ n* G/ l
【现象】5 h7 U- b" O5 f7 s( _ U
执行pip install 命令,报错如下:
, i. ]1 t# F0 B3 p% m; x3 { 代码如下:
. T5 X/ b* k7 i9 P4 E/ D d:\>pip install pylint --allow-external pylint
Y9 k# f2 G2 s4 `1 d3 V U; s5 G downloading/unpacking pylint+ v2 z" a( P: `* s2 K. s
requirement already satisfied (use --upgrade to upgrade): six in c:\python27\lib\site-packages\six-1 p2 C$ o {' N/ O
.8.0-py2.7.egg (from pylint)
4 g; S9 W+ V' ~, R downloading/unpacking astroid>=1.3.6 (from pylint)9 C! L! h1 u, [( p: x, _
real name of requirement astroid is astroid
. i- k7 T3 `. d5 U: G could not find any downloads that satisfy the requirement astroid>=1.3.6 (from pylint)
: k/ j; c3 ?, U some insecure and unverifiable files were ignored (use --allow-unverified astroid to allow).8 N8 A- t0 R* q, l# A, r. L
cleaning up...5 y) H. T3 p$ e0 @8 I0 k: t
no distributions at all found for astroid>=1.3.6 (from pylint)& G% g' ]# J" J/ F
storing debug log for failure in c:\users\aaa\pip\pip.log
9 P! h5 ?5 a% w" c 【分析】
1 n& Y8 B1 h& k 在perl中安装新模块,一般可以用ppm图形化工具,也可以用cpan来安装,比如说: cpan>install test::class, 非常方便,不会碰到这种情况,这种情况主要是因为pip版本问题: pip最新的版本(1.5以上的版本), 出于安全的考, `! I8 G/ X& B
虑,pip不允许安装非pypi的url,因为该安装文件实际上来自pylint.org,因而导致上面的错误!
: ^3 R) x p0 B) R1 @4 h note:; P3 l0 }7 u4 Y& F' ?* j1 `/ s. w* q
1. 可以在官方changelog里面查看更改的信息- `$ L$ ?( l- T1 n) r2 t! h
2. 可以用pip --version来查看pip的版本信息0 M0 e( I# W; M( ~9 a. K
代码如下:
8 r' P/ V9 H6 e& f' N3 I c:\>pip --version! j& Q/ V& Q/ _& F2 \5 S
pip 1.5.6 from c:\python27\lib\site-packages (python 2.7)1 A ^9 T' h4 G) ?$ R
【办法】
& ?: o. `0 L& j 针对上面的情况,既然这个问题是因为pip版本的原因,可以改用pip低一点的版本
5 D$ z5 i8 D& }# {7 y& N# ~! o 方法一: 用pip 1.4版本,再执行pip install pylint命令来安装/ U# W9 g2 {( u) `
方法二: 执行命令时,加上--allow-all-external, --allow-unverified及依赖包版本(astroid==1.3.6)) }* y. c2 F0 s Y+ W7 C
代码如下:3 E/ ^ ]. `; V& O
pip install pylint --allow-all-external pylint astroid==1.3.6 --allow-unverified pylint8 j$ E$ v* n9 L# w
note:; X. T& C6 T3 {% ? I! j/ O
1. --allow-all-external # 允许所有外部地址的标签,只有打上该标签pip方可下载外部地址模块
, y5 [7 @/ L) B+ K3 @- J 2. --allow-unverified # pip没有办法校验外部模块的有效性,所以必须同时打上该标签4 q, v9 k: ~4 U1 q
3. astroid==1.3.6 # 依赖包必须要添加上,并赋予其版本号,pip方能从列表下载! ?, o! O6 E- A* D
方法三: 在当前目录下,新增requirements.txt,内容如下:
, ^4 |. b* F% N* y; S 代码如下:
1 ?9 H7 W7 v9 H( U* T) m7 f; c. _ # requirements.txt. W# c# R/ c1 w
--allow-all-external pylint
+ C# j3 h1 Y9 \: u# R9 Y --allow-unverified pylint
. [8 V( I% X7 N; s, D9 U: _/ ? pylint' r; n* q- j3 m/ |/ {# F6 G3 p$ p
--allow-all-external astroid==1.3.67 N X: }% ?1 H% \5 E' v
再执行: pip install -r requirements.txt1 U; e7 k1 s2 Z! L& }1 G! k2 Q% ]
【结论】4 F3 S6 Q' r9 d2 t3 f Y+ [
1. pip这个设计不够友好,使用也很不方便,远不如perl中的ppm,期待python中也有这么个工具。9 S1 o% N1 @3 |5 [1 a: f
2. 如果碰到这种错,导致不能安装模块的话: 直接下载压缩包安装好了。 >>>下载包地址<<<$ i, R. S* I" M0 f) o& k8 g
3. 执行pip -h命令查看更新pip相关的帮助信息; n7 s( V2 H+ t1 }8 M: n3 G
代码如下:" n2 }0 r- e4 A+ Z$ \1 T
usage:
+ k5 l: v" |. t* {4 M9 b% Q, w pip [options]
, [. }( L7 m5 E/ }: M commands:4 B; h. k, T* o: n$ l
install install packages.* C7 u% N4 K/ a9 N6 t* f0 {9 _
uninstall uninstall packages.
* T0 |* `! V7 } freeze output installed packages in requirements format.: C) j% r" G5 p: K& Y
list list installed packages.
3 z/ Q# U7 @( Z0 m! {6 R4 Y% g/ ?0 K show show information about installed packages.( {, z. R: m4 ?
search search pypi for packages.; f/ Z: i# R8 K7 r5 a' i
wheel build wheels from your requirements.. N, a ]; n/ j5 x. A
zip deprecated. zip individual packages.
+ e; H, P4 S7 `- m( t! `& | unzip deprecated. unzip individual packages.
. L8 z* w+ ]1 U! m# P bundle deprecated. create pybundles.3 T( q, D* L) k
help show help for commands.( j# X/ s9 V/ v7 ~
general options:
6 w3 Q: u1 A8 @" X7 a Q -h, --help show help.
* q% h9 A$ E6 ]6 C& H, [ -v, --verbose give more output. option is additive, and can be used up to 3 times.
) }% q8 u3 {: T Z -v, --version show version and exit.
. I1 H; H5 W, u2 E0 [+ y9 K -q, --quiet give less output.
; U2 @! V8 B- s* q# J4 ~ --log-filepath to a verbose non-appending log, that only logs failures. this log is active by default at pip.log.
A. E2 o0 s$ m9 o8 o5 U0 y, I --logpath to a verbose appending log. this log is inactive by default.4 }. U8 K; {# d) d; [0 Z
--proxyspecify a proxy in the form [user:passwd@]proxy.server:port.% X0 t8 ~1 |' z ]" I& w
--timeout set the socket timeout (default 15 seconds).
3 Y) ^. ?. p1 B- p; F' x --exists-action default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
: J- u* w+ J; S; \5 f3 O0 K G --certpath to alternate ca bundle.+ B: a* x8 r' n$ h
更多技术文章信息请查看: 技术文章 |
|