不灭的焱

革命尚未成功,同志仍须努力

作者:php-note.com  发布于:2019-05-11 23:58  分类:Linux软件安装/优化 

1. 准备工作

下载源码包

wget -c https://www.python.org/ftp/python/2.7.16/Python-2.7.16.tgz

查看是否安装make工具

~#rpm -qa|grep make
automake-1.11.1-4.el6.noarch
make-3.81-20.el6.x86_64

如果没有安装make工具

yum -y install gcc automake autoconf libtool make

查看是否安装zlib库

~#rpm -qa|grep zlib
zlib-devel-1.2.3-29.el6.x86_64
zlib-1.2.3-29.el6.x86_64

安装zlib

yum install zlib-devel

检查是否安装ssl 库

~#rpm -qa|grep openssl
openssl-devel-1.0.1e-16.el6_5.x86_64
openssl-static-1.0.1e-16.el6_5.x86_64
openssl098e-0.9.8e-17.el6.centos.2.x86_64
openssl-1.0.1e-16.el6_5.x86_64
openssl-perl-1.0.1e-16.el6_5.x86_64

安装openssl

yum install openssl*

安装bzip2依赖库

yum install -y bzip2*

2. 编译安装

cp Python-2.7.16.tgz /alidata/tmp/123   # 把源码解压到这个 临时目录
tar -xzvf Python-2.7.16.tgz
vim Modules/Setup.dist

找到代码1:

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

找到代码2:

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

把注释"#"去掉

 

编译

./configure --prefix=/alidata/server/python/python2.7
make all 
make install
make clean
make distclean

安装成功

~# /alidata/server/python/python2.7/bin/python2.7
Python 2.7.16 (default, May 12 2019, 00:07:30) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

建立python2.7 软链

mv /usr/bin/python /usr/bin/python.bak
ln -s /alidata/server/python/python2.7/bin/python2.7 /usr/bin/python2.7
ln -s /usr/bin/python2.7 /usr/bin/python

3. 解决yum无法使用的问题

~# yum update
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   No module named yum

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
2.7.16 (default, May 12 2019, 00:07:30) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)]

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

因为centos 6.9 下yum默认使用的是python2.6

vim /usr/bin/yum

把以下内容

#!/usr/bin/python
import sys
try:
    import yum
except ImportError:
.......

的第一行,修改如下:

#!/usr/bin/python2.6

4. 安装python-pip工具

先安装setup-tools

wget -c https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg  --no-check-certificate
chmod +x setuptools-0.6c11-py2.7.egg
sh setuptools-0.6c11-py2.7.egg

安装pip

wget https://pypi.python.org/packages/source/p/pip/pip-1.3.1.tar.gz --no-check-certificate
cp pip-1.3.1.tar.gz /usr/src/
tar zxvf pip-1.3.1.tar.gz
cd pip-1.3.1
python setup.py install
ln -s /usr/local/python2.7/bin/pip /usr/bin/pip

 

 

CentOS7 将 Python 默认版本升级为 Python 3.x