转至https://blog.csdn.net/Nemo____/article/details/83886733
此问题为pip损坏所致
执行pip --version 命令仍然报出此错误
Traceback (most recent call last):
File "/home/yr/anaconda3/bin/pip", line 8, in <module>
from pip._internal import main
File "/home/yr/anaconda3/lib/python3.7/site-packages/pip/_internal/__init__.py", line 40, in <module>
from pip._internal.cli.autocompletion import autocomplete
File "/home/yr/anaconda3/lib/python3.7/site-packages/pip/_internal/cli/autocompletion.py", line 8, in <module>
from pip._internal.cli.main_parser import create_main_parser
File "/home/yr/anaconda3/lib/python3.7/site-packages/pip/_internal/cli/main_parser.py", line 11, in <module>
from pip._internal.commands import (
File "/home/yr/anaconda3/lib/python3.7/site-packages/pip/_internal/commands/__init__.py", line 9, in <module>
from pip._internal.commands.download import DownloadCommand
File "/home/yr/anaconda3/lib/python3.7/site-packages/pip/_internal/commands/download.py", line 10, in <module>
from pip._internal.operations.prepare import RequirementPreparer
File "/home/yr/anaconda3/lib/python3.7/site-packages/pip/_internal/operations/prepare.py", line 9, in <module>
from pip._internal.distributions import (
File "/home/yr/anaconda3/lib/python3.7/site-packages/pip/_internal/distributions/__init__.py", line 1, in <module>
from pip._internal.distributions.source import SourceDistribution
ImportError: cannot import name 'SourceDistribution' from 'pip._internal.distributions.source' (/home/yr/anaconda3/lib/python3.7/site-packages/pip/_internal/distributions/source/__init__.py)
解决方法:
参照官网 https://packaging.python.org/tutorials/installing-packages/ 中的一段文字:
If pip isn’t already installed, then first try to bootstrap it from the standard library:
python -m ensurepip --default-pip
If that still doesn’t allow you to run pip:
Securely Download get-pip.py [1]
Run python get-pip.py. [2] This will install or upgrade pip. Additionally, it will install setuptools and wheel if they’re not installed already.
Warning Be cautious if you’re using a Python install that’s managed by your operating system or another package manager. get-pip.py does not coordinate with those tools, and may leave your system in an inconsistent state. You can use python get-pip.py --prefix=/usr/local/ to install in /usr/local which is designed for locally-installed software.
通过如上内容做如下操作:
1、 首先执行命令: python -m ensurepip --default-pip
2、 下载 get-pip.py 文件 地址为 https://bootstrap.pypa.io/get-pip.py
3、 最后从命令行进入到 get-pip.py 所在的目录,执行命令 : python get-pip.py
注意 此步骤执行的时候可能会报出权限错误, 此时应执行 python get-pip.py --user
执行 pip --version 查看pip是否安装完成。
亲测,解决了我的问题。