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.
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.
- Run the command
$ sudo apt-get install libbz2-dev libc6-dev libgdbm-dev libncursesw5-dev libreadline-gplv2-dev libsqlite3-dev libssl-dev tk-devin the terminal to install the packages that Python and SSL required.
- 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.
- Run the command
./configurein the above archive file unzipped directory.
- Run the command
make && sudo make installto build the Python and install it to your Ubuntu.
- Now you can run the command
pip install pcakage-nameto install python packages on your Ubuntu.
6.1.2 For Windows you can follow the below steps.
- If you encounter such an error on Windows, it is mainly because the
libssl-1_1-x64.dllinstalled on your Windows has been replaced by another program that you have installed later.
- 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.
- 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.