安装python3.7

  • 准备编译环境
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
  • 编译安装
wget https://www.python.org/ftp/python/3.7.10/Python-3.7.10.tgz
tar xvzf Python-3.7.10.tgz
cd Python-3.7.10
./configure
make && make altinstall

vi /etc/ld.so.conf 添加/usr/local/lib,然后ldconfig)
mv /usr/bin/python /usr/bin/python2.6.6
ln -s /usr/local/bin/python3.7 /usr/bin/python

## 将python头文件拷贝到标准目录,以避免编译ansible时,找不到所需的头文件
cd /usr/local/include/python3.7/
cp -a ./* /usr/include/

## 修改yum脚本,使其指向旧版本的python,已避免其无法运行
vim /usr/bin/yum
#!/usr/bin/python --> #!/usr/bin/python2.6.6

vim /usr/libexec/urlgrabber-ext-down
#!/usr/bin/python --> #!/usr/bin/python2.6.6

python2.7.5升级到python2.7.15

(1)、查看Centos版本及python版本

  • Centos版本

[root@qsh ~]# cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)

  • python版本

[root@qsh ~]# python -V
Python 2.7.5
[root@qsh usr]# ll -l /usr/bin/python*
lrwxrwxrwx 1 root root 7 3月 29 22:44 /usr/bin/python -> python2
lrwxrwxrwx 1 root root 9 3月 29 22:44 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7136 11月 6 00:29 /usr/bin/python2.7


(2)、从官网下载python对应版本的包并解压

[root@qsh packages]# wget https://www.python.org/ftp/python/2.7.15/Python-2.7.15.tgz
[root@qsh packages]# tar zxvf Python-2.7.15.tgz


(3)、配置、编译、安装

安装gcc(在编译时会依赖)

yum install gcc* openssl openssl-devel ncurses-devel.x86_64  bzip2-devel sqlite-devel python-devel zlib

配置、编译、安装

cd Python-2.7.15
./configure --prefix=/usr/local # [配置]指定可执行文件、库文件、配置文件、资源文件的安装路径。若没有权限加sudo
make # 编译
make altinstall # 不要使用make install,否则会覆盖系统自带python

(4)、安装后环境检查

python安装后的版本

[root@qsh ~]# python -V  # 发现版本还是原版本Python 2.7.5

安装前后的python对比

[root@qsh ~]# ll -l /usr/bin/python*  # 系统自带的
lrwxrwxrwx 1 root root 7 2019/03/03 14:24:13 /usr/bin/python -> python2
lrwxrwxrwx 1 root root 9 2019/03/03 14:24:13 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7216 2018/10/31 07:46:45 /usr/bin/python2.7
-rwxr-xr-x 1 root root 1835 2018/10/31 07:46:26 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 2019/03/26 17:13:24 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 2019/03/26 17:13:24 /usr/bin/python-config -> python2-config

[root@qsh ~]# ll -l /usr/local/bin/python* # 手工安装的
-rwxr-xr-x 1 root root 8296384 2019/03/26 17:16:56 /usr/local/bin/python2.7
-rwxr-xr-x 1 root root 1687 2019/03/26 17:17:05 /usr/local/bin/python2.7-config

备份旧版本,连接新版本

[root@qsh ~]# mv /usr/bin/python /usr/bin/python2.7.5
[root@qsh ~]# ll -l /usr/bin/python*
lrwxrwxrwx 1 root root 9 2019/03/03 14:24:13 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7216 2018/10/31 07:46:45 /usr/bin/python2.7
lrwxrwxrwx 1 root root 7 2019/03/03 14:24:13 /usr/bin/python2.7.5 -> python2 # 改为2.7.5
-rwxr-xr-x 1 root root 1835 2018/10/31 07:46:26 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 2019/03/26 17:13:24 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 2019/03/26 17:13:24 /usr/bin/python-config -> python2-config

[root@qsh ~]# ln -s /usr/local/bin/python2.7 /usr/bin/python # 增加连接

[root@qsh ~]# ll -l /usr/bin/python*
lrwxrwxrwx 1 root root 24 2019/03/26 17:20:18 /usr/bin/python -> /usr/local/bin/python2.7 # 新增的,并指向新安装的python
lrwxrwxrwx 1 root root 9 2019/03/03 14:24:13 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7216 2018/10/31 07:46:45 /usr/bin/python2.7
lrwxrwxrwx 1 root root 7 2019/03/03 14:24:13 /usr/bin/python2.7.5 -> python2
-rwxr-xr-x 1 root root 1835 2018/10/31 07:46:26 /usr/bin/python2.7-config
lrwxrwxrwx 1 root root 16 2019/03/26 17:13:24 /usr/bin/python2-config -> python2.7-config
lrwxrwxrwx 1 root root 14 2019/03/26 17:13:24 /usr/bin/python-config -> python2-config

再次检查python版本

[root@qsh ~]# python -V
Python 2.7.15

若想访问老版本python(如2.7.5版本)

[root@qsh ~]# python2.7.5 -V
Python 2.7.5


设置yum

系统预装的yum引用的老版本python

[root@qsh ~]# yum -y install epel-release
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.13 (default, Apr 2 2017, 04:48:29)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]

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

#解决方法
[root@qsh ~]# vi /usr/bin/yum
首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7

改完之后继续安装,又报错...
ImportError: No module named urlgrabber.grabber

[root@qsh ~]# vi /usr/libexec/urlgrabber-ext-down
首行的#!/usr/bin/python 改为 #!/usr/bin/python2.7


执行 python -V 遇到的问题:

python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
# 原因:linux系统默认没有把/usr/local/python27/lib路径加入动态库搜索路径

解决:

[root@vip ~]# vim /etc/ld.so.conf
# 添加如下一行内容
/usr/local/python27/lib

[root@vip ~]# ldconfig # 使新添加的路径生效



安装pip 工具

(1)、下载安装

wget https://bootstrap.pypa.io/pip/2.7/get-pip.py
sudo python2 get-pip.py

(2)、设置软连接

ln -s /usr/local/bin/pip2.7 /usr/bin/pip2
ln -s /usr/local/bin/pip2.7 /usr/bin/pip2.7

(3)、安装 ipython

pip install ipython==1.2.1
ln -s /usr/local/python27/bin/ipython /usr/bin/ipython