Could not build the ssl module! 原因与解决方法

今天我在Centos6.8上面安装pyhton3.7.6的时候,当我执行以下命令后:

./configure
make
make install

结果,报了如下错误,具体如下:

Could not build the ssl module!

Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().

LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381

中文翻译

Python需要具有X509_VERIFY_PARAM_set1_host()的OpenSSL 1.0.2或1.1兼容libssl。

LibreSSL 2.6.4和更早版本不提供必要的API,https://github.com/libressl-portable/portable/issues/381

Could not build the ssl module

 

原因:

大概意思就是:python与目前系统上的openssl不兼容,只兼容OpenSSL 1.0.2版本或OpenSSL 1.1以上版本的 libssl。

具体为什么高版本的OpenSSL要用到libssl,这是因为openssl的漏洞问题,感兴趣的朋友可以百度一下。

 

如果我们用python3.7,不解决这个“Could not build the ssl module! ”的问题,你还会报如下这两种错误:

 

1、python调用相关代码的时候,会报错如下:

Traceback (most recent call last):
  File "/tmp/tmp.sZQObjwJDI/fetch.py", line 21, in <module>
    import ssl
  File "/usr/local/lib/python3.7/ssl.py", line 98, in <module>
    import _ssl             # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'

ModuleNotFoundError: No module named '_ssl'

 

2、pip安装相关模块的时候,会报错如下:

WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting ssl
  WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/ssl/
  WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/ssl/
  WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/ssl/
  WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/ssl/
  WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/ssl/
  Could not fetch URL https://pypi.org/simple/ssl/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/ssl/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
  ERROR: Could not find a version that satisfies the requirement ssl (from versions: none)
ERROR: No matching distribution found for ssl
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

 ERROR: Could not find a version that satisfies the requirement ssl (from versions: none) ERROR: No matching distribution found for ssl

 

因此:你想直接忽略这个错误,是行不通的。

 

解决方法

 

第一步:安装 OpenSSL

从 OpenSSL 的官方网站 https://www.openssl.org/ 下载源代码。此处使用“openssl-1.1.1a.tar.gz”。

 

依次执行以下命令进行安装:

#tar xzvf openssl-1.1.1a.tar.gz
#cd openssl-1.1.1a
#./config --prefix=/usr/local/openssl shared
#make
#make install

 

注意,在执行 ./config 的时候一定要指定 --prefix=/usr/local/openssl 参数,方便后面使用。

 

验证 OpenSSL 安装时报以下异常信息:

#openssl version
openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory

中文翻译

openssl:加载共享库时出错:libssl.so.1.1:无法打开共享库文件:没有这样的文件或目录

 

这个是因为 OpenSSL 库的位置没有在系统共享 Lib 目录中导致的。通过创建软连接方式解决。

#ln -s /usr/local/opensslbbssl.so.1.1 /usrb64bssl.so.1.1
#ln -s /usr/local/opensslbbcrypto.so.1.1 /usrb64bcrypto.so.1.1
#openssl version
OpenSSL 1.1.1d 10 Sep 2019

 

最后一行表示安装成功。因为出来了openssl的版本。

 

第二步:安装 LibreSSL

从 LibreSSL 官网 http://www.libressl.org/ 下载源代码。此处使用“libressl-2.8.2.tar.gz”。

 

以此执行以下命令安装:

#wget https://ftp.openbsd.org/pub/OpenBSD/LibreSSL/libressl-2.8.2.tar.gz
#tar xzvf libressl-2.8.2.tar.gz
#cd libressl-2.8.2
#./config
#make
#make install

 

此时再验证 OpenSSL 版本,如下:

#openssl version
LibreSSL 2.8.2

 

第三步:修改 Python源代码 安装配置文件

在 Python3.7.6源代码目录下修改配置文件 Modules/Setup.dist,将以下前面的#注释去掉。

注意:Modules/Setup.dist 需要先“make”才能生成,如果以前有生成,可以先用“make clean”清理掉,再“make”命令一下。

#SSL=/usr/local/ssl
#_ssl _ssl.c \
#        -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
#        -L$(SSL)/lib -lssl -lcrypto

Modules/Setup.dist

 

第四步:安装 Python

开始安装前需要先设置以下环境变量:

export LDFLAGS=”-L/usr/local/openssl/lib”
export CPPFLAGS=”-I/usr/local/openssl/include”
export PKG_CONFIG_PATH=”/usr/local/openssl/lib/pkgconfig”

注意:

这里如果你设置后,提示“gcc: ”-I/usr/local/openssl/include”: No such file or directory”这种类似的情况,第2条命令就不用操作了,如果已经操作了,只需要退出来,重新登陆服务器就可以了。

export 的效力仅限于该次登陆操作。

 

再安装一些依赖包:

# yum -y install bzip2 bzip2-devel ncurses openssl openssl-devel openssl-static xz lzma xz-devel sqlite sqlite-devel gdbm gdbm-devel tk tk-devel libffi-devel

 

最后来安装python,命令如下:

# wget https://www.python.org/ftp/python/3.7.1/Python-3.7.6.tar.xz
# xz -d Python-3.7.6.tar.xz
# tar xvf Python-3.7.6.tar
# ./configure --prefix=/usr/local/Python3.7.6
# make
# make install

 

安装成功后,或许你需要做一些软链接:

# ln -s /usr/local/Python3.7.6 /usr/bin/python
# ln -s /usr/local/Python3.7.6 /usr/bin/python3
# ln -s /usr/local/bin/pip3.7 /usr/bin/pip3

 

第五步:测试 Python3 https 请求

此处需要取消证书验证。

#python3
Python 3.7.6 (default, Nov 28 2018, 17:42:41)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> import urllib.request
>>> context = ssl._create_unverified_context()
>>> urllib.request.urlopen('https://www.sogou.com/',context=context).read()

测试成功!

    A+
发布日期:2020年02月06日 20:52:06  所属分类:Python
最后更新时间:2020-02-07 10:56:52
付杰
  • ¥ 498.0元
  • 市场价:998.0元
  • ¥ 69.0元
  • 市场价:99.0元
  • ¥ 68.0元
  • 市场价:168.0元
  • ¥ 999.0元
  • 市场价:1599.0元

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: