Python3.10.x源码编译安装过程及遇到的问题处理

CentOS7 编译安装 Python310 过程

从Python3.10开始,其依赖的openssl版本也同步进行了升级,必须为v1.1.1及以上。 OpenSSL在python环境中被用于加密相关功能,大多数使用场景都会用到,所以这个问题我们还必须要解决下。本来想源码安装openssl-1.1.1q,发现互联网前辈已经有制做好的rpm包,那就省事了,接下来我们就记录下整个安装过程。

安装 Python3.10.x 编译环境

$ yum install epel-release -y
$ yum groupinstall "Development tools" -y

Python 3.10.2 使用环境依赖

$ yum install bzip2-devel ncurses-devel gdbm-devel xz-devel \
    sqlite-devel libffi-devel libuuid-devel readline-devel \
    zlib-devel openssl-devel bzip2-devel \
    openssl11 openssl11-devel -y

添加openssl1.1.1环境变量

$ echo "export CFLAGS=$(pkg-config --cflags openssl11)" >> /etc/bashrc
$ echo "export LDFLAGS=$(pkg-config --libs openssl11)" >> /etc/bashrc
$ source /etc/bashrc

编译安装 Python3.10.x

如果编译过程中出现 Could not import runpy module报错,取消--enable-optimizations,执行 make clean,再重新在编译一次Python即可。

$ wget -c https://www.python.org/ftp/python/3.10.6/Python-3.10.6.tar.xz && \
    tar xf Python-3.10.6.tar.xz && \
    cd Python-3.10.6 && \
    sed -i 's/PKG_CONFIG openssl /PKG_CONFIG openssl11 /g' configure && \
    ./configure --prefix=/usr/local/python310 \
        --enable-shared \
        --enable-optimizations && \
    make -j && \
    make altinstall

# 验证
$ /usr/local/python310/bin/python3 --version
Python 3.10.6

配置国内pip镜像源

$ cat <<EOF | sudo tee ~/.pip/pip.conf
[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
extra-index-url=
        http://pypi.douban.com/simple/
        http://mirrors.aliyun.com/pypi/simple/

[install]
trusted-host=
        pypi.tuna.tsinghua.edu.cn
        pypi.douban.com
        mirrors.aliyun.com

#proxy = [user:passwd@]proxy.server:port
#ssl_verify: false
EOF