Cent OS系统默认自带有Python2版本,在正常情况下,是不需要重装的。往往有些时候,Python环境不可用,或者yum命令无法执行时,就可以通过重装来修复环境。
1.删除已有的Python
#rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps
#whereis python |xargs rm -frv
#whereis python
2.删除已有的yum
#rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps
#whereis yum |xargs rm -frv
3. 创建两个目录用于存放Python和yum的rpm包
#mkdir /usr/local/src/python
#mkdir /usr/local/src/yum
4. 查到centOS的版本并下载相应的rpm包
#cat /etc/redhat-release
对应的rpm包下载网址:http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/python-2.7.5-86.el7.x86_64.rpm
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/python-devel-2.7.5-86.el7.x86_64.rpm
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/python-iniparse-0.4-9.el7.noarch.rpm
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/python-pycurl-7.19.0-19.el7.x86_64.rpm
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/python-setuptools-0.9.8-7.el7.noarch.rpm
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/python-libs-2.7.5-86.el7.x86_64.rpm
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/python-urlgrabber-3.10-9.el7.noarch.rpm
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/rpm-python-4.11.3-40.el7.x86_64.rpm
进入到python目录下
#cd /usr/local/src/python
将上面的rpm包下载到python目录
cd /usr/local/src/yum
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/yum-3.4.3-163.el7.centos.noarch.rpm
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/yum-utils-1.1.31-52.el7.noarch.rpm
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-52.el7.noarch.rpm
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/yum-plugin-protectbase-1.1.31-52.el7.noarch.rpm
http://mirrors.ustc.edu.cn/centos/7.7.1908/os/x86_64/Packages/yum-plugin-aliases-1.1.31-52.el7.noarch.rpm
5.安装python
#rpm -ivh python-* rpm-python-*
出现上面依赖包的解决方法:
#rpm -ivh python-* rpm-python-* --nodeps --force
输入#python 查看Python是否按照成功
6. 安装yum
#cd /usr/local/src/yum
#rpm -ivh yum-* --nodeps --force
7.安装Python3.8
# wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz !获取Python软件包
# tar -zxvf Python-3.8.1.tgz !解压
# cd Python-3.8.1
# yum -y install libffi-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make !安装相关依赖
在 ./configure 操作前,先进行编码配置:
# export LANGUAGE=en_US.UTF-8
# export LANG=en_US.UTF-8
# export LC_ALL=en_US.UTF-8
# ./configure prefix=/usr/local/python38
# make && make install
建立软链接
# ln -s /usr/local/python38/bin/python3 /usr/local/bin/python3
# ln -s /usr/local/python38/bin/pip3 /usr/local/bin/pip3
8.将Cent OS 默认的Python版本更改为Python3
环境变量修改
# vi ~/.bashrc
添加下面path配置
PATH=$PATH:$HOME/bin:/usr/local/python38/bin
重载文件
source ~/.bashrc
更新Python的默认指向
# ls -l /usr/bin | grep python !查看python命令指向
# sudo mv /usr/bin/python /usr/bin/python.bak !备份
# sudo ln -s /usr/local/bin/python3 /usr/bin/python !更新
检查Python版本
9.更新yum文件,否则会运行不了
将两个文件的首行#!/usr/bin/python改为#!/usr/bin/python2 之前Python指向的Python文件
# vim /usr/bin/yum
# vim /usr/libexec/urlgrabber-ext-down