一、安装python3
【注意】全程在 root 权限下执行
1、先安装openssl,不然以后python会遇到出错:
ModuleNotFoundError: No module named '_ssl'
curl http://www.openssl.org/source/openssl-1.1.1.tar.gz -o openssl-1.1.1.tar.gz
tar -zxvf openssl-1.1.1.tar.gz
cd openssl-1.1.1
./config --prefix=/usr/local/openssl shared zlib
make && make install
2、设置系统环境变量LD_LIBRARY_PATH
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/openssl/lib" >> /etc/profile
source /etc/profile
3、安装各种依赖,以防python导入发生一些错误,如:
ModuleNotFoundError: No module named ‘_ctypes‘
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 gcc libffi-devel
yum不可用点这:【最新可用】centOS7替换阿里yum源
4、安装python3
curl https://www.python.org/ftp/python/3.10.11/Python-3.10.11.tar.xz -o Python-3.10.11.tar.xz # 下载安装包
tar -zxvf Python-3.10.11.tar.xz #(解压安装包)
./configure --prefix=/usr/local//python3 --with-openssl=/usr/local//openssl # (配置安装目录,指定openssl环境的安装目录)
make && make install #(编译、安装)
安装成功后执行:
python3 -V
二、修改环境变量
修改环境变量python为python3,使其执行
python -V
输出为python3的版本
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip # (pip命令行调用指向pip3)
ln -s /usr/local/python3/bin/python3 /usr/bin/python # (python命令行调用指向python3)
修改yum的python配置,不然修改环境变量后yum用不了
sed -i '1s|.*|#!/usr/bin/python2.7|' /usr/bin/yum
sed -i '1s|.*|#!/usr/bin/python2.7|' /usr/libexec/urlgrabber-ext-down