python3.7 ImportError: No module named _ssl 解决方法

笔者在 centos6.5 安装 python3.7 碰到此问题,安装好以后,执行 python3.7 命令行,import ssl 出现错误 ImportError: No module named _ssl 。 该错误表现在 pip install 时会报 pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

在网上找了很多资料,经过验证,在编译前修改 python 源码的方式(Modules/Setup.dist)是不奏效的。 升级 openssl1.1.x之后的版本是有效的。

解决步骤如下:

一、下载 openssl 编译安装

openssl 官方下载地址:https://www.openssl.org/source/

我当时安装的版本是: openssl-1.1.1c


tar xf openssl-1.1.1c.tar.gz
cd openssl-1.1.1c
./config --prefix=/usr/local/openssl-1.1.1c
make
make install

二、配置 openssl 共享库

'''新建文件'''
vim /etc/ld.so.conf.d/openssl.conf

'''填入如下内容'''
/usr/local/openssl-1.1.1c/lib/

'''保存'''

'''更新共享库'''
ldconfig

'''检验 openssl-1.1.1 是否已加入共享库'''
ldconfig -v | grep ssl

/usr/local/openssl-1.1.1c/lib:
    libssl.so.1.1 -> libssl.so.1.1    '''有这个表示加成功了'''
    libssl3.so -> libssl3.so
    libssl.so.10 -> libssl.so.1.0.1e

三、安装 python3.7

cd /usr/local/python3.7/
./configure --prefix=/usr/local/python3.7 --with-openssl=/usr/local/openssl-1.1.1c
make && make install

猜你喜欢

转载自blog.51cto.com/tchuairen/2435472