CentOS 6 系统默认 Python 版本是:2.6.6
g4t\kb0KD$a J0平时在使用中,某些库要求是 2.7.x 版本的库,记录 Python 升级到 2.7.x 过程 。51Testing软件测试网-A
`q6L0qFqw
51Testing软件测试网1m4E i,Q:eR
51Testing软件测试网od:s5sqrK"|:q
1. 准备安装包,系统是最小化安装
6`[i?l;c2mBm0# 下载安装依赖的相关包51Testing软件测试网@w@X Cwa
[root@isTester.com ~]# yum install vim gcc make wget -y
Q4i;UaC{8n4V0[root@isTester.com ~]# yum install openssl-devel zlib-devel readline-devel sqlite-devel -y51Testing软件测试网?6}0f7F1|5w
51Testing软件测试网4~'k9W{"_4}9t
51Testing软件测试网 jk{it z
v
# 下载
,~+n9Nu p@
i0[root@isTester.com ~]# cd /usr/local/src51Testing软件测试网 n9a;D+X*Z(^1J
[root@isTester.com ~]# wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tgz51Testing软件测试网3F+{(b7Z7S%o
# 解压
9M['F~$_(m:dG0[root@isTester.com ~]# tar -zxvf Python-2.7.10.tgz51Testing软件测试网TvgNps
[root@isTester.com ~]# ls
_9tp(N7iiPIx0Python-2.7.10 Python-2.7.10.tgz
@ b9D.oYI D4~~0
Ne7F"v Y:j7oI0
51Testing软件测试网8?2f}NhKU?
n t
2. 编译配置安装
h,{de2YFG}M0[root@isTester.com ~]# cd Python-2.7.10
U"]yDH]0[root@isTester.com Python-2.7.10]# ./configure --enable-shared --enable-loadable-sqlite-extensions \
1n(\p$U8l5G!G*u(@L0^0 --prefix=/usr/local/python27 --with-zlib --with-ssl
?4\1TbLa0[root@isTester.com Python-2.7.10]# vim ./Modules/Setup # 找到下边这一行内容,去掉注释51Testing软件测试网/Q |
f#Fvm
#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
Q,F
uq;n M)K0[root@isTester.com Python-2.7.10]# make && make install
yh1e0D/{ p0
)Q'O c7XkG$x0
51Testing软件测试网v)X)q&O Yt6zs
3. 查看 python 版本信息51Testing软件测试网{#x5TWXl"K
[root@isTester.com Python-2.7.10]# python -V
,_Z0BA6r6`Q m5l0Python 2.6.651Testing软件测试网1fG`lo
q
# 版本依旧是 2.6.651Testing软件测试网 aW/]#gQp
gkj
x7w)p,T5CF[0
51Testing软件测试网5\lCIZ
4. 用 python2.7 替换旧版本51Testing软件测试网_G~!Q6_
[root@isTester.com Python-2.7.10]# cd /usr/bin/51Testing软件测试网c$AH5u F-`h\
f#J2]"u2u(g0[root@isTester.com bin]# ls python* -l 51Testing软件测试网S$@;N%a6VhpB"j?
# 查找旧 python 版本信息51Testing软件测试网9hl"Ed2`M
-rwxr-xr-x. 2 root root 4864 2月 22 2013 python
?x,Q
wCBJ0lrwxrwxrwx. 1 root root 6 10月 22 18:38 python2 -> python51Testing软件测试网XxP h3Ik5V
d
-rwxr-xr-x. 2 root root 4864 2月 22 2013 python2.651Testing软件测试网'^_T)x#^
Mr"u*Y
v
51Testing软件测试网Xd Sdw
B3TZ Y
[root@isTester.com bin]# mv /usr/bin/python /usr/bin/python2.6.651Testing软件测试网0|(W+Ze+umf
[root@isTester.com bin]# ln -s /usr/local/python27/bin/python2.7 /usr/bin/python
O1G~7u-EZ0
s)?t'W7j'W0[root@isTester.com bin]# ls python* -l51Testing软件测试网|
A3y-g8_qm&|
lrwxrwxrwx. 1 root root 33 10月 23 00:01 python -> /usr/local/python27/bin/python2.751Testing软件测试网 A-@5d,NILAD_ b
lrwxrwxrwx. 1 root root 6 10月 22 18:38 python2 -> python
(E Hm0of+k{ @0-rwxr-xr-x. 2 root root 4864 2月 22 2013 python2.651Testing软件测试网G1`"^[8L?%P7@I
-rwxr-xr-x. 2 root root 4864 2月 22 2013 python2.6.651Testing软件测试网L*]-g.bNPyFM|
{"hpD7FS'j*q0
51Testing软件测试网N4I:Y0\1L1Q
]%Z
5. 重新验证 Python 版本信息
f8y S5X7Sr-w0[root@isTester.com bin]# python -V
l
a'|0}q0Python 2.7.10
x,^l5{%E3kc0#可以看到,系统识别的 python 版本已经是 python 2.7.1051Testing软件测试网(b;m[bDT |
GwoMXSF0|0#如果执行 python -V 遇到如下报错:
,P|c"_6S B@1I+es$UC~Q0python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory51Testing软件测试网Qqw-@8}
# 原因:linux 系统默认没有把/usr/local/python27/lib路径加入动态库搜索路径
]&BLGEM/Ib0解决:51Testing软件测试网mTihnb
[root@isTester.com ~]# vim /etc/ld.so.conf51Testing软件测试网!Q"|T.r4k.g
# 添加如下一行内容
;@/gi,]^Y0/usr/local/python27/lib51Testing软件测试网7a`Q,KX;z
[root@isTester.com ~]# ldconfig
`ws ?@b
i
w
{0# 使新添加的路径生效
z8G,Tdgh(A.m0
51Testing软件测试网
F}/r8@
Y"V8{9P
再次,重新验证 Python 版本信息51Testing软件测试网!Z-j1VS|
[root@isTester.com bin]# python -V
a)\d,o0o0Python 2.7.10
K"Dk
h-e6a0#可以看到,系统识别的 python 版本已经是 python 2.7.1051Testing软件测试网0|4zwX]q
51Testing软件测试网0EUc9y!vY2^7S
51Testing软件测试网S&bL*z
Jn
r`(J#FE;W:{0二、解决 yum 兼容性问题
lP(r.kU[3]!_0因为 yum 是不兼容 Python 2.7 的,所以 yum 不能正常工作,我们需要指定 yum 的 Python 为 2.6。
8rm]$N-a%~:B/@:Q"{H01. 升级 python 后 yum 出现的问题
8P WEE1NRP0[root@isTester.com bin]# yum
'p2C/` p){&i2^ W'DW0There was a problem importing one of the Python modules
@1LW-]$Nk(t KV }k0required to run yum. The error leading to this problem was:
#d0i)pdK4k0 No module named yum
0E0f?3A1T U0... ... ... ...
Ax"~
n7gIF0
51Testing软件测试网7U@.~L}Cc,y }'m
2. 编辑 yum 配置文件
i:j1Qu8fUQ0[root@isTester.com bin]# vim /usr/bin/yum
eY;bAU3|Z0#!/usr/bin/python
nl$p2D:|0# 第一行修改为 python2.6.6
d+cqM.C)^+r\&L0#!/usr/bin/python2.6.651Testing软件测试网C}2@[:G
jNl
K6Ln@T@T1`+T0
:J Tm p
bzD03. 验证 yum 问题解决
ja$~VD\H0[root@isTester.com bin]# yum repolist
)Ay;@"d%u4~qg)Q0Loaded plugins: fastestmirror51Testing软件测试网,H2WB*e8x"AhG)|&s
Existing lock /var/run/yum.pid: another copy is running as pid 15013.51Testing软件测试网6F%R%W0D_(P.E(g0z
…….51Testing软件测试网{0@i$}?_
…….51Testing软件测试网,rJpCsmB
51Testing软件测试网&nk Ll)M*w*z3L
#搞定
c[!ZY,[5N&~0
51Testing软件测试网1_9` _,X8x%s;u8n g
三、升级 python 后,安装 pip 工具51Testing软件测试网u}'m-K)jI
1. 下载安装
`B|&XWHpO0[root@isTester.com ~]# wget https://bootstrap.pypa.io/get-pip.py
T\5j(f0Gup/f"y0[root@isTester.com ~]# python get-pip.py51Testing软件测试网K[8j9\j&o \h9O
2. 设置软连接51Testing软件测试网u6c(j aRgX
[root@isTester.com ~]# ln -s /usr/local/python27/bin/pip2.7 /usr/bin/pip
Q'n*Y)LbU1[0#如果/usr/bin/pip下文件已经存在,先 mv /usr/bin/pip /usr/bin/pip2.6.6,在执行如上。51Testing软件测试网Q|[[ DvQV
51Testing软件测试网QJM
N^I
J1Z'S