Python3 import ssl报错解决方法

# requests支持https访问必须升级openssl为libressl
# 安装libressl依赖:
yum -y install libffi-devel
yum -y install zlib zlib-devel
yum -y install bzip2 bzip2-devel
yum -y install ncurses ncurses-devel
yum -y install readline readline-devel
yum -y install openssl openssl-devel
yum -y install openssl-static
yum -y install xz lzma xz-devel
yum -y install sqlite sqlite-devel
yum -y install gdbm gdbm-devel
yum -y install tk tk-devel
yum -y install wget

# 编译安装libressl-2.8.0
下载libressl-2.8.0.tar.gz
https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/
wget https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.8.0.tar.gz
tar xf libressl-2.8.0.tar.gz
cd libressl-2.8.0
./config --prefix=/usr/local/libressl && make && make install
rm -rf libressl-2.8.0 libressl-2.8.0.tar.gz
mv /usr/bin/openssl /usr/bin/openssl.bak
mv /usr/include/openssl /usr/include/openssl.bak
ln -sf /usr/local/libressl/bin/openssl /usr/bin/openssl
ln -sf /usr/local/libressl/include/openssl /usr/include/openssl
ln -sf /usr/local/libressl/lib/libssl.so.45 /usr/lib/
ln -sf /usr/local/libressl/lib/libcrypto.so.43 /usr/lib/

echo '/usr/local/libressl/lib' >/etc/ld.so.conf.d/libressl-2.8.0.conf
ldconfig -v #重新加载库文件

进入解压后的目录libressl-2.8.0
./config --prefix=/usr/local/libressl
make && make install
mv /usr/bin/openssl /usr/bin/openssl.bak
mv /usr/include/openssl /usr/include/openssl.bak
ln -sf /usr/local/libressl/bin/openssl /usr/bin/openssl
ln -sf /usr/local/libressl/include/openssl /usr/include/openssl
ln -sf /usr/local/libressl/lib/libssl.so.45 /usr/lib/
ln -sf /usr/local/libressl/lib/libcrypto.so.43 /usr/lib/

新建文件
vim /etc/ld.so.conf.d/libressl-2.8.0.conf
#将以下行加入文件,并保存
/usr/local/libressl/lib
重新加载库文件
ldconfig -v

重新编译python(requests不需要重新编译, ssl需要)先修改python源码的Module/Setup。需要将默认注释掉的关于ssl的5行取消,大约在53%处。

# CSV file helper
#_csv _csv.c
 
# 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=/usr/local/libressl
_ssl _ssl.c 
    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl 
    -L$(SSL)/lib -lssl -lcrypto

./configure --prefix=/usr/local/python3.8 --enable-shared --enable-optimizations --with-openssl=/usr/local/libressl

参考链接:
      https://www.cnblogs.com/mengzhilva/p/11059329.html
      https://blog.csdn.net/yw804909465/article/details/106426261/
      https://www.dazhuanlan.com/2019/12/23/5e003e2e9ac89/
      https://blog.csdn.net/Scorpio921/article/details/82682757

原文地址:https://www.cnblogs.com/xwupiaomiao/p/14788566.html