每当我试图使用pip安装任何包时,我都会得到这个导入错误:

guru@guru-notebook:~$ pip3 install numpy
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'

guru@guru-notebook:~$ cat `which pip3`
#!/usr/bin/python3
# GENERATED BY DEBIAN

import sys

# Run the main entry point, similarly to how setuptools does it, but because
# we didn't install the actual entry point from setup.py, don't use the
# pkg_resources API.
from pip import main
if __name__ == '__main__':
    sys.exit(main())

它之前工作得很好,我不知道为什么它会抛出这个错误。 我已经搜索了这个错误,但找不到任何方法来修复它。

如果你需要进一步的细节,请让我知道,我会更新我的问题。


当前回答

您可以使用update-alternatives简单地修复PIP和pip3路径

首先要检查的是当前的$PATH 执行echo $PATH,查看是否可以找到pip3和pip通常所在的/usr/local/bin

有一个变化,您的系统在这里查找/bin/pip和/bin/pip 所以我将通过添加到你的~/来修复PATH。Bash_profile文件,因此它会持续存在

导出路径= $路径:/ usr /地方/ bin 然后检查它是用哪个PIP和哪个pip3固定的

如果没有,最后使用update-alternatives来修复它

update-alternatives --install /bin/pip3 pip3 /usr/local/bin/pip3 30

如果你想把PIP指向pip3,那么

update-alternatives --install /bin/pip pip /usr/local/bin/pip3 30

其他回答

我在Ubuntu 16.04系统上遇到了同样的问题。我设法用以下命令重新安装pip来修复它:

Curl https://bootstrap.pypa.io/get-pip.py | sudo python3

我使用sudo apt删除python3-pip,然后pip工作。

 ~ sudo pip install pip --upgrade
[sudo] password for sen: 
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'
➜  ~ sudo apt remove python3-pip   
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libexpat1-dev libpython3-dev libpython3.5-dev python-pip-whl python3-dev python3-wheel
  python3.5-dev
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  python3-pip
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 569 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 215769 files and directories currently installed.)
Removing python3-pip (8.1.1-2ubuntu0.4) ...
Processing triggers for man-db (2.7.5-1) ...
➜  ~ pip

Usage:   
  pip <command> [options]

上面的命令对我没用,但它们非常有用:

sudo apt purge python3-pip
sudo rm -rf '/usr/lib/python3/dist-packages/pip'  
sudo apt install python3-pip
cd
cd .local/lib/python3/site-packages
sudo rm -rf pip*  
cd
cd .local/lib/python3.5/site-packages
sudo rm -rf pip*  
sudo pip3 install jupyter

你可以试试这个:

sudo ln -sf $( type -P pip ) /usr/bin/pip

从pip._internal导入main

from pip._internal import main

编辑pip代码

sudo nano /usr/bin/pip3