'Python 3 No module named '_ssl'

The Problem

While I run you python3 application, it shows

  File "/usr/local/lib/python3.6/ssl.py", line 101, in <module>
import _ssl             # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'

What I've tried

install the dependencies

yum install openssl-devel

I also edited the setup.py file and recomplie python3

# Detect SSL support for the socket module (via _ssl)
search_for_ssl_incs_in = [
                          '/usr/local/ssl/include',
                          '/usr/local/include/openssl', #I've added this line
                          '/usr/contrib/ssl/include/'
                         ]

I've complied the openssl with the path configuration

#tar -xzvf openssl-***.tar.gz

#./config --prefix=/usr/local --openssldir=/usr/local/openssl

#make & make install

CentOS 7

Python 3.6



Solution 1:[1]

I found some solution:

if you use centos,try:

s1

yum install openssl-devel -y

then when you compile, append --with-ssl,just like this

./configure prefix=/usr/local/share/python3 --with-ssl

s2

-- install depend library, make share compile is fluent

yum install -y zlib zlib-devel openssl-devel sqlite-devel bzip2-devel libffi libffi-devel gcc gcc-c++

(ubuntu)sudo apt-get install libz-dev

wget --no-check-certificate http://www.openssl.org/source/openssl-1.1.1.tar.gz
tar -zxvf openssl-1.1.1.tar.gz
cd openssl-1.1.1
./config --prefix=$HOME/openssl shared zlib
make && make install

-- configure shared ld library path so that compile can find it

echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/openssl/lib" >> $HOME/.bash_profile
source $HOME/.bash_profile

(zsh user has some different with .zsh_profile) -- compile with openssl path

 ./configure prefix=/usr/local/share/python3 --with-openssl=$HOME/openssl

Solution 2:[2]

I faced the same issue, I installed python from source and didn't enabled ssl option while compiling. So I find the solution in the following article. You need to find ssl section Modules/Setup.dist and uncomment that section. Hope this will help someone.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 latsha