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

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

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


当前回答

对于Python 2.7版本的@Anthony解决方案,通过将python3更改为Python,如下所示:

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

其他回答

我们可以通过修改pip文件来清除错误。

检查文件的位置:

$ which pip

路径-> /usr/bin/匹普

转到该位置(/usr/bin/pip)并打开终端

输入:$ sudo nano pip

你可以看到:

import sys
from pip import main
if __name__ == '__main__':
     sys.exit(main())

改变:

import sys
from pip import __main__
if __name__ == '__main__':
     sys.exit(__main__._main())

然后按CTRL + o写入更改并退出

希望这能奏效!!

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

我使用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]

我使用下面的代码来加载一个可能需要安装的模块,从而避免了这个错误(我也得到了这个错误)-使用最新的Python和最新的pip没有问题

try
  from colorama import Fore, Back, Style
except:
  !pip install colorama
  from colorama import Fore, Back, Style

不管怎样,我遇到了pip的问题(不是pip2或pip3):

$ pip -V
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main

$ pip2 -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

$ pip3 -V
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

不知怎么的(我不记得是怎么的了),我在~/中安装了python的东西。本地目录。在我从那里删除pip目录之后,pip又开始工作了。

$ rm -rf /home/precor/.local/lib/python2.7/site-packages/pip
$ pip -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)