一、前言
升级glibc的时候根据INSTALL升级说明,要求python版本3.4以上。所以需要对python版本进行升级。centos7默认安装的python版本为2.7.5,python通过yum安装也只能安装2.7.5版本,python3可以通过yum安装3.6.8版本。如果需要更高的版本,只能通过源码编译安装。此博文介绍源码安装方式升级python和python3版本,源码编译安装要求操作系统已经安装了gcc。环境说明如下:
操作系统:centos7.6
python版本:升级前版本2.7.5,升级后2.7.18
python3版本:升级前3.6.8,升级后版本3.8.8
二、python安装步骤
1、查看当前python版本
[root@s143 ~]# python -V
Python 2.7.5
2、yum安装python
[root@s143 ~]# yum install -y python2
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Package python-2.7.5-90.el7.x86_64 already installed and latest version
Nothing to do
3、yum安装python3
[root@s143 ~]# yum install -y python3
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed:
python3.x86_64 0:3.6.8-18.el7
Dependency Installed:
libtirpc.x86_64 0:0.2.4-0.16.el7 python3-libs.x86_64 0:3.6.8-18.el7 python3-pip.noarch 0:9.0.3-8.el7 python3-setuptools.noarch 0:39.2.0-10.el7
Complete!
————————————————
1、下载python新版本
[root@s143 opt]# wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz
2、解压软件包
[root@s143 opt]# tar -zxvf Python-2.7.18.tgz
3、预编译软件包
[root@s143 opt]# cd Python-2.7.18
[root@s143 Python-2.7.18]# ./configure --prefix=/usr
4、编译软件包
[root@s143 Python-2.7.18]# make
…
running build_scripts
creating build/scripts-2.7
copying and adjusting /opt/Python-2.7.18/Tools/scripts/pydoc -> build/scripts-2.7
copying and adjusting /opt/Python-2.7.18/Tools/scripts/idle -> build/scripts-2.7
copying and adjusting /opt/Python-2.7.18/Tools/scripts/2to3 -> build/scripts-2.7
copying and adjusting /opt/Python-2.7.18/Lib/smtpd.py -> build/scripts-2.7
changing mode of build/scripts-2.7/pydoc from 644 to 755
changing mode of build/scripts-2.7/idle from 644 to 755
changing mode of build/scripts-2.7/2to3 from 644 to 755
changing mode of build/scripts-2.7/smtpd.py from 644 to 755
/usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python-gdb.py
5、安装软件包
[root@s143 Python-2.7.18]# make install
6、检查升级后的版本
[root@s143 Python-2.7.18]# python -V
Python 2.7.18
四、python3升级步骤
1、下载安装包
[root@s143 opt]# wget https://www.python.org/ftp/python/3.8.8/Python-3.8.8.tgz
2、解压软件包
[root@s143 opt]# tar -zxvf Python-3.8.8.tgz
3、预编译
注意在编译结束后会有提示"If you want a release build with all stable optimizations active (PGO, etc),please run ./configure --enable-optimizations",加上–enable-optimizations预编译的话后续编译会报错“Could not import runpy module ”,原因是gcc版本太低,enable-optimizations参数要求gcc版本8.1.0以上。
[root@s143 opt]# cd Python-3.8.8
[root@s143 Python-3.8.8]# ./configure
4、编译
[root@s143 Python-3.8.8]# make
…
if test `uname -s` = Darwin; then
cp python-config.py python-config;
fi
5、编译安装
[root@s143 Python-3.8.8]# make install
…
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-18.1 setuptools-40.6.2
6、升级后版本检查
[root@s143 Python-3.6.10]# python3 -V
Python 3.8.8
五、QA
1、安装python3.8.8的时候报错zlib not available
报错信息:zipimport.ZipImportError: can’t decompress data; zlib not available
报错原因:没有安装zlib模块
解决方案:安装zlib模块
[root@s143 bin]# yum install -y zlib*
2、安装完python之后yum报错File “/usr/bin/yum”, line 30
报错信息:File “/usr/bin/yum”, line 30
————————————————
如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!
添加我为好友,拉您入交流群!
请使用微信扫一扫!