CentOS6.6自带的Python版本2.6.6想给他升级一下,安装Python2.7.9

一、准备安装包

1
2
3
  wget https: //bootstrap .pypa.io /get-pip .py  #下载setuptools和pip
  wget https: //pypi .python.org /packages/source/i/ipython/ipython-3 .1.0. tar .gz  #下载ipython
  wget https: //www .python.org /ftp/python/2 .7.9 /Python-2 .7.9. tar .xz  #下载python

二、安装依赖包

安装readline-devel,使python安装完成后支持退格安装gcc,否则配置时会报错,导致无法安装;还有一些扩展模块

1
2
3
yum -y  install  readline-devel
yum -y  install  gcc
yum -y  install  zlib-devel openssl-devel sqlite-devel

三、安装python2.7.9

由于下载的python包为xz格式的,所以解压tar.xz文件需要先xz -d xxx.tar.xz,再tar zvf命令将xxx.tar解包

1
2
3
4
5
6
7
8
9
10
11
12
13
xz -d Python-2.7.9. tar .xz
tar  xvf Python-2.7.9. tar
cd  Python-2.7.9
. /configure  --prefix= /usr/local/python
make  &&  make  install
mv  /usr/bin/python  /usr/bin/python2 .6.6  #把之前版本的python重命名
ln  -sv  /usr/local/python/bin/python2 .7  /usr/bin/python
[root@MidApp ~] # python
Python 2.7.9 (default, Nov 22 2017, 09:37:44) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type  "help" "copyright" "credits"  or  "license"  for  more  information.
>>>  import  ssl
>>>

四、安装ipython

也完全可以先安装pip,再使用pip install ipython 安装ipython

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
tar  zxvf ipython-3.1.0. tar .gz
cd  ipython-3.1.0
python setup.py build
python setup.py  install
ln  -sv  /usr/local/python/bin/ipython  /usr/bin/ipython
[root@MidApp ~] # ipython 
WARNING: IPython History requires SQLite, your  history  will not be saved
Python 2.7.9 (default, Nov 22 2017, 09:37:44) 
Type  "copyright" "credits"  or  "license"  for  more  information.
  
IPython 3.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about  'object' , use  'object??'  for  extra details.
  
In [1]:  import  sys
  
In [2]: 3+2
Out[2]: 5 
In [3]:

五、安装setuptools和pip

命令行里直接下面执行命令就行

1
2
3
4
5
6
7
python get-pip.py
[root@MidApp ~] # pip list
ipython (3.1.0)
numpy (1.13.3)
pip (9.0.1)
setuptools (37.0.0)
wheel (0.30.0)

六、我在这里遇到了几个坑,很麻烦:

1、安装pip时,遇到zipimport.ZipImportError: can’t decompress data这种错误

解决方法:

1
vim Python-2.7.9 /Modules/Setup

找到下面地方,去掉注释

1
zlib zlibmodule.c -I$(prefix) /include  -L$(exec_prefix) /lib  -lz

然后再使用

1
make  &&  make  install

重新编译一下python

2、再次安装pip时遇到下列错误:

1
2
3
4
5
6
7
pip is configured with locations that require TLS /SSL , however the ssl module  in  Python is not available.
Collecting six
Could not fetch URL 
https: //pypi .python.org /simple/six/ :
  There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping
Could not  find  a version that satisfies the requirement six (from versions: )
No matching distribution found  for  six

解决方法:

1
vim Python-2.7.9 /Modules/Setup

找到下面内容去掉注释,保存退出

1
2
3
4
SSL= /usr/local/ssl
_ssl _ssl.c \
         -DUSE_SSL -I$(SSL) /include  -I$(SSL) /include/openssl  \
         -L$(SSL) /lib  -lssl -lcrypto

然后再使用

1
make  &&  make  install

重新编译一下python

3、安装完新的python版本之后,奇怪的发现yum命令不能使用了

解决方法:原来yum命令依赖于python环境,需要改一下yum所调用的python的路径变量。

1
2
3
[root@MidApp ~] # which yum
/usr/bin/yum
[root@MidApp ~] # vim /usr/bin/yum

修改第一行:

1
#!/usr/bin/python2.6.6

再次使用yum命令,OK!