ImportError: No module named _ssl解决方法

import ssl时出现ImportError: No module named _ssl错误是因为咱安装Python的时候没有把ssl模块编译进去导致的。

解决步骤:

系统没有openssl,手动安装openssl
1.下载openssl,地址为http://www.openssl.org/source/openssl-1.0.2a.tar.gz
2.安装:
tar -xzvf openssl-1.0.2a.tar.gz
./config --prefix=/usr/local --openssldir=/usr/local/openssl
make && make install

3.在/usr/local目录下找到lib64和include目录,(注意openssl的库是被安装到lib还是lib64,这步很重要)
找到路径/usr/local/lib64、/usr/local/include,后面的步骤会用到这两个路径
[root@Linux local]# pwd
/usr/local
[root@Linux local]# ll
drwxr-xr-x. 2 root root 4096 11月 13 13:59 bin
drwxr-xr-x. 3 root root 4096 11月 13 13:59 include
drwxr-xr-x. 2 root root 4096 11月 1 2011 lib
drwxr-xr-x. 4 root root 4096 11月 13 13:59 lib64
drwxr-xr-x. 2 root root 4096 11月 1 2011 libexec
drwxr-xr-x. 2 root root 4096 11月 1 2011 sbin

4.下载Python安装包并解压
tar -xzf Python-2.7.15.tg
cd Python-2.7.15

5.在Modules找到Setup.dist文件,按如下步骤修改,使编译Python的时候能找到刚才安装的openssl的库
1)找到SSL相关配置
#SSL=/usr/local/ssl
#_ssl _ssl.c \
# -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
# -L$(SSL)/lib -lssl -lcrypto

2) 由于openssl是被安装在/usr/local目录下的lib64和include目录的不是安装在/usr/local/ssl目录,所有把步骤1)找到的4行的注释去掉,如下修改
SSL=/usr/local
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib64 -lssl -lcrypto

6.编译安装Python,并创建软连接
./configure --prefix=/usr/local/python27
make
make install
ln -s /usr/local/python27/bin/python2 /usr/bin/python27

7.测试ssl是否能正常使用
[root@Linux Python-2.7.15]# python27
Python 2.7.15 (default, Nov 13 2018, 15:36:49)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18.0.7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>

————————————————
版权声明:本文为CSDN博主「xiemanR」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xiemanR/article/details/85224509

猜你喜欢

转载自www.cnblogs.com/VinsonYang/p/12333560.html