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

非root用户编译Python源码时共享库文件缺失问题求助

非root用户编译Python3.12时_ctypes、_ssl等模块导入失败问题

我以非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:
_sqlite3

The 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 _ssl

Could not build the ssl module!
Python requires a OpenSSL 1.1.1 or newer

Checked 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阶段的模块导入检查仍会失败。

按以下步骤重新操作:

  1. 设置环境变量,让动态链接器识别本地库
    在执行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使配置立即生效。

  2. 清理旧编译文件并重新配置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的配置。

  3. 编译并安装
    执行编译(-j$(nproc)利用多线程加速):

    make -j$(nproc)
    

    完成后安装:

    make install
    
  4. 验证模块可用性
    安装完成后,重新打开终端(或source配置文件),运行:

    python3.12 -c "import _ctypes; import _ssl; print('Success')"
    

    若输出Success,说明_ctypes、_ssl模块已正常工作。

另外,对于那些缺失的可选模块(如_curses、readline等),若有需要,可通过源码编译对应依赖到$HOME/.local,或联系系统管理员安装相关开发包。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 14:31:02