前一阵子需要用python3在虚拟机centos7测试一些数据,嫌弃系统自带的python2多余,不小心删掉了(此处后悔一万遍),导致yum源使用不了,不得已重新安装一次,做个记录
1. 删除系统原有python
##删除python
rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps ##强制删除已安装程序及其关联
whereis python |xargs rm -frv ##删除所有残余文件 ##xargs,允许你对输出执行其他某些命令
whereis python ##验证删除,返回无结果
##删除yum
rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps
whereis yum |xargs rm -frv
2. 重新安装系统python2
##安装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-iniparse-0.4-9.el7.noarch.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-urlgrabber-3.10-10.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
##安装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-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm
##安装系统python和yum
cd /usr/local/src/python
rpm -ivh python-* rpm-python-*
或者
rpm -ivh python-* rpm-python-* --nodeps --force
安装完后即可运行python查看
安装yum时遇到一点小问题、有个lib文件提示不存在,需要查找一下,重新指定,没遇到的不管
cd /usr/local/src/yum
rpm -ivh yum-*
如遇到重新指定一下软连接、
ln -s /usr/local/src/python3/usr/local/lib64 /usr/local/lib64
至此python2原有的yum便安装好了,可是仍旧没有解决我的问题,接下来就是想办法通过让python2和python2共存,在此以前需要重新安装一下openssl,不然pip3会报错
3. 安装新的openssl
##卸载旧的openssl
rpm -qa | grep openssl
yum -y remove openssl-1.0.2k-25.el7_9.x86_64
##下载新的openssl
wget http://www.openssl.org/source/openssl-1.1.1.tar.gz
如果提示失败使用下面这个
wget --no-check-certificate http://www.openssl.org/source/openssl-1.1.1.tar.gz
##解压编译
tar -zxvf openssl-1.1.1.tar.gz
cd openssl-1.1.1
./config --prefix=/usr/local/openssl
##如果提示perl就安装一下
#yum -y install perl
make && make install
到此基本完成,如果遇到有文件不存在重新指定一下如libssl.so.1.1不存在,或者
#error while loading shared libraries: libcrypto.so.1.1: cannot open shared object file: No such file or directory
找到位置重新指定
find / -name libssl.so.1.1
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
##创建软连接openssl
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
##查看版本
openssl version
好了,新版的openssl已经安装完成,接下来安装python3
先看看有没有安装python3,不想要的可以删一下
##删除python3
rpm -qa|grep python3|xargs rpm -ev --allmatches --nodeps
whereis python3 |xargs rm -frv
安装python3
需要先安装依赖包
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
创建目录
mkdir /usr/local/python3
cd /usr/local/python3
下载安装包,我装的是3.10,具体可以到https://www.python.org/ftp/python/ 找对应版本
wget https://www.python.org/ftp/python/3.10.10/Python-3.10.10.tar.xz
##3.11 https://www.python.org/ftp/python/3.11.3/Python-3.11.3.tar.xz
解压
tar -xvJf Python-3.10.10.tar.xz
cd Python-3.10.10
安装并指定openssl
./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
make clean # 清空缓存
make && make install
创建软连接
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
验证安装成功
python3 -V
pip3 -V
好了,安装完成,顺便提一嘴,如果安装对应python包时临时切换源可以这样
python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
网友评论