簡體   English   中英

Installing SSL package with PIP requires SSL package to be already installed

[英]Installing SSL package with PIP requires SSL package to be already installed

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

我不得不升級一個軟件,它現在需要 Python 3.11。

我按照互聯網上的說明( https://linuxstans.com/how-to-install-python-centos/ ),現在安裝了 Python 3.11,但無法下載任何東西,所以所有與互聯網有關的程序,包括 PIP,因為 SSL package 沒有安裝,所以不能工作。

安裝 Python 包的正常方法是使用 PIP,這不起作用,因為我要安裝的 SSL package 沒有安裝。

我嘗試了互聯網上的所有建議,但它們都已過時且不再有效,因為它們要么不適用於 Python 的 3.11 版本,要么不適用於 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

我從https://pypi.org/simple/ssl/下載了 GZip 文件,在本地解壓縮並嘗試從本地源安裝它們,但是 PIP 堅持使用 Z0E8433F9A404F1F3BA601C14B22 愚蠢的工具連接。

該怎么辦?

如何在升級到 Python 3.11 后使 PIP 和其他基於 HTTPS 的 Python 程序正常工作:

首先:你不一定需要像pyenv這樣的神奇工具。 可能是 pyenv 會執行這些步驟,但我想了解發生了什么。 (好吧,我承認make也是一個“神奇”的工具)

簡要描述:在從源代碼編譯 Python 期間,可以選擇將 OpenSSL 支持直接注入其中。

在 CentOS 7 Python 中默認安裝 2.7.5 並且無法使用內置的 package 管理器更新到更高版本。 Python 3.6.8 是 CentOS 7 存儲庫中可用的最新版本。 3.6 也無法使用 package 管理器更新到更高版本。

因此唯一可能的解決方案是從源代碼編譯 Python。

  • 更新您的yum軟件包,重新啟動,安裝運行 OpenSSL 和 Python 所需的所有軟件包。
  • 下載最新的OpenSSL源碼,解壓編譯。
  • 下載最新的 Python 源代碼,解壓,配置使用編譯好的 OpenSSL 並使用 altinstall 參數編譯。 不要刪除以前的 Python 版本。 你會遇到比好處更多的問題。 我不得不多次將虛擬機恢復到最新的快照,因為我完全破壞了一些東西。

更新和安裝 yum 包

> yum update
> yum install openssl-devel bzip2-devel libffi-devel

一篇文章還建議安裝一些“開發工具”

> yum groupinstall "Development Tools"

但是這一步對我來說失敗了,我可以在沒有它的情況下完成安裝。

下載最新的OpenSSL源碼,解壓編譯

我選擇了/usr/src目錄來使用源代碼進行操作。

下載

> cd /usr/src
> wget https://ftp.openssl.org/source/openssl-1.1.1q.tar.gz --no-check-certificate

打開包裝

> tar -xzvf openssl-1.1.1q.tar.gz
> cd openssl-1.1.1q

編譯

> ./config --prefix=/usr --openssldir=/etc/ssl --libdir=lib no-shared zlib-dynamic
> make

對編譯后的 OpenSSL 運行測試

> make test

安裝

> make install

檢查是否安裝了 OpenSSL

> openssl version
OpenSSL 1.1.1q  5 Jul 2022
> which openssl
/usr/bin/openssl

下載編譯Python

下載

> cd /usr/src
> wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0a4.tgz

打開包裝

> tar -xzf Python-3.11.0a4.tgz
> cd Python-3.11.0a4

配置

> ./configure --enable-optimizations --with-openssl=/usr

重要的是,當您在上面配置 OpenSSL 時, --with-openssl選項與--prefix選項具有相同的值!!!

編譯並安裝(是時候喝杯咖啡了——這需要時間)

> make altinstall

檢查是否安裝了 Python 3.11:

> python3.11 -V
Python 3.11.0a4

如果您設置了符號鏈接,那么 Python 3.11 應該可以由“python3”和/或“python”別名調用

> python3 -V
Python 3.11.0a4
> python -V
Python 3.11.0a4

還要檢查 PIP 是否正常工作,以及它的符號鏈接別名是否在那里。

現在是時候檢查基於 Python 的程序是否正常工作了。 其中一些應該由 PIP 重新安裝,因為它們安裝在以前的 Python 版本的子目錄中。

完成這些操作后,我還收到了 SSL 證書錯誤:

<urlopen 錯誤 [SSL: CERTIFICATE_VERIFY_FAILED] 證書驗證失敗:無法獲取本地頒發者證書 (_ssl.c:998)>

運行后

> pip3 install certifi

問題消失了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM