Linux下默认系统自带python2.6的版本,这个版本被系统很多程序所依赖,所以不建议删除,如果使用最新的Python3那么我们知道编译安装源码包和系统默认包之间是没有任何影响的,所以可以安装python3和python2共存
首先去python官网下载python3的源码包,网址:https://www.python.org/
进去之后点击导航栏的Downloads,也可以鼠标放到Downloads上弹出菜单选择Source code,就是源码包的意思,点进去之后页面下方可以看到下载链接,包括源码包、Mac OSX安装包、Windows的安装包
1 安装依赖包
[root@node1 ~]# yum install wget gcc automake autoconf zlib-devel libtool bzip2-devel openssl-devel xz -y
[root@node1 ~]# yum install readline-devel.x86_64 #解决python3退格功能
拓展:
[root@node1 ~]# yum install tkinter tk-devel tk -y # 在Linux中python默认是不安装Tkinter模块,matplotlib依赖Tkinter模块,安装tensorflow需要;
2 编译安装
[root@node1 ~]# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
[root@node1 ~]# tar xf Python-3.6.5.tar.xz && cd Python-3.6.5
[root@node1 ~]# ./configure
[root@node1 ~]# ./configure --enable-unicode=ucs2 --enable-shared // --enable-optimizations # 二者装一个就行,这个是多装一个Tkinter模块
[root@node1 ~]# make && make install
3 把python3作为默认python
[root@node1 ~]# python --version
Python 2.6.6
[root@node1 ~]# which python
/usr/bin/python
[root@node1 ~]# which python3
/usr/local/bin/python3
[root@node1 ~]# ll /usr/bin/python*
-rwxr-xr-x 2 root root 9728 8月 18 2016 /usr/bin/python
lrwxrwxrwx 1 root root 6 12月 11 14:14 /usr/bin/python2 -> python
-rwxr-xr-x 2 root root 9728 8月 18 2016 /usr/bin/python2.6
[root@node1 ~]# rm -f /usr/bin/python /usr/bin/python2
[root@node1 ~]# ln -s /usr/local/bin/python3 /usr/bin/python
[root@node1 ~]# ln -s /usr/bin/python2.6 /usr/bin/python2
验证:
[root@node1 ~]# python --version
Python 3.6.5
4 指定yum调用的python版本
4.1 修改/usr/bin/yum
[root@node1 ~]# vim /usr/bin/yum
1 #!/usr/bin/python
改为
1 #!/usr/bin/python2.6 # 改为python默认版本,CentOS 7.*为python2.7
或:
[root@node1 ~]# ansible all -m shell -a "sed -i '1s#python$#python2.6#g' /usr/bin/yum“
[root@node1 ~]# ansible all -m shell -a "head -1 /usr/bin/yum”
修改软链接
[root@node1 ~]# ll /usr/bin/python*
lrwxrwxrwx 1 root root 22 1月 4 09:41 /usr/bin/python -> /usr/local/bin/python3
lrwxrwxrwx 1 root root 18 1月 4 09:41 /usr/bin/python2 -> /usr/bin/python2.6
-rwxr-xr-x 1 root root 9728 8月 18 2016 /usr/bin/python2.6
[root@node1 ~]#
4.2 CentOS 7.*
需要修改 /usr/libexec/urlgrabber-ext-down文件
[root@node1 ~]# ansible all -m shell -a "sed -i '1s#python$#python2.7#g' /usr/libexec/urlgrabber-ext-down"
[root@node1 ~]# ansible all -m shell -a "head -1 /usr/libexec/urlgrabber-ext-down"
还需要修改/usr/bin/yum-config-manager
[root@node1 ~]# ansible all -m shell -a "sed -i '1s#python$#python2.7#g' /usr/bin/yum-config-manager"
[root@node1 ~]# ansible all -m shell -a "head -1 /usr/bin/yum-config-manager"
附:
mv /usr/local/lib/{,bak_}libisl.so.10.1.1-gdb.py
提示:Ignoring ensurepip failure: pip 7.1.2 requires SSL/TLS
这是原因没有安装或升级openssl: