这篇文章主要介绍了python中pip安装非pypi官网第三方库的方法,pip最新的版本(1.5以上的版本), 出于安全的考虑,pip不允许安装非pypi的url,本文就给出两种解决方法,需要的朋友可以参考下; R5 C z$ `4 ~1 w% b
在python中安装非自带python模块,有三种方式:2 ~9 ]% Z c8 j& G& u3 {
1.easy_install' O% D6 ]6 B1 c
2.pip
; H. S. d# W7 G4 i 3.下载压缩包(.zip, .tar, .tar.gz)后解压, 进入解压缩的目录后执行python setup.py install命令
g' R5 W @3 |* {! w1 ]3 Q9 i 本文主要针对pip安装时可能会碰到的一种情况,及解决办法:- X# e) N, d, z9 ~; `
假如我要安装pylint模块,该模块非python自带模块,用import肯定不能导入,需要额外安装7 \7 v8 \. _! ?* l r
代码如下:/ E- i$ H+ G \+ L3 ?/ U1 n+ o
>>> import pylint
/ E4 V' a! j3 T! A traceback (most recent call last):
) {( g% _. _5 P file "", line 1, in
# `6 N% U' x& r/ l. m importerror: no module named pylint" m; g8 ^& C9 y5 w+ I" F
【现象】
$ H$ r4 f: q$ H7 [ 执行pip install 命令,报错如下:; x2 w+ m% ^5 m3 h% a, x6 `
代码如下:+ @, e" e% B, L1 d5 E2 ^
d:\>pip install pylint --allow-external pylint
6 g! {/ v) j: T4 k4 T6 e5 ]% E downloading/unpacking pylint5 d7 i& e0 t; y
requirement already satisfied (use --upgrade to upgrade): six in c:\python27\lib\site-packages\six-1
% o2 _: F$ K1 t8 c! N2 N+ @ .8.0-py2.7.egg (from pylint)/ u4 N2 U6 z }% n2 L8 c. j
downloading/unpacking astroid>=1.3.6 (from pylint)
$ @0 H/ W5 C% E real name of requirement astroid is astroid; U6 [9 R" k; l9 Y
could not find any downloads that satisfy the requirement astroid>=1.3.6 (from pylint)
5 C! e+ \0 W; }8 \% I( m5 _( W. J some insecure and unverifiable files were ignored (use --allow-unverified astroid to allow).
/ b% @) S5 e4 l/ H5 Z2 j2 X cleaning up...
! N- j# E2 N- ?7 h no distributions at all found for astroid>=1.3.6 (from pylint)
3 o7 X w5 x) w3 Z9 r) W storing debug log for failure in c:\users\aaa\pip\pip.log# r' e3 d% A6 Y6 A; e4 j
【分析】* c' q, Z7 U- U& u4 b! W# Y# U e
在perl中安装新模块,一般可以用ppm图形化工具,也可以用cpan来安装,比如说: cpan>install test::class, 非常方便,不会碰到这种情况,这种情况主要是因为pip版本问题: pip最新的版本(1.5以上的版本), 出于安全的考
! u$ M4 ?- k5 y9 P6 y 虑,pip不允许安装非pypi的url,因为该安装文件实际上来自pylint.org,因而导致上面的错误!2 l7 s5 t9 b# \
note:
/ o: l6 [8 I ~# Z& y 1. 可以在官方changelog里面查看更改的信息
7 Y; u# T6 Z! N. M, S; R 2. 可以用pip --version来查看pip的版本信息
6 ^+ j8 u M% N) a 代码如下:
; W* h5 @* j' l- W9 u) h& t1 i c:\>pip --version
+ U$ s- E) g, z pip 1.5.6 from c:\python27\lib\site-packages (python 2.7)
. q7 y A S; {% t 【办法】# C6 z1 R, t3 F7 c2 B; u% W* S
针对上面的情况,既然这个问题是因为pip版本的原因,可以改用pip低一点的版本
* g4 u0 n2 V6 S* Z. X' [4 O) b 方法一: 用pip 1.4版本,再执行pip install pylint命令来安装
0 Y3 f. y9 B8 W+ e5 B# |& @ 方法二: 执行命令时,加上--allow-all-external, --allow-unverified及依赖包版本(astroid==1.3.6)
9 I. u# ?% x5 F, f- w, v 代码如下:
- h! G- o# i9 k pip install pylint --allow-all-external pylint astroid==1.3.6 --allow-unverified pylint
- ]- ?4 }0 v' h: \7 }' G# c note:8 I2 k& Z$ C4 Y
1. --allow-all-external # 允许所有外部地址的标签,只有打上该标签pip方可下载外部地址模块$ {, B! s3 N1 S, S8 N. I9 Y: w
2. --allow-unverified # pip没有办法校验外部模块的有效性,所以必须同时打上该标签/ w3 H) t1 }! m
3. astroid==1.3.6 # 依赖包必须要添加上,并赋予其版本号,pip方能从列表下载
' H1 F6 M t V5 D* ?1 b: D 方法三: 在当前目录下,新增requirements.txt,内容如下:
! H& S& p5 W d# D+ R 代码如下:/ O& t2 M: ?0 |' ~4 I) Q
# requirements.txt7 L; ?/ Y* `0 o$ }( g- B/ P' M
--allow-all-external pylint
n: z& n, c8 J7 l --allow-unverified pylint! i6 w+ ?2 j, {3 e/ r, ?
pylint
o: L/ b1 ?# j) E$ X5 V# K* A1 D- t --allow-all-external astroid==1.3.68 \. P- Y% \, W/ m, m0 @" e5 R
再执行: pip install -r requirements.txt- ~1 n @0 s6 L+ m+ i( U7 c
【结论】+ h+ S( }, L; D4 d
1. pip这个设计不够友好,使用也很不方便,远不如perl中的ppm,期待python中也有这么个工具。$ c* ~; W' k# }& d* v
2. 如果碰到这种错,导致不能安装模块的话: 直接下载压缩包安装好了。 >>>下载包地址<<<9 |% y0 j# v/ S; K9 @, p* X
3. 执行pip -h命令查看更新pip相关的帮助信息9 Y$ D3 |8 w0 o9 [3 ~0 Q/ |1 P
代码如下:
% t) H, E/ R! {$ g( w usage:# M+ z9 T6 p- P- Y+ q* N l
pip [options]
$ z' r$ p/ Y* S& j( s8 m+ p commands:6 H" R/ e# x' e- h6 P$ o5 I
install install packages.' V+ I5 k5 P. x/ ` n
uninstall uninstall packages.: P8 h7 a- l( o f3 _% \
freeze output installed packages in requirements format.
& v# K0 @9 f! r2 x) x5 s list list installed packages.
% c5 W9 \: ~, `& A show show information about installed packages.
7 P+ _) h' O K1 d; H search search pypi for packages.
' o/ _" E/ u2 I wheel build wheels from your requirements.7 d8 ?# L3 s( [
zip deprecated. zip individual packages.
# K7 z( H* A* j unzip deprecated. unzip individual packages.& P Z3 @6 V* }* c( ?! E, j& c
bundle deprecated. create pybundles.
" l( s% F& Y* K! {7 b; E, t help show help for commands.
. g# J0 a8 K; G9 [) }1 E general options:
! N8 w/ Z% }* S7 {2 \ -h, --help show help.
4 d3 D4 O7 ~- x4 n -v, --verbose give more output. option is additive, and can be used up to 3 times.: f+ E, [" J, W/ }. s8 d
-v, --version show version and exit.
U9 M' U2 Y# `3 S* w" K8 G -q, --quiet give less output.) K2 F% k3 r; L; G% \7 T& g* Q
--log-filepath to a verbose non-appending log, that only logs failures. this log is active by default at pip.log.) T& X I9 |+ ]' C, N
--logpath to a verbose appending log. this log is inactive by default.7 D1 F) ?7 q6 v3 Z1 N
--proxyspecify a proxy in the form [user:passwd@]proxy.server:port.
# P5 J6 w& U P6 h7 n( o --timeout set the socket timeout (default 15 seconds).$ E( S/ B5 c3 `# j
--exists-action default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup.
4 I1 {2 v* c0 }; W2 @* l --certpath to alternate ca bundle.
- k6 e4 k$ S8 {; T$ D8 j J/ A更多技术文章信息请查看: 技术文章 |
|