当前位置: 代码网 > it编程>前端脚本>Python > CentOS7安装Python3.10遇到gcc版本过低怎么解决

CentOS7安装Python3.10遇到gcc版本过低怎么解决

2026年08月18日 Python 我要评论
一、安装 python3.6如果需要 python3.6,那不要编译,直接 yum 安装最省事。# centos 7 yum 可以直接安装 python 3.6.8yum install python

一、安装 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-8/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)

总结

全文均已验证

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com