Linux系统升级Python3
来自CloudWiki
产生原因
Python版本问题,默认Red Hat Enterprise Linux 6.3的python版本为2.6.6,升级python版本,可解决该问题。
检查当前Linux版本和Python版本方法
more /etc/redhat-release
Red Hat Enterprise Linux Server release 6.3 (Santiago)
python -V
Python 2.6.6
升级Python方法
安装必要组件
yum -y install wget gcc
yum -y install zlib*
下载Python安装包,以3.6.6版本为例
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
tar -xzvf Python-3.6.6.tgz
编译安装
cd Python-3.6.6
./configure --prefix=/usr/local
make
make altinstall 或者 make install
make clean
注意:make install 与 make altinstall的区别:
install 除了做 altinstall 外还会做 bininstall , maninstall 共三个动作bininstall 就是在处理 Symbolic Link Chain 的相关事务, 而 maninstall 则是在产生 unversioned manual pages, 所以, 很明显的, 不使用 bininstall 可以避免 python install 时 update link 的问题。如果使用make install,你将会看到在系统中有两个不同版本的Python在/usr/bin/目录中。这将会导致很多问题,而且不好处理。
设置环境参数
rm /usr/bin/python
ln -s /usr/local/bin/python3.6 /usr/bin/python
python -V
Python 3.6.6
更改yum
由于升级 Python 之后,默认的python指向了python3.6,yum不能正常使用,错误信息如下:
[root@master Python-3.6.6]# yum repolist
File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ SyntaxError: invalid syntax
需要编辑 yum 的文件,vim /usr/bin/yum文件,将第一行记录#!/usr/bin/python改为#!/usr/bin/python2.6
[root@master Python-3.6.6]# yum repolist
Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * aliyun: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com repo id repo name status aliyun CentOS-6 - Base - mirrors.aliyun.com 6,713 extras CentOS-6 - Extras - mirrors.aliyun.com 47 updates CentOS-6 - Updates - mirrors.aliyun.com 1,056 repolist: 7,816