前言

  由于python3.10之后版本不在支持libressl使用ssl,需要使用openssl安装来解决编译安装
python时候遇到的ssl模块导入失败的问题,这里需要用的openssl1.1.1版本或者更高版本

在这里插入图片描述

编译安装openssl

下载地址
参见https://www.openssl.org/,包括以下版本:
https://www.openssl.org/source/openssl-3.1.0-alpha1.tar.gz
https://www.openssl.org/source/openssl-1.1.1s.tar.gz
https://www.openssl.org/source/openssl-3.0.7.tar.gz
编译安装
注:编译之前请先确保系统中安装了make以及gcc的软件包。,编译安装前确认/usr/include/openssl//为空
tar -zxf openssl-1.1.1s.tar.gz
cd openssl-1.1.1s/
./config -fPIC --prefix=/usr/include/openssl enable-shared
make
make install

其中:
 -fPIC: 位置无关代码
 --prefix=: 路径 一般选 /usr/include/openssl
 enbale-shared: 动态库

安装openssl3.0.7问题
  安装openssl3.0.7时候报错,查看对应文件显示模块缺少,需要加载导入模块

在这里插入图片描述

报错原因: 缺少IPC/Cmd.pm模块

解决方法:
安装perl-CPAN

$ yum install -y perl-CPAN

进入CPAN的she模式,首次进入需要配置shel,按照提示操作即可

 $ perl -MCPAN -e shell

在shell中安装缺少的模块,确定是cpan[1]算是进入了操作命令入口

cpan[1]> install IPC/Cmd.pm

在这里插入图片描述
退出界面回到shell

cpan[1]> quit
  安装成功后,重新编译OpenSSL即可

编译安装python3.11.2

下载地址

python下载地址

https://www.python.org/ftp/python/3.11.2/Python-3.11.2.tgz
编译安装

需要修改解压后的python Moudle/Setup文件

tar -xf Python-3.11.2.tar.xz
cd Python-3.11.2/
需要修改解压后的python Moudle/Setup文件
# To statically link OpenSSL:
- # _ssl _ssl.c $(OPENSSL_INCLUDES) $(OPENSSL_LDFLAGS) \
+ _ssl _ssl.c $(OPENSSL_INCLUDES) $(OPENSSL_LDFLAGS) \
- #    -l:libssl.a -Wl,--exclude-libs,libssl.a \
+     -l:libssl.a -Wl,--exclude-libs,libssl.a \
- #    -l:libcrypto.a -Wl,--exclude-libs,libcrypto.a
+     -l:libcrypto.a -Wl,--exclude-libs,libcrypto.a
- # _hashlib _hashopenssl.c $(OPENSSL_INCLUDES) $(OPENSSL_LDFLAGS) \
+ _hashlib _hashopenssl.c $(OPENSSL_INCLUDES) $(OPENSSL_LDFLAGS) \
- #    -l:libcrypto.a -Wl,--exclude-libs,libcrypto.a
+     -l:libcrypto.a -Wl,--exclude-libs,libcrypto.a

编译安装,注意configure命令
此处参考了:configure配置

chmod +x configure
mkdir /usr/local/python-3.11.2
./configure --prefix=/usr/local/python-3.11.2 --with-zlib=/usr/include/ --with-openssl-rpath=auto  --with-openssl=/usr/include/openssl  OPENSSL_LDFLAGS=-L/usr/include/openssl   OPENSSL_LIBS=-l/usr/include/openssl/ssl OPENSSL_INCLUDES=-I/usr/include/openssl
make -j 4
make install
验证是否成功安装ssl模块
/usr/local/python-3.11.2/bin/python3 -V
/usr/local/python-3.11.2/bin/python3 
# 执行下面命令不报错既正常
>>import _ssl
>>
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐