How To Resolve Could Not Find SSL Module Error After Install Python

When i import python ssl module just after python is installed, it is prompted that ssl module cannot be found like below. This article will tell you how to resolve it.

$ python
Python 2.7.15 (default, Oct 23 2018, 18:08:43) 
>>> import ssl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/python27/lib/python2.7/ssl.py", line 60, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: No module named _ssl
>>>

1. Verify Openssl Package Has Been Installed Correctlly.

First, i use below command to check whether python openssl package has been installed correctly or not. Then i found package openssl-devel is missing.

$ rpm -aq|grep openssl
openssl-0.9.8e-20.el5
openssl-0.9.8e-20.el5

2. Install openssl-devel Package With Yum.

$ yum install openssl-devel -y

3. Check openssl-devel Package Installation Result.

Now check openssl package again, you can find openssl-devel has been installed.

$ rpm -aq|grep openssl
openssl-devel-1.0.1e-57.el6.x86_64
openssl-1.0.1e-57.el6.x86_64

4. Recompile Python.

Edit python setup file /src/Python-2.7.15/Modules/Setup with below source code.

# Socket module helper for socket(2)
_socket socketmodule.c timemodule.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/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto

Then go to python source code directory and rebuild python executable.

$ cd /src/Python-2.7.15/
$ make
$ make install

5. Import Python ssl Module Again.

Now import ssl module in python interactive command console again, the error no module named _ssl will disappear.

$ python
Python 2.7.15 (default, Oct 23 2018, 19:08:43)
>>> import ssl
>>>