我使用的是 Debian 11,它附带了 python3.9。但我不需要它,我需要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 就可以工作了。我不知道如何解决这个问题。我在谷歌上查了一下,但找不到有人遇到类似问题的问题。
答案1
第一个异常ModuleNotFoundError: No module named 'importlib_metadata'
指出 pip 无法找到导入库.元数据Python 3.8 中添加的模块,因此 Debian 存储库中的 pip 与您手动安装的 Python 3.7 不兼容。您可以手动安装旧版本的 pip,但我建议使用也可以设置的 Python 虚拟环境不同的Python版本。
所以你可以做类似的事情
$ python3.7 -m venv python37
$ source python37/bin/activate
并使用您安装的 Python 3.7 版本,而无需实际更改/usr/bin/python3
系统范围内的内容,也不会破坏除 pip 之外的其他内容。