1. 查看OpenSSL是否存在或者版本是否符合要求
首先使用openssl version
命令查看OpenSSL 是否安装好,若安装好查看版本是否符合要求,安装python3.7,3.8 至少需要OpenSSL1.0.2版本
2. 升级OpenSSL
wget http://www.openssl.org/source/openssl-1.0.2r.tar.gz
tar zxvf openssl-1.0.2r.tar.gz
./config --prefix=~/openssl1.0.2r --openssldir=~/openssl1.0.2r/openssl no-zlib #这里的安装目录可以按照自己需要修改
cd openssl-1.0.2r
make && make install
echo "export LD_LIBRARY_PATH=~/openssl1.0.2r/lib:$LD_LIBRARY_PATH" >> ~/.bashrc
source ~/.bashrc
3.重新编译安装python3.7或3.8
这里以python3.7为例
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
tar xvf Python-3.7.4.tar.xz
cd Python-3.7.4
./configure --prefix=~/Python3.7.4
vim ./Modules/Setup
在Setup文件中搜索SSL,找到相关内容替换成下面的!
# Socket module helper for socket(2)
_socket socketmodule.c
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=~/openssl1.0.2r
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
注意: 对于python3.8需要把第2行换成_socket socketmodule.c timemodule.c
保存退出后就可以编译安装啦!
make && make install
echo "export PATH=~/Python3.7.4/bin:$PATH" >> ~/.bashrc
source ~/.bashrc
最后用pip3安装就没有报错啦!
注意:本文安装软件的目录都是默认的当前用户目录下即 ~/