Debian

在 Debian 上使用 update-alternatives 切換到 Python3.7.9 時 Pip 中斷

  • September 30, 2021

我正在使用 python3.9 附帶的 Debian 11。但是我不需要,我需要python3.7。所以,我安裝了 pythonn3.7.9 並更新了替代品。

問題是當我將 python 版本切換到 3.7.9 時,我無法使用 pip 安裝任何東西。pip install我在命令中收到以下錯誤:

sudo pip3 install requests
[sudo] password for deby: 
Traceback (most recent call last):
 File "/usr/bin/pip3", line 10, in <module>
   from importlib.metadata import distribution
ModuleNotFoundError: No module named 'importlib.metadata'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "/usr/bin/pip3", line 13, in <module>
   from importlib_metadata import distribution
ModuleNotFoundError: No module named 'importlib_metadata'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 584, in _build_master
   ws.require(__requires__)
 File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 901, in require
   needed = self.resolve(parse_requirements(requirements))
 File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 792, in resolve
   raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.VersionConflict: (pip 20.1.1 (/usr/local/lib/python3.7/site-packages), Requirement.parse('pip==20.3.4'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
 File "/usr/bin/pip3", line 15, in <module>
   from pkg_resources import load_entry_point
 File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3261, in <module>
   @_call_aside
 File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3245, in _call_aside
   f(*args, **kwargs)
 File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3274, in _initialize_master_working_set
   working_set = WorkingSet._build_master()
 File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 586, in _build_master
   return cls._build_from_requirements(__requires__)
 File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 599, in _build_from_requirements
   dists = ws.resolve(reqs, Environment())
 File "/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py", line 787, in resolve
   raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pip==20.3.4' distribution was not found and is required by the application

但是當我切換回 Python3.9 時,Pip 就可以工作了。我不知道如何解決這個問題。我在Google上查了一下,但找不到任何人遇到類似問題的問題。

在此處輸入圖像描述

第一個異常ModuleNotFoundError: No module named 'importlib_metadata'指向 pip 無法找到在 Python 3.8 中添加的importlib.metadata模組,因此來自 Debian 儲存庫的 pip 與您手動安裝的 Python 3.7 不兼容。您可以手動安裝舊版本的 pip,但我建議使用 Python 虛擬環境,它也可以使用不同的 Python 版本進行設置。

所以你可以做類似的事情

$ python3.7 -m venv python37
$ source python37/bin/activate

並使用您安裝的 Python 3.7 版本,而無需實際更改/usr/bin/python3系統範圍內的內容並可能破壞 pip 之外的其他內容。

引用自:https://unix.stackexchange.com/questions/671277