问题现象:
在linux中安装python后,使用pip安装第三方模块报错:
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
问题原因
- 当前行系统没有 openssl 或者版本比较低,一般 python3.7 需要的openssl的版本为1.0.2或者1.1.x。需要对 openssl 进行升级,并重新编译 python3.7.0。
- 在编译前需要修改Modules/Setup文件。
解决过程
升级openssl
# 解压缩
tar -zxvf openssl-3.0.12.tar.gz
# 编译安装
cd openssl-3.0.12/
./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib
make
make install
# 备份原配置
mv /usr/bin/openssl /usr/bin/openssl.20230119.bak
mv /usr/include/openssl /usr/include/openssl.20230119.bak
# 进行配置
echo "/usr/local/ssl/lib64" > /etc/ld.so.conf.d/openssl.conf
cp /usr/local/openssl/bin/openssl /usr/bin/openssl
# 刷新内存
ldconfig -v
# 查看版本
openssl version
返回结果:OpenSSL 3.0.12 24 Oct 2023 (Library: OpenSSL 3.0.12 24 Oct 2023)
安装python3.11.6
tar -zxvf Python-3.11.6.tgz
cd Python-3.11.6/
vi Modules/Setup
# 大概在218行,找到To statically link OpenSSL一行,将下面的内容注释都打开,然后保存退出
218 # To statically link OpenSSL:
219 _ssl _ssl.c $(OPENSSL_INCLUDES) $(OPENSSL_LDFLAGS) \
220 -l:libssl.a -Wl,--exclude-libs,libssl.a \
221 -l:libcrypto.a -Wl,--exclude-libs,libcrypto.a
222 _hashlib _hashopenssl.c $(OPENSSL_INCLUDES) $(OPENSSL_LDFLAGS) \
223 -l:libcrypto.a -Wl,--exclude-libs,libcrypto.a
# 编译安装python
./configure --prefix=/app/python3.11.6 --with-openssl=/usr/include/openssl
make
make install
# 验证
python3.11
import _ssl
# 不报错即可