pip 命令中的 SSLError("Can't connect to HTTPS URL because the SSL module is not available.")

SSLError("Can't connect to HTTPS URL because the SSL module is not available.") in pip command

在我的Ubuntu 20.04。我正在使用两个 python 版本。其中之一是我的 Ubuntu 安装附带的 Python3.8.2,另一个是 Python3.7.5。我使用 update-alternatives 安装了 Python3.7.5 和系统默认版本。但现在的问题是没有 pip 命令在 Python3.7.5 上运行。尽管 pip 在此 (Python3.7.5) 安装中可用,并且在打印版本时它显示以下内容(使用命令 pip3.7 -V):

pip 19.2.3 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

但是每当我尝试使用它安装软件包时,总是会显示标题中提到的错误。例如,在安装以下软件包时:

sudo pip3.7 install intel-tensorflow==1.15.2

抛出以下错误:

WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Collecting intel-tensorflow==1.15.2
  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/intel-tensorflow/
  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/intel-tensorflow/
  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/intel-tensorflow/
  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/intel-tensorflow/
  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/intel-tensorflow/
  Could not fetch URL https://pypi.org/simple/intel-tensorflow/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/intel-tensorflow/ (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 intel-tensorflow==1.15.2 (from versions: none)
ERROR: No matching distribution found for intel-tensorflow==1.15.2
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

为什么会发生这种情况? 无论我要安装哪个模块,所有 pip3.7 安装都显示相同的错误。每当我使用系统默认 python 版本(Python3.8.2)时也没有这样的问题。

TL;DR 您可能缺少一些系统依赖项。 sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget

阅读下文,了解我们如何到达那里的全部细节。

错误指出 SSL python 模块不可用;这意味着您要么没有安装合适的 ssl 库(可能没有,因为您声明系统 python 可以 pip install fine),或者您从源代码构建或以其他方式安装的 python 不包括ssl 模块。

如果您从源代码构建它,您需要确保设置配置选项--with-openssl

此外,我真的要警告不要使用 sudo pip 安装任何东西。使用 virtualenv 之类的东西从系统 python 或您安装的其他 python 版本中分离 python 环境。

编辑:

经过仔细检查,假设 dev headers 存在,python 配置脚本似乎默认启用 ssl 支持。确保所有开发 headers 都存在 sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget,然后重试配置和构建 python。