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

Page content

CentOS7 编译安装 Python310 过程

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

安装 Python3.10.x 编译环境

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

Python 3.10.2 使用环境依赖

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

添加openssl1.1.1环境变量

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

编译安装 Python3.10.x

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

 1$ wget -c https://www.python.org/ftp/python/3.10.6/Python-3.10.6.tar.xz && \
 2    tar xf Python-3.10.6.tar.xz && \
 3    cd Python-3.10.6 && \
 4    sed -i 's/PKG_CONFIG openssl /PKG_CONFIG openssl11 /g' configure && \
 5    ./configure --prefix=/usr/local/python310 \
 6        --enable-shared \
 7        --enable-optimizations && \
 8    make -j && \
 9    make altinstall
10
11# 验证
12$ /usr/local/python310/bin/python3 --version
13Python 3.10.6

配置国内pip镜像源

 1$ cat <<EOF | sudo tee ~/.pip/pip.conf
 2[global]
 3index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
 4extra-index-url=
 5        http://pypi.douban.com/simple/
 6        http://mirrors.aliyun.com/pypi/simple/
 7
 8[install]
 9trusted-host=
10        pypi.tuna.tsinghua.edu.cn
11        pypi.douban.com
12        mirrors.aliyun.com
13
14#proxy = [user:passwd@]proxy.server:port
15#ssl_verify: false
16EOF