Centos7 编译安装 Python3.9
一、安装依赖关系
yum -y install zlib-devel xz-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel libffi-devel gcc make
二、下载编译安装(3.9.12)
#下载解压
wget -c https://www.python.org/ftp/python/3.9.12/Python-3.9.12.tgz
tar -xzvf ./Python-3.9.12.tgz
#编译安装
cd Python-3.9.12
./configure --enable-optimizations --enable-shared --prefix=/usr/local/python3
make altinstall
三、配置软链接
ln -s /usr/local/python3/bin/python3.9 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.9 /usr/bin/pip3
四、检测版本
python3 -V #查看python版本
pip3 list #查看pip3是否安装成功
五、常见报错
遇到报错:
python3: error while loading shared libraries: libpython3.9.so.1.0: cannot open shared object file: No such file or directory
#查看动态库情况
ldd /usr/local/python3/bin/python3.9
#源码目录复制过去
cp libpython3.9.so.1.0 /usr/lib/
ldconfig
六、永久设置pip3为国内镜像(可选)
1. 全局设置
pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple
pip3 config set install.trusted-host https://pypi.tuna.aliyun.com/pypi/simple
python3 -m pip install pip -U
2. 查看配置
# pip3 config list
global.index-url='https://mirrors.aliyun.com/pypi/simple'
install.trusted-host='https://pypi.tuna.aliyun.com/pypi/simple'