一、案例
[root@instance-38r7isl1 Python-3.7.0]# python3
Python 3.7.0 (default, Feb 21 2020, 16:38:35)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python3/lib/python3.7/ssl.py", line 98, in <module>
import _ssl # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
>>>
问题解决方案
(1) ./Modules/_ssl.c:57:25: error: openssl/rsa.h: No such file or directory
解决方案:查看openssl版本:openssl version
[root@instance-38r7isl1 Python-3.7.0]# openssl version
OpenSSL 1.0.1e-fips 11 Feb 2013
- 下载高级版本:(目录/usr/local/openssl)
[root@instance-38r7isl1 openssl] wget https://www.openssl.org/source/openssl-1.1.1-pre8.tar.gz
[root@instance-38r7isl1 openssl] tar -vxzf openssl-1.1.1-pre8.tar.gz
- 安装编译openssl
[root@instance-38r7isl1 openssl-1.1.1-pre8] ./config --prefix=/usr/local/openssl no-zlib
[root@instance-38r7isl1 openssl-1.1.1-pre8] make
[root@instance-38r7isl1 openssl-1.1.1-pre8] make install
- 配置软连接
[root@instance-38r7isl1 openssl-1.1.1-pre8] ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl
[root@instance-38r7isl1 openssl-1.1.1-pre8] ln -sf /usr/local/openssl/include/openssl /usr/include/openssl
- 修改Python3安装目录下Modules文件夹中的Setup文件和Setup.dist文件,打开ssl的相关配置,并修改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=/usr/local/openssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
# The crypt module is now disabled by default because it breaks builds
# on many systems (where -lcrypt is needed), e.g. Linux (I believe).
#_crypt _cryptmodule.c # -lcrypt # crypt(3); needs -lcrypt on some systems
- 重新安装编译Python3
[root@instance-38r7isl1 Python-3.7.0]# ./configure --prefix=/usr/local/python3
[root@instance-38r7isl1 Python-3.7.0]# make
[root@instance-38r7isl1 Python-3.7.0]# make install
- 测试完成
[root@instance-38r7isl1 Modules]# python3
Python 3.7.0 (default, Feb 21 2020, 17:27:43)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>
(2) openssl error while loading shared libraries: libssl.so.1.1
/usr/local/openssl/bin/openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory
ln -s /usr/local/openssl/lib/libssl.so.1.1 /usr/lib64/libssl.so.1.1
ln -s /usr/local/openssl/lib/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1
转载:https://blog.csdn.net/weixin_38422258/article/details/104430673
查看评论