CentOS7下重装Python2.7与yum并安装Python3
在搭建Calibre-web的时候遇到了一点问题,pip老是出错,看了一下腾讯云CentOS7自带的Python是2.7,打算给它升个Python3,不小心把Python2.7和yum pip给一起删了,草 因为一些东西是依赖Python2的,装Python3的时候不能删
删除原环境
删除pip
1
python -m pip uninstall pip
删除Python
1
2
3rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps # 删除Python,解除关联
whereis python |xargs rm -frv # 删除残余文件
whereis python # 检查是否完全删除删除yum
1
2
3rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps
whereis yum |xargs rm -frv
whereis yum
配置新环境
从网上找教程要么是依赖yum安装Python,要么是依赖Python安装yum。。。
首先查看CentOS的版本号,后文选择对应的版本
1 | cat /etc/redhat-release |
我的系统版本是7.9.2009
下载Python2.7 从网易镜像站下载,此处以我的系统版本为例 如果提示404应该是更新了,可以自己上去找新版本复制链接
1
2
3
4
5
6
7
8
9
10
11
12mkdir /usr/local/src/python
cd /usr/local/src/python
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/python-2.7.5-89.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/python-devel-2.7.5-89.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/python-libs-2.7.5-89.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/python-pycurl-7.19.0-19.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/python-setuptools-0.9.8-7.el7.noarch.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/python-urlgrabber-3.10-10.el7.noarch.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/python-iniparse-0.4-9.el7.noarch.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/rpm-python-4.11.3-45.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/libxml2-python-2.9.1-6.el7.5.x86_64.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/python-kitchen-1.1.1-5.el7.noarch.rpm下载yum
1
2
3
4
5
6
7mkdir /usr/local/src/yum
cd /usr/local/src/yum
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/yum-3.4.3-168.el7.centos.noarch.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/yum-utils-1.1.31-54.el7_8.noarch.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/yum-updateonboot-1.1.31-54.el7_8.noarch.rpm
wget http://mirrors.163.com/centos/7.9.2009/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm安装Python2.7
1
2cd /usr/local/src/python
rpm -ivh python-* rpm-python-* libxml2-* --nodeps --force # 强制安装,不考虑依赖安装yum
1
2cd /usr/local/src/yum
rpm -ivh yum-*安装pip
1
yum -y install python-pip
到此Python2.7和yum的重装完成。
安装Python3
安装依赖包
1
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make
下载Python3 我这里下载3.10.1,如果想下载新版本可以去官网找 如果速度慢可以手动下载上传
1
2
3cd /usr/local
wget https://www.python.org/ftp/python/3.10.1/Python-3.10.1.tgz
tar -zxvf Python-3.10.1.tgz编译并安装
1
2
3
4mkdir /usr/local/python3
cd Python-3.10.1
./configure --prefix=/usr/local/python3
make && make install设置软链接(快捷方式) 注意这里是3.10不是3.10.1
1
2ln -s /usr/local/python3/bin/python3.10 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.10 /usr/bin/pip3
到此完成所有工作。可以再检查一下各个版本是否正确 1
2
3
4
5python -V
python3 -V
pip -V
pip3 -V
yum --version
后记
安装之后又遇到pip3的警告:WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
上网查是缺openssl,然而输入openssl version
显示有1.0.2版本,于是打算升级一下
1 | wget https://www.openssl.org/source/openssl-1.1.1m.tar.gz |
查看版本更新到了1.1.1,然而并没有解决问题
再和openssl一起重新编译Py3
1 | cd /usr/local/Python-3.10.1 |
完事。顺便更新一下pip
1 | /usr/local/python3/bin/python3.10 -m pip install --upgrade pip |