源码编译安装python3.12没有ssl模块,python3.12 ModuleNotFoundError: No module named ‘_ssl‘

已经编译安装了opensll 3.3

源码编译安装python3.12没有ssl模块,python3.12 ModuleNotFoundError: No module named '_ssl'

报错如下:

bash 复制代码
[root@localhost ~]# python
Python 3.12.5 (main, Sep  9 2024, 10:33:15) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl;
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/python3.12/lib/python3.12/ssl.py", line 100, in <module>
    import _ssl             # if we can't import it, let the error propagate
    ^^^^^^^^^^^
ModuleNotFoundError: No module named '_ssl'

**重点:**报错最根本的原因是第一次安装openssl的时候,没有指定libdir,必须指定--libdir=lib,不指定默认会把库放到lib64下面的,python3会找不到库,然后报上面的错

网上很多乱七八糟的安装方法,最常见的是./Configure --prefix=/usr/local/openssl/,这样安装出来的是有问题的,安装的时候会把lib放到lib64,这样python3就找不到openssl要用的库了

还有一点要注意:自定义的openssl地址一定要放在/urs/local下,放在其他路径,也是无法找到库的

看一下官网的安装步骤是这样的

贴一下代码在这里,openssl-VERSION就是你下载的openssl 版本

bash 复制代码
curl -O https://www.openssl.org/source/openssl-VERSION.tar.gz

tar xzf openssl-VERSION

pushd openssl-VERSION

./config \
    --prefix=/usr/local/custom-openssl \
    --libdir=lib \
    --openssldir=/etc/ssl

make -j1 depend

make -j8

make install

###*********我自己是这样设置参数安装的***************
tar -xvf openssl-3.3.1.tar
cd openssl-3.3.1/
./Configure --prefix=/usr/local/openssl/ --libdir=lib --openssldir=/etc/ssl
make -j 4 && make install

安装完成之后,是这样的(lib64是第一次安装没指定libdir留下的,没删除)

python3的安装步骤

Build Python with custom OpenSSL (see the configure --with-openssl and --with-openssl-rpath options)

bash 复制代码
pushd python-3.x.x
./configure -C \
    --with-openssl=/usr/local/custom-openssl \
    --with-openssl-rpath=auto \
    --prefix=/usr/local/python-3.x.x

make -j8

make altinstall
###**********我自己安装**************
tar -xvf Python-3.12.5.tar
cd Python-3.12.5
./configure --prefix=/usr/local/python3.12 --with-openssl=/usr/local/openssl
make -j 4 && make install
###做个软件链接
ln -s /usr/local/python3.12/bin/python3.12 /usr/bin/python3

安装完是这样的

不影响原来python2的使用

测试SSL模块

bash 复制代码
[root@localhost data]# python3
Python 3.12.5 (main, Sep 10 2024, 15:16:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl;
>>> print(ssl.OPENSSL_VERSION);
OpenSSL 3.3.1 4 Jun 2024
>>>
>>> exit()

截图如下

大功告成!

还是官网靠谱,看了网上别人写的安装方法照着装,结果保存半天找不到原因

相关推荐
Lsetea13 小时前
OpenSSL verify报unable to get issuer certificate:error 2与partial_chain排查
linux·https·ssl证书·openssl·证书链
青春不朽51215 小时前
requests 超时、重试、SSL 报错?9 个高频问题与一整张排错对照表
网络·网络协议·ssl
天天进步201517 小时前
Caddy 实战:5 分钟给域名开启 HTTPS,自动申请 SSL 证书并反向代理
网络协议·https·ssl
Lsetea1 天前
OpenSSL verify报unhandled critical extension:error 34与关键扩展排查
运维·https·ssl证书·openssl·证书链
Lsetea2 天前
OpenSSL verify报CA certificate key too weak:auth_level与弱密钥链排查
运维·https·ssl证书·openssl·证书链
熊文豪3 天前
KingbaseES 数据加密全解:从 SSL 到全密态
网络·网络协议·ssl
idanzk3 天前
Git 推送 GitHub 报 SSL_READ /src refspec main 不匹配 完整踩坑记录
git·github·ssl
javaDocker3 天前
16.5 小时,我打掉了两只「SSL 握手失败」的怪
网络·网络协议·ssl
Lsetea4 天前
OpenSSL verify报invalid CA certificate:basicConstraints与keyCertSign排查
运维·https·ssl证书·openssl·证书链
Lsetea5 天前
OpenSSL verify报error 62:证书主机名不匹配与-verify_hostname排查
运维·https·ssl证书·openssl·san