CentOS 7.9 安装配置Python2与Python3共存
CentOS 7.9默认安装的是Python2.7.5版本, yum仓库中python3的版本是Python3.6.8。当需要更新的Python3版本时,需要手动编译安装。同时CentOS中部分工具如yum依赖Python2,yum安装python3时不会引发依赖问题,手动编译安装Python3需要额外配置Python3与Python2共存。
安装步骤如下:
# 查看当前版本
$ python -V
Python 2.7.5
$ which python
/usr/bin/python$ pip -V
pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)# 安装依赖
$ sudo yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make# 备份当前版本
$ sudo mv /usr/bin/python /usr/bin/python2.7.5
$ ls /usr/bin/python2
python2 python2.7 python2.7.5# 下载python3.10
$ wget https://www.python.org/ftp/python/3.10.9/Python-3.10.9.tar.xz
# 编译安装
$ tar xf Python-3.10.9.tar.xz
$ cd Python-3.10.9
$ ./configure prefix=/usr/local/python3 -C --with-openssl=/usr/local/openssl --with-openssl-rpath=auto
$ sudo make && sudo make install$ sudo ln -s /usr/local/python3/bin/python3 /usr/bin/python
$ sudo ln -s /usr/local/python3/bin/python3.10-config /usr/bin/python3-config
$ python -V
Python 3.10.9# 配置yum Python版本,此时yum无法使用
$ sudo yum repolistFile "/usr/bin/yum", line 30except KeyboardInterrupt, e:^^^^^^^^^^^^^^^^^^^^
SyntaxError: multiple exception types must be parenthesized
$ which yum
/usr/bin/yum
# 修改yum主文件第一行,使用python2.7.5
$ sudo vim /usr/bin/yum
#!/usr/bin/python2.7.5# 此时yum安装软件还会遇到如下错误
Total download size: 422 k
Installed size: 1.0 M
Is this ok [y/d/N]: y
Downloading packages:File "/usr/libexec/urlgrabber-ext-down", line 28except OSError, e:^^^^^^^^^^
SyntaxError: multiple exception types must be parenthesizedExiting on user cancel# 修改库文件urlgrabber-ext-down第一行,使用python2.7.5
$ sudo vim /usr/libexec/urlgrabber-ext-down
#! /usr/bin/python2.7.5# 测试yum安装软件成功。# 如果是云主机环境,使用python2安装的cloud-init,此时还需要修改cloud-init相关依赖,否则会导致cloud-init工作不正常。
$ sudo vi /usr/bin/cloud-init
#!/usr/bin/python2.7 # 将python解释器指向python2.7# pip3配置
$ sudo ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
$ pip3 -V
pip 22.3.1 from /usr/local/python3/lib/python3.10/site-packages/pip (python 3.10)