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

When I import the 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 Correctly.

First, I use the below command to check whether the openssl package has been installed correctly or not. Then I found the 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 the 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 the python source code directory and rebuild the 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
>>>

6. Question & Answer.

6.1 SSL module is not available in the current python installation.

After installing Python successfully, I find I can not use pip to install python packages. My Python version is 3.7 and the pip install package-name command return an error message that said the SSL module is not available in the current python installation.

This error is similar to the error in this article, below are the steps to fix it in different OS.

6.1.1 For Ubuntu you can follow the below steps.
  1. Run the command $ sudo apt-get install libbz2-dev libc6-dev libgdbm-dev libncursesw5-dev libreadline-gplv2-dev libsqlite3-dev libssl-dev tk-dev in the terminal to install the packages that Python and SSL required.
  2. Download the python source file from https://www.python.org/ftp/python/, save the download file to a local folder and unzip the archive file.
  3. Run the command ./configure in the above archive file unzipped directory.
  4. Run the command make && sudo make install to build the Python and install it to your Ubuntu.
  5. Now you can run the command pip install pcakage-name to install python packages on your Ubuntu.
6.1.2 For Windows you can follow the below steps.
  1. If you encounter such an error on Windows, it is mainly because the libcrypto-1_1-x64.dll or libssl-1_1-x64.dll installed on your Windows has been replaced by another program that you have installed later.
  2. The above two DLL files are saved in the directory Windows\System32. You can replace them back manually if you have another machine that can run the pip install command without the error.
  3. If you have only one machine, you can install win 32 openssl again, and the win 32 OpenSSL installation will replace the above DLL files with the newest version.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.