编译Python 3.14主分支时遇ModuleNotFoundError: 'encodings'错误求助
编译Python主分支/3.14分支时遭遇
encodings模块缺失错误 环境信息
- ~/.pyenv/versions中已安装Python 3.13.7
- Clang版本:19.1.7
- 系统Python版本:3.10.12
- 系统:Ubuntu 22.04.5 LTS
编译操作
克隆Python官方仓库主分支并创建新分支后,执行以下编译命令:
./configure CC=clang --prefix=$HOME/python3.14_1 --enable-optimizations --with-lto --enable-experimental-jit make -j$(nproc)
错误日志
执行make时触发以下错误:
. . . Current thread 0x000077860849b740 [_bootstrap_pyth] (most recent call first): <no Python frame> Fatal Python error: make[2]: *** [Makefile:1943: Python/frozen_modules/io.h] Error 1 make[2]: *** Waiting for unfinished jobs.... Failed to import encodings module Python runtime state: core initialized ModuleNotFoundError: No module named 'encodings' Current thread 0x00007d427a677740 [_bootstrap_pyth] (most recent call first): <no Python frame> make[2]: *** [Makefile:1952: Python/frozen_modules/genericpath.h] Error 1 Fatal Python error: Failed to import encodings module Python runtime state: core initialized ModuleNotFoundError: No module named 'encodings' Current thread 0x000075b2c53fc740 [_bootstrap_pyth] (most recent call first): <no Python frame> make[2]: *** [Makefile:1940: Python/frozen_modules/codecs.h] Error 1 make[2]: *** [Makefile:1946: Python/frozen_modules/_collections_abc.h] Error 1 make[2]: *** [Makefile:1937: Python/frozen_modules/abc.h] Error 1 make[2]: *** [Makefile:1949: Python/frozen_modules/_sitebuiltins.h] Error 1 make[2]: *** [Makefile:1958: Python/frozen_modules/posixpath.h] Error 1 make[2]: *** [Makefile:1955: Python/frozen_modules/ntpath.h] Error 1 make[2]: Leaving directory '/home/s265d007/cpython' make[1]: *** [Makefile:1003: profile-gen-stamp] Error 2 make[1]: Leaving directory '/home/s265d007/cpython' make: *** [Makefile:1015: profile-run-stamp] Error 2
排查尝试
执行以下排查命令时触发运行时错误:
./_bootstrap_python -S -c "import encodings; print(encodings.__file__)" \ PYTHONPATH=./Lib
错误输出:
Fatal Python error: Run filename expected Python runtime state: preinitialized
切换到3.14分支编译,仍出现相同错误。
可能的解决方向
- 调整configure参数:暂时禁用
--enable-optimizations和--with-lto,先完成基础编译,再逐步添加优化参数。优化选项可能导致编译过程中依赖处理异常:./configure CC=clang --prefix=$HOME/python3.14_1 --enable-experimental-jit make clean make -j$(nproc) - 修正PYTHONPATH设置方式:排查命令中环境变量位置错误,正确的设置应该放在命令前:
PYTHONPATH=./Lib ./_bootstrap_python -S -c "import encodings; print(encodings.__file__)" - 补全编译依赖:确保Ubuntu系统上的Python编译依赖完整,执行:
sudo apt-get update sudo apt-get install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev - 彻底清理编译缓存:清除之前的编译残留文件后重新配置编译:
make distclean ./configure CC=clang --prefix=$HOME/python3.14_1 --enable-optimizations --with-lto --enable-experimental-jit make -j$(nproc)
内容的提问来源于stack exchange,提问作者satya




