Anaconda构建的GCC11版Python启动出现段错误的解决咨询
问题描述
我通过Anaconda搭建了多个Python版本,最近启动Python时碰到了Segmentation fault(段错误)问题。我怀疑这和构建Python用的GCC版本有关:之前安装的、用GCC 9和GCC 10构建的Python运行正常,但GCC 11构建的Python一启动就报段错误。
现在我的Anaconda似乎只能安装GCC 11构建的Python。我试过创建仅包含Python的全新干净环境、重新安装Anaconda,还执行了conda clean -a命令,问题仍未解决。请问如何让Python在当前Anaconda安装环境下正常运行?
出现段错误的Python示例
$ ./python Python 3.8.17 | packaged by conda-forge | (default, Jun 16 2023, 07:06:00) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. Segmentation fault
$ ./python Python 3.9.16 | packaged by conda-forge | (main, Feb 1 2023, 21:39:03) [GCC 11.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. Segmentation fault
运行正常的Python示例
$ ./python Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:20:46) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
$ ./python Python 3.7.12 | packaged by conda-forge | (default, Oct 26 2021, 06:08:53) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
$ ./python Python 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:10) [GCC 10.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
系统默认GCC版本
$ gcc --version gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44) Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
创建Python环境的命令示例
conda create -n py39 -c conda-forge python=3.9
解决方案
1. 指定安装旧版本GCC构建的Python包
直接指定对应GCC9/10构建的具体Python版本安装:
# 安装GCC9构建的Python3.9.7 conda create -n py39_old -c conda-forge python=3.9.7 # 安装GCC10构建的Python3.8.13 conda create -n py38_old -c conda-forge python=3.8.13
2. 锁定conda编译器版本偏好
编辑~/.condarc文件,添加以下配置,强制conda优先选择GCC9构建的包:
channel_priority: strict conda_build: compiler_override: c: gcc-9 cxx: g++-9 fortran: gfortran-9
3. 使用Anaconda官方频道安装
尝试切换到Anaconda官方频道,其提供的Python包通常适配更广泛的旧系统环境:
conda create -n py39_stable -c anaconda python=3.9
4. 排查系统库兼容性
段错误可能源于系统旧版本库(如glibc)与GCC11构建包不兼容:
- 执行
ldd ./python查看Python依赖的系统库 - 对比正常与出错Python的依赖差异,重点关注glibc版本。若系统glibc版本过低(如2.17及以下),只能选择旧版本GCC构建的Python。
内容的提问来源于stack exchange,提问作者Kwonil
相关产品推荐
相关产品推荐

