案例说明:在Django搭建的博客上进行邮件密码找回,当在输入框中输入邮件之后网页报错:no module named _ssl;此时进入到python环境,我的python环境是3.6版本,尝试import ssl,返回错误:ModuleNotFoundError: No module named ‘_ssl’;开始按照网上的一些处理方式,安装好了需要的软件包后,先编辑python软件包中的Setup.dist文件,去掉指定四行代码的注释再重新编译python,但是python3中依旧无法导入ssl模块,但是试了下python2环境下是可以成功导入,在Google上找了不少资料得以成功解决,步骤如下:
1.安装依赖库
yum install -y zlib zlib-dev openssl-devel sqlite-devel bzip2-devel libffi libffi-devel gcc gcc-c++
2.修改python安装目录中的Setup.dist
vi /file/Python-3.6.0/Modules/Setup.dist
3.将以下五行注释取消
_socket socketmodule.c
SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
4.设置环境变量LD_LIBRARY_PATH
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/openssl/lib" >> $HOME/.bash_profile
source $HOME/.bash_profile
5.重新编译
./configure --prefix=/usr/local/python3.6 --with-openssl=/usr/local/openssls
make && make install
6.查看效果
[root@mail Python-3.6.0]# python
Python 3.6.0 (default, Sep 20 2019, 20:23:48)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> exit()