源码编译安装python3.12没有ssl模块,python3.12 ModuleNotFoundError: No module named ‘_ssl‘
已经编译安装了opensll 3.3
源码编译安装python3.12没有ssl模块,python3.12 ModuleNotFoundError: No module named ‘_ssl’
报错如下:
[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 版本
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)
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模块
[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()
截图如下
大功告成!
还是官网靠谱,看了网上别人写的安装方法照着装,结果保存半天找不到原因