Python3 import ssl error resolved Summary

I. Case

[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'
>>> 

Problem Solution
(1) ./Modules/_ssl.c:57:25: error: openssl / rsa.h: No such file or directory

Solution: Check the version openssl: openssl version

[root@instance-38r7isl1 Python-3.7.0]# openssl version
OpenSSL 1.0.1e-fips 11 Feb 2013
  • Download the premium version :( directory / 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
  • Installation compile 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
  • Configuration software connection
[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
  • Modify Modules folder Python3 installation directory Setup files and Setup.dist file, open the relevant configuration ssl and modify SSL directory
# 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

  • Reinstall compiled 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
  • Finished test
[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
Published 40 original articles · won praise 31 · views 620 000 +

Guess you like

Origin blog.csdn.net/weixin_38422258/article/details/104430673