Centos下升级Python


预准备(可忽略)

由于Centos需要提前安装Sqlite数据库,否则之后Python无法正常导入sqlite3,因此建议在升级Python前预先安装Sqlite数据库

1
yum  install  sqlite-devel -y


一、查看本机Pyhton版本

本机Centos6.5 预带Pyhton版本为2.6.6

1
2
# python --version
>>Python 2.6.6


二、下载欲升级最新Python安装包

可以去官网找最新安装包 https://www.python.org/downloads/ 


三、修改编译文件支持SSL

编译安装的时候并没有把SSL模块编译进去,安装后可能会导致urllib2不支持https链接

1
2
3
4
5
6
7
8
9
10
11
12
13
Traceback (most recent call last):
   File  "<stdin>" , line  1 in  <module>
   File  "/usr/local/lib/python2.7/urllib2.py" , line  154 in  urlopen
     return  opener. open (url, data, timeout)
   File  "/usr/local/lib/python2.7/urllib2.py" , line  431 in  open
     response  =  self ._open(req, data)
   File  "/usr/local/lib/python2.7/urllib2.py" , line  454 in  _open
     'unknown_open' , req)
   File  "/usr/local/lib/python2.7/urllib2.py" , line  409 in  _call_chain
     result  =  func( * args)
   File  "/usr/local/lib/python2.7/urllib2.py" , line  1265 in  unknown_open
     raise  URLError( 'unknown url type: %s'  %  type )
urllib2.URLError: <urlopen error unknown url  type : https>

我们需要先安装openssl-devel(已安装可忽略)

1
2
3
4
5
6
# rpm -aq|grep openssl
>>openssl-1.0.1e-30.el6.x86_64
# yum install openssl-devel -y
# rpm -aq|grep openssl
>>openssl-1.0.1e-30.el6.x86_64
>>openssl-devel-1.0.1e-42.el6.x86_64

在编译安装Python之前加上以下这段代码(代码去除注释即可)

1
>> vi  Python-2.7.11 /Modules/Setup .dist
1
2
3
4
5
6
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL = / usr / local / ssl
_ssl _ssl.c \
         - DUSE_SSL  - I$(SSL) / include  - I$(SSL) / include / openssl \
         - L$(SSL) / lib  - lssl  - lcrypto

*另注:

由于系统缺少了readline相关模块,会导致编译安装Python后无法使用上下左右退格,所以需要再编译安装前安装readline-devel模块

1
yum -y  install  readline-devel


、编译安装Python

1
2
3
4
5
6
wget  
tar  -zxvf Python-2.7.10.tgz
cd  Python-2.7.10
. /configure 
make  &&  make  install
#也可以先查看是否有安装gcc再进行编译安装

通过命令查看是否已安装新版本

1
2
# /usr/local/bin/python2.7 -V
>>Python 2.7.11

现在我们可以通过python2.7启动python,但是这样太麻烦,我们可以自行修改下

1
2
mv  /usr/bin/python  /usr/bin/python2 .6.6
ln  -s  /usr/local/bin/python2 .7  /usr/bin/python
1
2
# python -V
>>Python 2.7.11


、修改yum脚本环境变量引用

安装完新版本Python后会发现yum出错,所以我们要把yum指向老版本

1
vi  /usr/bin/yum
1
2
3
4
5
6
7
#!/usr/bin/python2.6.6 # 修改#!/usr/bin/python为#!/usr/bin/python2.6.6
import  sys
try :
     import  yum
except  ImportError:
     print  >> sys.stderr, """\
.....

测试yum命令,一切正常


、升级完Python后安装pip

python从2.6升级到2.7之后会出现安装setuptools和pip启动失败,这是因为路径的问题,我们可以下载一个脚本,运行后会自动重新下载一个setuptools

curl -O https://bootstrap.pypa.io/ez_setup.py

# python ez_setup.py

安装setuptools

1
# yum install -y python-setuptools

安装distribute

由于Setuptools包不再维护distribute,所以安装后可能由于distribute版本缺失导致报错

1
2
3
4
5
6
7
8
9
# easy_install
Traceback (most recent call last):
   File  "/usr/local/bin/easy_install" , line 5,  in  <module>
     from pkg_resources  import  load_entry_point
   File  "/usr/lib/python2.7/dist-packages/pkg_resources.py" , line 2675,  in  <module>
     parse_requirements(__requires__), Environment()
   File  "/usr/lib/python2.7/dist-packages/pkg_resources.py" , line 552,  in  resolve
     raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: distribute==0.6.10 distribution was not found and is required by the application

通过以下方式安装缺失的distribute

1
2
3
4
# wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.10.zip --no-check-certificate
# unzip distribute-0.6.10.zip
# cd distribute-0.6.10
# /usr/local/bin/python2.7 setup.py install

or

1
2
3
4
# wget 
# tar xf distribute-0.6.10.tar.gz
# cd distribute-0.6.10
# /usr/local/bin/python2.7 setup.py install


安装pip

# easy_install pip

1
<span style= "font-family: sans-serif;" > # </span><span style="font-family: sans-serif;">easy_install</span><span style="font-family: sans-serif;"> </span><span style="font-family: sans-serif;">-</span><span style="font-family: sans-serif;">i</span><span style="font-family: sans-serif;"> </span><span style="font-family: sans-serif;">https</span><span style="font-family: sans-serif;">:</span><span style="font-family: sans-serif;">/</span><span style="font-family: sans-serif;">/</span><span style="font-family: sans-serif;">pypi</span><span style="font-family: sans-serif;">.tuna</span><span style="font-family: sans-serif;">.tsinghua</span><span style="font-family: sans-serif;">.edu</span><span style="font-family: sans-serif;">.cn</span><span style="font-family: sans-serif;">/</span><span style="font-family: sans-serif;">simple </span><span style="font-family: sans-serif;">pip</span><br data-filtered="filtered">

、升级完Python后ibus输入法报错

升级 Python2.7 后会导致 Ibus 输入法python2.6的gtk无法使用问题

1
2
3
/usr/libexec/ibus-ui-gtk
/usr/  bin /ibus-setup
/usr/libexec/ibus-engine-table

分别修改以上三个文件中的  “python”  为  “python2.6”

、配置pip和easy_isntall的镜像源


easy_install的配置文件 ~/.pydistutils.cfg :

1
2
[easy_install]
index_url = https: //pypi .tuna.tsinghua.edu.cn /simple


pip的配置文件 ~/.pip/pip.conf :

1
2
[global]
index-url = https: //pypi .tuna.tsinghua.edu.cn /simple


至此,升级完毕,后续继续补充



本文仅作一个记录,以备忘记查看方便