使用 PIP 安装 SSL 软件包需要已安装 SSL 软件包

Pau*_*aul 5 python ssl pip

  • CentOS 7(严格要求)
  • Python 3.11(严格要求)

我必须升级一个软件,它现在需要 Python 3.11。

我按照互联网上的说明(https://linuxstans.com/how-to-install-python-centos/),现在Python 3.11已安装,但无法下载任何东西,所以所有与互联网有关的程序,包括 PIP,由于未安装 SSL 软件包而无法工作。

安装 Python 包的正常方法是使用 PIP,但它不起作用,因为我要安装的 SSL 包尚未安装。

我尝试了互联网上的所有建议,但它们都已经过时并且不再起作用,因为它们要么不适用于 3.11 版本的 Python,要么不适用于 CentOS 7。

我在运行应用程序软件时遇到的错误:

ModuleNotFoundError:没有名为“_ssl”的模块

当我尝试使用 pip 安装 ssl 时:

# pip install --trusted-host pypi.org ssl
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
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
Run Code Online (Sandbox Code Playgroud)

我从https://pypi.org/simple/ssl/下载了 GZip 文件,在本地解压它们并尝试从本地源安装它们,但 PIP 坚持使用 HTTPS 连接......愚蠢的工具。

该怎么办?

Pau*_*aul 21

如何在升级到 Python 3.11 后让 PIP 和其他基于 HTTPS 的 Python 程序正常工作:

首先:您不一定需要任何像pyenv. 可能 pyenv 会执行这些步骤,但我想了解发生了什么。(好吧,我承认这make也是一个“神奇”的工具)

简要描述:在从源代码编译 Python 期间,可以选择将 OpenSSL 支持直接注入其中。

在 CentOS 7 中,默认安装 Python 2.7.5,并且无法使用内置包管理器更新到更高版本。Python 3.6.8 是 CentOS 7 存储库中提供的最新版本。3.6 也无法使用包管理器更新到更高版本。

所以唯一可能的解决方案是从源代码编译Python。

  • 更新您的yum软件包,重新启动,安装运行 OpenSSL 和 Python 所需的所有软件包。
  • 下载最新的OpenSSL源代码,解压并编译。
  • 下载最新的Python源代码,解压,配置使用编译好的OpenSSL并使用altinstall参数进行编译。不要删除以前的 Python 版本!你会遇到的问题多于好处。我不得不多次将虚拟机恢复到最新快照,因为我完全破坏了一些东西。

更新并安装 yum 软件包

> yum update
> yum install openssl-devel bzip2-devel libffi-devel
Run Code Online (Sandbox Code Playgroud)

一篇文章建议还安装一些“开发工具”

> yum groupinstall "Development Tools"
Run Code Online (Sandbox Code Playgroud)

但这一步对我来说失败了,我能够在没有它的情况下完成安装。

下载最新的OpenSSL源码,解压并编译

我选择了/usr/src目录来对源代码进行操作。

下载

> cd /usr/src
> wget https://ftp.openssl.org/source/openssl-1.1.1q.tar.gz --no-check-certificate
Run Code Online (Sandbox Code Playgroud)

打开包装

> tar -xzvf openssl-1.1.1q.tar.gz
> cd openssl-1.1.1q
Run Code Online (Sandbox Code Playgroud)

编译

> ./config --prefix=/usr --openssldir=/etc/ssl --libdir=lib no-shared zlib-dynamic
> make
Run Code Online (Sandbox Code Playgroud)

对已编译的 OpenSSL 运行测试

> make test
Run Code Online (Sandbox Code Playgroud)

安装

> make install
Run Code Online (Sandbox Code Playgroud)

检查是否安装了 OpenSSL

> openssl version
OpenSSL 1.1.1q  5 Jul 2022
> which openssl
/usr/bin/openssl
Run Code Online (Sandbox Code Playgroud)

下载并编译Python

下载

> cd /usr/src
> wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0a4.tgz
Run Code Online (Sandbox Code Playgroud)

打开包装

> tar -xzf Python-3.11.0a4.tgz
> cd Python-3.11.0a4
Run Code Online (Sandbox Code Playgroud)

配置

> ./configure --enable-optimizations --with-openssl=/usr
Run Code Online (Sandbox Code Playgroud)

重要的是该选项与上面配置 OpenSSL 时的选项--with-openssl具有相同的值!--prefix

编译并安装(该喝杯咖啡了 - 这需要时间)

> make altinstall
Run Code Online (Sandbox Code Playgroud)

检查Python 3.11是否已安装:

> python3.11 -V
Python 3.11.0a4
Run Code Online (Sandbox Code Playgroud)

如果您设置了符号链接,则 Python 3.11 应该可以通过“python3”和/或“python”别名调用

> python3 -V
Python 3.11.0a4
> python -V
Python 3.11.0a4
Run Code Online (Sandbox Code Playgroud)

还要检查 PIP 是否正常工作以及它的符号链接别名是否存在。

现在是时候检查基于 Python 的程序是否正常工作了。其中一些应该通过 PIP 重新安装,因为它们安装在以前的 Python 版本的子目录中。

完成这些操作后,我还收到 SSL 证书错误:

<urlopen 错误 [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:998)>

运行后

> pip3 install certifi
Run Code Online (Sandbox Code Playgroud)

问题就解决了。