升级centos7自带的Python2.7到Python3.8
Python是和centos以及Redhat系列高度集成的一个编程环境,但fedora(通常centos和redhat都属fedora系列,不是包含关系哦,这是题外话,不扯了。)centos7和Redhat7自带的版本为低版本的Python2.7(centos5.6好像是Python2.6??忘了),Python的2 3版本之争一直比较激烈,因此Python3版本推出了模块包2to3这样的模块,辅助Python2的用户过渡到Python3,
以上统统为扯淡,现在的目标是将系统自带的原有的Python2.7升级到Python3.8.2版本,Python的一个重要功能是Python是yum 这个包管理程序的一个首要依赖,因此,需要在升级后讲yum的依赖更改为Python3.8.2。那么,具体的实施步骤如下:
一,准备Python3.8.2的安装包,
wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz
耐心等待下载完或者提前下载好该文件上传到服务器内。
二,解压安装包,并移动到符合安装规范的目录内
tar xf Python-3.8.2.tar.xz && mv Python-3.8.2.tar.xz /usr/local/src/python
三,安装编译所需的依赖包
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc libffi-devel -y
所需依赖需要解释一下,基本OpenSSL,gcc ,zlib-devel 这几个是编译常用的依赖,尤其是gcc,只要编译基本都必须有的哦,openssl是加密算法的提供模块,通常也是必yum安装的。
四,进入解压后的Python目录,执行编译命令。也就是进入 /usr/local/src/python 这个目录,切记!!!!!!!~~~~~~~
cd /usr/local/src/python/
/configure && make && make install (这个是编译命令)
通常编译的速度和cpu内存有关系的,越高越快,4c4核大概7 8分钟完毕。安装完毕后,可以发现,pip的版本比较低需要升级。
这个问题后续解决。先解决软连接的问题。
五,备份Python2.7的可执行文件,链接Python3.8的可执行文件,替换原来的Python2.7可执行文件位置。
mv /usr/bin/python /usr/bin/python_2.7
vim /usr/libexec/urlgrabber-ext-down#将文件内的 /usr/bin/python2 改为 /usr/bin/python2.7
vim /usr/bin/yum#将文件内的 /usr/bin/python2 改为 /usr/bin/python2.7
ln -sf /usr/local/Python/bin/* /usr/bin/
ln -sf /usr/local/python/bin/python3.8 /usr/bin/python
ln -s /usr/bin/pip3.8 /usr/bin/pip
以上的链接命令很重要,千万不可遗漏!!!!
就像下图这样更改,两个文件都这样改:
六,升级pip的版本为20版本,并将pip源更改为阿里云的源,以提高pip的安装速度。执行以下脚本。
#!/bin/bash
user=`whoami`
echo "$user"
direct=~/.pip
if [ ! -d "$direct" ];then
mkdir -p "$direct"
else
echo "this directory is exits"
fi
cat >~/.pip/pip.conf <<EOF
[global]
index-url=http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
yum install python-pip -y
pip install --upgrade pip
[root@centos7 ~]# pip install numpy
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting numpy
Downloading http://mirrors.aliyun.com/pypi/packages/e5/7d/fe25dcdfc46d14e037cbb87e480ac067da36f56a8e65928bf1040ff35793/numpy-1.19.4-cp38-cp38-manylinux2010_x86_64.whl (14.5 MB)
|████████████████████████████████| 14.5 MB 2.1 MB/s
Installing collected packages: numpy
Successfully installed numpy-1.19.4
[root@centos7 ~]# vim test.py
[root@centos7 ~]# python test.py
\[root@centos7 ~]# python test.py
[root@centos7 ~]# python -V #检查Python版本,正确
Python 3.8.2
[root@centos7 ~]# yum install httpd #yum功能完好可用
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
此时编写任意一个Python文件导入numpy模块,然后执行该文件,可以看到无报错,Python2.7升级到Python3.8.2完美成功!!!!!!!!!~