python二进制依赖ssl版本比当前系统的高

问题

你的 Python3.10.9 SSL 模块存在,但依赖的 OpenSSL 版本缺失
报错里已经把真相亮出来了:

ImportError: libssl.so.1.1: cannot open shared object file: No such file or directory

说明:

  • Python3.10.9 在编译时链接到 OpenSSL 1.1
  • 当前系统没有 libssl.so.1.1 → 直接导致 _ssl 模块无法加载

这是典型的“动态链接的 OpenSSL 匹配不上”问题。


解决

方案一:安装 OpenSSL 1.1

CentOS8/Rocky9 默认是 OpenSSL 3,不包含 1.1你需要自己装兼容的 openssl11 包。

如果环境是 Rocky / CentOS Stream,这样做:

1
2
yum install https://vault.centos.org/8.5.2111/AppStream/x86_64/os/Packages/openssl11-1.1.1k-5.el8.x86_64.rpm
yum install https://vault.centos.org/8.5.2111/AppStream/x86_64/os/Packages/openssl11-libs-1.1.1k-5.el8.x86_64.rpm

验证:

1
ls -l /usr/lib64/libssl.so.1.1

如果文件存在,再执行:

1
python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"

通常会恢复正常。


方案二:自己下载 OpenSSL 1.1 并链接

适合封闭环境或系统限制安装。下载 OpenSSL 1.1.1:

1
2
3
4
5
6
7
wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar xf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w

./config --prefix=/usr/local/openssl11 --openssldir=/usr/local/openssl11
make -j$(nproc)
make install

建立软链给 Python:

1
2
ln -s /usr/local/openssl11/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/openssl11/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1

测试:

1
python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"

方案三:重新编译 Python 并静态绑定 OpenSSL

动态依赖 openssl 会随着系统更新损坏 Python。
生产上建议 让 Python 自带 OpenSSL,彻底去掉运行环境依赖。

编译流程如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar xf openssl-1.1.1w.tar.gz

cd openssl-1.1.1w
./config --prefix=/data/base/openssl11
make -j$(nproc)
make install

cd Python-3.10.9
./configure --prefix=/data/base/py3.10.9 \
--with-openssl=/data/base/openssl11 \
--enable-optimizations
make -j$(nproc)
make install

测试:

1
/data/base/py3.10.9/bin/python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"

这套 Python 不会受系统 OpenSSL 破坏。


Python SSL 动态依赖关系

graph TD
    A[Python3.10.9] --> B[_ssl.so 模块]
    B --> C[libssl.so.1.1]
    B --> D[libcrypto.so.1.1]

    C --> E[系统 OpenSSL 1.1 缺失,导致 ImportError]
    D --> E