the ssl module in Python is not available错误解决

时间:2021-11-06 02:58:10

在使用pip安装pymongo的过程中报错,提示如下:

 $ pip3 install pymongo
pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting pymongo
Could not fetch URL https://pypi.python.org/simple/pymongo/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping
Could not find a version that satisfies the requirement pymongo (from versions: )
No matching distribution found for pymongo

这是由于缺少ssl模块,进入python中验证。

$ python
Python 3.7.0a3 (default, Jan 10 2018, 14:36:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] 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/lib/python3.7/ssl.py", line 100, in <module>
import _ssl # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
>>>

原因找到:缺少openssl-devel包

$ rpm -qa | grep openssl
openssl-1.0.2k-8.el7.x86_64
xmlsec1-openssl-1.2.20-7.el7_4.x86_64
openssl-libs-1.0.2k-8.el7.x86_64

解决方法:

1、安装openssl-devel包

$ sudo yum install -y openssl-devel

2、重新编译安装pyhton

修改Setup配置

$ vi Modules/Setup
找到
# 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/ssl
#_ssl _ssl.c \
# -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
# -L$(SSL)/lib -lssl -lcrypto
改为
# 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/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto

编译安装

 ./configure &&  sudo make && sudo make install

验证:

$ python
Python 3.7.0a3 (default, Jan 10 2018, 15:13:22)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>

安装pymongo

$ sudo /usr/local/bin/pip3 install pymongo
Collecting pymongo
Downloading pymongo-3.6..tar.gz (581kB)
% |████████████████████████████████| 583kB 28kB/s
Installing collected packages: pymongo
Running setup.py install for pymongo ... done
Successfully installed pymongo-3.6.

安装完成