非root用户编译Python源码时共享库文件缺失问题求助
我以非root用户在Linux下编译Python3.12源码,提前从源码编译了依赖的OpenSSL(用于pip)和libffi头文件,指定安装到$HOME/.local目录。但执行make后出现以下错误:
[ERROR] _ctypes failed to import: libffi.so.8: cannot open shared object file: No such file or directory
[ERROR] _hashlib failed to import: libcrypto.so.3: cannot open shared object file: No such file or directory
[ERROR] _ssl failed to import: libssl.so.3: cannot open shared object file: No such file or directory
The following modules are disabled in configure script:
_sqlite3The necessary bits to build these optional modules were not found:
_curses _curses_panel _dbm
_gdbm _lzma _tkinter
nis readline
To find the necessary bits, look in configure.ac and config.log.Following modules built successfully but were removed because they could not be imported:
_ctypes _hashlib _sslCould not build the ssl module!
Python requires a OpenSSL 1.1.1 or newerChecked 111 modules (31 built-in, 67 shared, 1 n/a on linux-x86_64, 1 disabled, 8 missing, 3 failed on import)
我需要正常使用_ctypes等模块,目前无法满足需求。
已执行步骤
- 编译libffi:从源码编译,配置命令为
./configure --prefix=$HOME/.local,按指引安装到$HOME/.local。 - 编译OpenSSL:从源码编译,配置命令指定
--prefix=$HOME/.local,同样安装到该目录。 - 当前
.local目录结构:
├── bin │ ├── c_rehash │ └── openssl ├── include │ ├── ffi.h │ ├── ffitarget.h │ └── openssl │ └── <loads of header files>.h ├── lib │ ├── libcrypto.so │ ├── libffi.so │ ├── libssl.so │ └── pkgconfig │ └── libffi.pc ├── lib64 │ ├── cmake │ │ └── OpenSSL │ │ ├── OpenSSLConfig.cmake │ │ └── OpenSSLConfigVersion.cmake │ ├── engines-3 │ │ ├── afalg.so │ │ ├── capi.so │ │ ├── loader_attic.so │ │ └── padlock.so │ ├── libcrypto.a │ ├── libcrypto.so │ ├── libcrypto.so.3 │ ├── libffi.a │ ├── libffi.la │ ├── libffi.so │ ├── libffi.so.8 │ ├── libffi.so.8.1.3 │ ├── libssl.a │ ├── libssl.so │ ├── libssl.so.3 │ ├── ossl-modules │ │ └── legacy.so │ └── pkgconfig │ ├── libcrypto.pc │ ├── libssl.pc │ └── openssl.pc ├── share │ ├── ... ...
- 编译Python3.12:从源码编译,配置命令参考相关建议:
./configure --enable-optimizations --with-lto --with-openssl=$HOME/.local \ --prefix=$HOME/.local/ \ LIBFFI_INCLUDEDIR=$HOME/.local/include \ LDFLAGS="-L$HOME/.local/lib/../lib64 -L$HOME/.local/lib64" \ CFLAGS="-I$HOME/.local/include"
配置阶段检查libffi和OpenSSL显示正常,编译命令为:
make LIBFFI_INCLUDEDIR=$HOME/.local/include
执行后出现上述错误。
注:我是Linux新手
解决方法
问题核心是编译Python时,动态链接器找不到$HOME/.local/lib64下的共享库——即使编译阶段能找到头文件和库文件,make阶段的模块导入检查仍会失败。
按以下步骤重新操作:
设置环境变量,让动态链接器识别本地库
在执行configure和make前,先运行:export LD_LIBRARY_PATH="$HOME/.local/lib64:$HOME/.local/lib:$LD_LIBRARY_PATH" export PKG_CONFIG_PATH="$HOME/.local/lib64/pkgconfig:$HOME/.local/lib/pkgconfig:$PKG_CONFIG_PATH"若要永久生效,可将这两行添加到
~/.bashrc或~/.zshrc(对应你使用的shell),再执行source ~/.bashrc使配置立即生效。清理旧编译文件并重新配置Python
进入Python源码目录,先清理残留文件:make distclean然后重新执行配置命令(利用环境变量自动查找依赖):
./configure --enable-optimizations --with-lto --with-openssl=$HOME/.local \ --prefix=$HOME/.local \ CFLAGS="-I$HOME/.local/include" \ LDFLAGS="-L$HOME/.local/lib64 -L$HOME/.local/lib"无需单独指定
LIBFFI_INCLUDEDIR,环境变量和CFLAGS已包含头文件路径,pkgconfig会自动帮Python找到libffi的配置。编译并安装
执行编译(-j$(nproc)利用多线程加速):make -j$(nproc)完成后安装:
make install验证模块可用性
安装完成后,重新打开终端(或source配置文件),运行:python3.12 -c "import _ctypes; import _ssl; print('Success')"若输出
Success,说明_ctypes、_ssl模块已正常工作。
另外,对于那些缺失的可选模块(如_curses、readline等),若有需要,可通过源码编译对应依赖到$HOME/.local,或联系系统管理员安装相关开发包。
内容的提问来源于stack exchange,提问作者bart

