一.背景介绍
虚拟机的liunx系统CentOS7自带了python2.7.5,为使用方便需要换成最新的版本3.10.4
二.安装方法
1.安装升级GCC
yum install -y gcc* openssl openssl-devel ncurses-devel.x86_64 bzip2-devel sqlite-devel zlib python-devel mysql-devel openldap-devel
2.下载python安装包
wget https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tar.xz
3.解压
tar xvf Python-3.10.4.tar.xz
4.编译安装
cd Python-3.10.4
mkdir -p /usr/local/python3
./configure --prefix=/usr/local/python3
make && make install
5.将老版本python更换
mv /usr/bin/python /usr/bin/python2.7.5
6.更换默认python
ln -s /usr/local/python3/bin/python3 /usr/bin/python
ln -s /usr/local/python3/bin/pip3.10 /usr/bin/pip
7.修改 yum以及urlgrabber-ext-down中的python
vim /usr/bin/yum
vim /usr/libexec/urlgrabber-ext-down
解决方案1:pip设置后重装python(并未解决)
cd Python-3.10.4
进入目录下,依次执行以下3条命令
./configure –with-ssl
make
sudo make install
解决方案2:升级openssl版本并编译安装(解决)
#1.下载openssl
wget https://www.openssl.org/source/openssl-1.1.1o.tar.gz --no-check-certificate
tar zxvf openssl-1.1.1o.tar.gz
cd openssl-1.1.1o.tar.gz
# 2.编译安装
./config --prefix=/usr/local/openssl no-zlib #不需要zlib
make
make install
# 3.备份原配置
mv /usr/bin/openssl /usr/bin/openssl.bak
mv /usr/include/openssl/ /usr/include/openssl.bak
# 4.新版配置
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/local/lib64/libssl.so
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
# 5.修改系统配置
## 写入openssl库文件的搜索路径
echo "/usr/local/openssl/lib" >> /etc/ld.so.conf
## 使修改后的/etc/ld.so.conf生效
ldconfig -v
# 6.查看openssl版本
openssl version
#7 此时重新安装python
cd Python-3.10.4
./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/openssl
make
make install
#8.此时pip命令安装时报需要升级,升级
/usr/local/python3/bin/python3.10 -m pip install --upgrade pip
#9.升级pip完成后下载成功