CentOS7 编译安装 Python 3.10

一、安装 python3.6

如果需要 Python3.6,那不要编译,直接 yum 安装最省事。

# CentOS 7 yum 可以直接安装 Python 3.6.8
yum install python3 

二、编译安装 Python 3.10.10

1、安装依赖

yum groups install Development\ Tools 
yum install -y ncurses-devel gdbm-devel xz-devel sqlite-devel tk-devel uuid-devel readline-devel bzip2-devel libffi-devel gcc-c++ zlib zlib-devel openssl pcre curl-devel

2、下载 Python 源码

到 Python 官网下载 Python3.10.10 的 linux 运行环境。

Python 3.10.10 下载地址:https://www.python.org/downloads/release/python-31010/

wget https://www.python.org/ftp/python/3.10.10/Python-3.10.10.tgz

3、解压安装:

# 解压
tar -zxvf Python-3.10.10.tgz

# 配置,--prefix=/usr/local/python3.10,指定安装目录
# 个人感觉 prefix 参数不加比较好,这样就不需要配置下面的环境
cd Python-3.10.10
./configure --prefix=/usr/local/python3.10 --enable-optimizations

# 编译安装,-j$(nproc) ,多job加快编译
make -j$(nproc)
make install -j$(nproc)

4、配置环境

# 修改 profile 文件
vi /etc/profile

# profile 文件末尾增加下面信息
export PYTHON_HOME=/usr/local/python3.10
export PATH=$PYTHON_HOME/bin:$PATH

# 配置生效
source /etc/profile

5、创建软连接

该步骤替代原来 Python3,根据实际情况,这步可以不用做。

ln -s /usr/local/python3/bin/python3.10 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.10 /usr/bin/pip3

6、验证

python3 或 python3.10
pip3 或 pip3.10

三、两个编译异常解决

1、make 时 SystemError 异常

异常提示:

Could not import runpy module
Traceback (most recent call last):
  File "/usr/local/lib/python/3.8.3/Lib/runpy.py", line 15, in <module>
    import importlib.util
  File "/usr/local/lib/python/3.8.3/Lib/importlib/util.py", line 14, in <module>
    from contextlib import contextmanager
  File "/usr/local/lib/python/3.8.3/Lib/contextlib.py", line 4, in <module>
    import _collections_abc
SystemError: <built-in function compile> returned NULL without setting an error
generate-posix-vars failed
make[1]: *** [pybuilddir.txt] Error 1
make[1]: Leaving directory `/usr/local/lib/python/3.8.3'
make: *** [profile-opt] Error 2
[guest@kvmvps 3.8.3]$

导致原因:

低版本的 gcc(如 CentOS 7 默认 gcc4.8.5)编译时,--enable-optimizations 参数会出现上面问题

解决方法一:去除参数

# configure 时去除 --enable-optimizations 参数
cd Python-3.10.10
./configure --prefix=/usr/local/python3.10
make -j$(nproc)
make install -j$(nproc)

解决方法二:升级 GCC

gcc 7 及以后版本修复了此问题,进行升级gcc,此方法可以安装 gcc7-gcc11。

1、安装scl源

yum install -y centos-release-scl

2、安装 gcc、gcc+

yum install -y devtoolset-8-gcc devtoolset-8-gcc-c++

3、gcc8 生效:

# 当次生效
scl enable devtoolset-8 bash

# 永久生效
echo "source /opt/rh/devtoolset-7/enable" >>/etc/profile
source /etc/profile

4、查看版本:

# 查看 gcc 位置
which gcc
/opt/rh/devtoolset-8/root/usr/bin/gcc

# 确认 gcc 版本
gcc -v

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-8/root/usr --mandir=/opt/rh/devtoolset-8/root/usr/share/man --infodir=/opt/rh/devtoolset-8/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-8.3.1-20190311/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC) 

5、重新编译安装 Python

cd Python-3.10.10
./configure --prefix=/usr/local/python3.10 --enable-optimizations

# 编译安装,-j$(nproc) ,多job加快编译
make -j$(nproc)
make install -j$(nproc)

2、make 时报 ssl 1.1.1 module 错误

Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer

Openssl 版本低,需要安装 Openssl 1.1.1,需要编译安装。

从 OpenSSL 的官方网站下载源代码,进行编译安装。下载地址:https://www.openssl.org/source/

注意:在执行 ./config 的时候加上 --prefix=/usr/local/openssl 参数,方便 Python3.10 编译使用。步骤如下:

wget https://www.openssl.org/source/openssl-1.1.1u.tar.gz
tar xzvf openssl-1.1.1u.tar.gz
cd openssl-1.1.1u
./config --prefix=/usr/local/openssl shared
make
make install

回到 Python 3.10 解压目录重新编译安装。

cd Python-3.10.10
./configure --prefix=/usr/local/python3.10 \
    --with-openssl=/usr/local/openssl \
    --with-openssl-rpath=no \
    --enable-optimizations

make -j$(nproc) && make install -j$(nproc)

全文均已验证

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值