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

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

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


当前回答

这对我很管用!

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和最新的pip没有问题

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

在Debian上,你需要先更新apt ....

sudo apt-get update -qq
sudo apt-get install python-pip -qq
sudo pip install pip --upgrade --quiet
sudo pip2 install virtualenv --quiet

如果你跳过'sudo apt-get update -qq',你的pip将变得腐败,并显示'无法找到主'错误。

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

正如@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。我刚刚删除了旧文件夹,现在一切都很好!

使用python -m pip install代替pip install

例子:

python -m pip install --user somepackage
python3 -m pip install --user somepackage

pip(回应)。pip3)可执行文件由您的发行版(Ubuntu 16.04上的python-pip包)提供,位于/usr/bin/pip.

因此,当您升级pip时,它不能与pip包本身保持最新,并且可能会损坏。

如果你直接使用python -m pip,例如:

python -m pip install --user somepackage
python3 -m pip install --user somepackage

它会遍历你的Python路径,找到pip的最新版本并执行该文件。

它依赖于这样一个事实,即文件可以通过导入执行,但这是一种非常标准的接口类型,因此不太可能比更具黑客性的Debian脚本出错。

然后我建议添加以下别名到你的.bashrc:

pip() ( python -m pip "$@" )
pip3() ( python3 -m pip "$@" )

Ubuntu 18.04 /usr/bin/pip3文件:

from pip import main

大概main在某个时候被从PIP中移除了,这就是破坏的原因。

将所有内部api移到pip上。_internal”,它进入了PIP 18.0。

在Ubuntu 16.04中测试,从pip3 9.0.1更新到18.0。

pyenv

然而,最终,对于严肃的Python开发,我只建议你用pyenv + virtualenv安装你自己的本地Python,这也可以解决Ubuntu的这个bug: https://askubuntu.com/questions/682869/how-do-i-install-a-different-python-version-using-apt-get/1195153#1195153