You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CentOS7.9下编译Python3.13时自定义ncurses路径配置问题

CentOS 7.9编译Python 3.13的ncurses配置及运行问题解决

问题描述

我在CentOS 7.9系统中编译Python 3.13,ncurses编译库位于非标准路径~/custom/gcc-14.2/lib/。按配置文档定义CURSES_LIBS后,执行configure时未检测到ncurses相关文件;make时出现WINDOW类型未定义等编译错误;make install后Python无法运行,提示找不到encodings模块。请问正确定义CURSES_LIBS的方式是什么?还有其他需要配置的项吗?

原配置命令

# 使用自定义编译器
export CC=gcc-14.2 CXX=g++-14.2

# 当前目录为Python3.13.0根目录,在子目录编译
mkdir build && cd build

CURSES_LIBS="-L$HOME/custom/gcc-14.2" ../configure \
    --prefix=$HOME/custom/gcc-14.2 \
    --enable-loadable-sqlite-extensions \
    --enable-optimizations --with-lto \
    --with-platlibdir=lib64 \
    --with-ensurepip=install \
    --enable-shared 

configure检测输出片段

checking for ncursesw... no
checking for ncurses... no
checking for ncursesw/curses.h... no
checking for ncursesw/ncurses.h... no
checking for ncursesw/panel.h... no
checking for ncurses/curses.h... no
checking for ncurses/ncurses.h... no
checking for ncurses/panel.h... no
checking for curses.h... no
checking for ncurses.h... no

make编译错误片段

[...]

gcc-14.2 -pthread  -fno-strict-overflow -Wsign-compare -DNDEBUG -g -O3 -Wall    -fno-semantic-interposition -flto -fuse-linker-plugin -ffat-lto-objects -flto-partition=none -g -std=c11 -Wextra -Wno-unused-parameter -Wno-missing-field-initializers -Wstrict-prototypes -Werror=implicit-function-declaration -fvisibility=hidden -fprofile-generate -I../Include/internal -I../Include/internal/mimalloc -IObjects -IInclude -IPython -I. -I../Include   -fPIC -fPIC -c ../Modules/_cursesmodule.c -o Modules/_cursesmodule.o
In file included from ../Modules/_cursesmodule.c:116:
../Include/py_curses.h:80:5: error: unknown type name ‘WINDOW’
   80 |     WINDOW *win;
      |     ^~~~~~
../Modules/_cursesmodule.c:173:36: error: ‘FALSE’ undeclared here (not in a function)
  173 | static int initialised_setupterm = FALSE;
      |                                    ^~~~~
../Modules/_cursesmodule.c: In function ‘PyCursesCheckERR’:
../Modules/_cursesmodule.c:213:17: error: ‘ERR’ undeclared (first use in this function); did you mean ‘ERA’?
  213 |     if (code != ERR) {
      |                 ^~~
      |                 ERA

[...]

../Modules/_cursesmodule.c: In function ‘_curses_start_color_impl’:
../Modules/_cursesmodule.c:4228:1: warning: control reaches end of non-void function [-Wreturn-type]
 4228 | }
      | ^
../Modules/_cursesmodule.c: In function ‘_curses_meta_impl’:
../Modules/_cursesmodule.c:3624:1: warning: control reaches end of non-void function [-Wreturn-type]
 3624 | }
      | ^
make[2]: *** [Modules/_cursesmodule.o] 错误 1
make[1]: *** [profile-gen-stamp] 错误 2
make: *** [profile-run-stamp] 错误 2

运行错误信息

$ ~/custom/gcc-14.2/bin/python3.13 -c 'print("Hello")'
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Fatal Python error: Failed to import encodings module
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007fd963713780 (most recent call first):
  <no Python frame>

解决方案

一、正确配置ncurses相关环境变量

仅指定库路径不足以让configure找到完整的ncurses依赖,需同时提供头文件路径和具体链接库:

  1. 设置头文件与库链接参数
export CURSES_CFLAGS="-I$HOME/custom/gcc-14.2/include"
export CURSES_LIBS="-L$HOME/custom/gcc-14.2/lib -lncursesw -ltinfo"
  • CURSES_CFLAGS:告知configure ncurses头文件的存放路径(如curses.h、ncurses.h)
  • CURSES_LIBS:指定库路径的同时,明确链接libncursesw.so(Python优先依赖的宽字符版本)和libtinfo.so(ncurses依赖的终端信息库)
  1. 重新执行configure
    先清理旧编译目录,再运行配置命令:
export CC=gcc-14.2 CXX=g++-14.2
export CURSES_CFLAGS="-I$HOME/custom/gcc-14.2/include"
export CURSES_LIBS="-L$HOME/custom/gcc-14.2/lib -lncursesw -ltinfo"

rm -rf build && mkdir build && cd build

../configure \
    --prefix=$HOME/custom/gcc-14.2 \
    --enable-loadable-sqlite-extensions \
    --enable-optimizations --with-lto \
    --with-platlibdir=lib64 \
    --with-ensurepip=install \
    --enable-shared 

配置完成后,检查输出应包含checking for ncursesw... yes,说明ncurses检测通过。

二、解决编译阶段潜在问题

若仍出现编译错误,需确认:

  • 自定义ncurses库是否编译了宽字符支持(编译ncurses时需添加--enable-widec参数)
  • 若ncurses为静态库,需在CURSES_LIBS中指定静态库文件路径,或添加-static参数

三、解决encodings模块丢失问题

该错误是Python运行时无法定位核心模块导致的,按以下步骤修复:

  1. 设置共享库路径
    因启用了--enable-shared,需让系统找到Python的共享库:
export LD_LIBRARY_PATH=$HOME/custom/gcc-14.2/lib64:$LD_LIBRARY_PATH

可将该命令添加到~/.bashrc中,避免每次手动设置。

  1. 验证安装完整性
    检查$HOME/custom/gcc-14.2/lib64/python3.13目录下是否存在encodings文件夹。若缺失,建议先去掉--enable-optimizations --with-lto参数重新编译(优化选项可能导致编译异常),验证基础版本正常后再添加优化。

  2. 手动指定Python环境变量(可选)
    若仍无法运行,手动指定Python的home和模块路径:

export PYTHONHOME=$HOME/custom/gcc-14.2
export PYTHONPATH=$HOME/custom/gcc-14.2/lib64/python3.13

四、额外注意事项

  • CentOS 7.9系统库版本较老,使用gcc-14.2编译时,需确保zlib、openssl等依赖也用同版本gcc编译,避免链接冲突
  • 编译前可安装系统基础依赖:yum install -y bzip2-devel openssl-devel readline-devel sqlite-devel,减少基础依赖缺失问题
  • LTO选项对编译环境要求较高,若出现链接错误,可先禁用该选项

内容的提问来源于stack exchange,提问作者PlasmaBinturong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 00:53:11