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

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

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


当前回答

是有问题的包,当它生成de文件/usr/bin/pip, 您必须更改导入:

from pip import main

to

from pip._internal import main

这解决了问题,我不确定为什么它产生,但它在以下问题中说了一些事情:

pip10升级后的pyenv "ImportError:不能导入名称'main'"

其他回答

在执行任何pip命令之前,请使用以下命令

hash -d pip

会有用的

当我想将系统pip pip3从9.0.1升级到19.2.3时,我也遇到了这个问题。

执行pip3 install——upgrade pip后,pip版本变为19.2.3。但是main()在pip中被移动了。_internal在最新版本中,这会导致pip3失效。

因此,在文件/usr/bin/pip3中,将第9行:from pip import main替换为from pip。_internal导入main。这个问题将被修复,对python2-pip的作用相同。(在Ubuntu 18.04发行版上测试)

根据@Vincent H。的回答

正如@cryptoboy所说-检查你安装的pip/python版本

 demon@UbuntuHP:~$ pip -V
 demon@UbuntuHP:~$ pip2 -V
 demon@UbuntuHP:~$ pip3 -V

然后在.local/lib/文件夹中检查不需要的库。

当我迁移到更新的Kubuntu时,我备份了设置,在我的主目录中有.local/lib/python2.7/文件夹。安装python 3.6。我刚刚删除了旧文件夹,现在一切都很好!

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

这个错误可能是权限错误。因此,测试执行带有-H标志的命令:

sudo -H pip3 install numpy