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

截图如下

大功告成!

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

相关推荐
XMYX-010 小时前
解决 Apache/WAF SSL 证书链不完整导致的 PKIX path building failed 问题
网络协议·apache·ssl
少陽君1 天前
什么是CA根证书
服务器·https·ssl
梦想blog2 天前
漏洞修复 Nginx TLSSSL 弱密码套件
运维·nginx·ssl·漏洞修复·tlsv1.2
yong15858553432 天前
利用 openssl api 实现 TLS 双向认证
linux·ssl
深耕AI3 天前
Win64OpenSSL-3_5_2.exe【安装步骤】
openssl
XYiFfang3 天前
【Python+requests】解决Python requests中的ProxyError:SSL版本错误问题详解
python·debug·ssl·常见错误·代理配置
看那山瞧那水3 天前
DELPHI 利用OpenSSL实现加解密,证书(X.509)等功能
delphi·openssl
Linux运维技术栈5 天前
Linux系统部署:Certbot 实现 Nginx 自动续期&部署 Let‘s Encrypt 免费 SSL 证书
linux·运维·nginx·ssl·certbot
m0_474606787 天前
Nginx + Certbot配置 HTTPS / SSL 证书(简化版已测试)
nginx·https·ssl
神色自若9 天前
AbpVnext 阿里云ssl证书多个生产环境自动更新
服务器·阿里云·ssl