每当我试图使用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())

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

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


当前回答

我使用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 ln -sf $( type -P pip ) /usr/bin/pip

这对我很管用!

hash -r pip # or hash -d pip

现在,卸载pip安装的版本,并使用以下命令重新安装它。

python -m pip uninstall pip  # sudo
sudo apt install --reinstall python-pip

如果pip坏了,使用:

python -m pip install --force-reinstall pip

希望能有所帮助!

我也有同样的错误,但是python -m pip仍然在工作,所以我用核选项sudo python -m pip install -upgrade pip来修复它。它为我做到了。

我使用pip3修复错误的方法是:

Sudo cp -v /usr/local/bin/pip3 /usr/bin/pip3

所有的工作原理:

 demon@UbuntuHP:~$ pip -V
 pip 10.0.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

 demon@UbuntuHP:~$ pip2 -V
 pip 10.0.1 from /home/demon/.local/lib/python2.7/site-packages/pip (python 2.7)

 demon@UbuntuHP:~$ pip3 -V
 pip 10.0.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

也许新的10.0.1版本的pip没有更新/usr/bin中的二进制文件?(但事实似乎并非如此)

编辑:同样的问题出现在Ubuntu 18.04中。我发现的最佳解决方案是从/home/<user/链接pip二进制文件。Local /bin到/usr/ Local /bin或/usr/bin(取决于你的偏好),如下所示:

ln -sv /home/<user>/.local/bin/pip /usr/local/bin/pip
ln -sv /home/<user>/.local/bin/pip2 /usr/local/bin/pip2
ln -sv /home/<user>/.local/bin/pip2.7 /usr/local/bin/pip2.7
ln -sv /home/<user>/.local/bin/pip3 /usr/local/bin/pip3
ln -sv /home/<user>/.local/bin/pip3.6 /usr/local/bin/pip3.6

注意:将<用户>替换为当前运行用户

相关版本(最新)如下:

3.6版本:

/home/demon/.local/lib/python3.6/site-packages/pip (python 3.6)

2.7版本:

/home/demon/.local/lib/python2.7/site-packages/pip (python 2.7)

使用python3 -m pip install——user pip==9.0.1(或有效的版本)恢复