源码编译安装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()

截图如下

大功告成!

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

相关推荐
Hello_WOAIAI2 小时前
ImportError: DLL load failed while importing _ssl: 找不到指定的模块的解决方法
网络·网络协议·ssl
Dragon_qu·x3 小时前
Certbot 生成 SSL 证书并配置自动续期
运维·网络协议·https·ssl
Bob99983 小时前
电脑浏览器访问华为路由器报错,无法访问路由器web界面:ERR_SSL_VERSION_OR_CIPHER_MISMATCH 最简单的解决办法!
开发语言·javascript·网络·python·网络协议·华为·ssl
pzs02214 小时前
openssl的使用
openssl
Hello.Reader1 天前
使用 Nmap 进行 SSL/TLS 加密套件枚举
网络·网络协议·ssl
alsknv2 天前
只有IP地址没有域名怎么实现HTTPS访问?
网络·网络协议·tcp/ip·http·https·ssl
soladuor2 天前
xray 与 nginx 多网站共存(443端口复用方案)
nginx·https·ssl
小亦小亦_空中接力2 天前
openssl+keepalived安装部署
openssl·keepalived
DropLin3 天前
华为云免费ssl证书申请
网络协议·华为云·ssl
喜欢猪猪3 天前
Kafka支持SSL/TLS协议技术深度解析
分布式·kafka·ssl