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

其他回答

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

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

我也有同样的问题,但我已经解决了。这是我的解决方案。 首先,当我运行pip install something时,错误如下所示:

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

因此,我cd到文件/usr/bin/和cat 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())

然后我认为它不在安装路径中。所以我cd到python3-pip,像这样:

cd /.local/lib/python3.5/site-packages/pip

附注:你必须在电脑里按正确的方向输入cd 然后我cat文件来查看差异(你可以使用其他操作来查看代码):

cat __main__.py

我看到了这个:

from __future__ import absolute_import
import os
import sys
# If we are running from a wheel, add the wheel to sys.path
# This allows the usage python pip-*.whl/pip install pip-*.whl
if __package__ == '':
    # __file__ is pip-*.whl/pip/__main__.py
    # first dirname call strips of '/__main__.py', second strips off '/pip'
    # Resulting path is the name of the wheel itself
    # Add that to sys.path so we can import pip
    path = os.path.dirname(os.path.dirname(__file__))
    sys.path.insert(0, path)

from pip._internal import main as _main  # isort:skip # noqa

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

所以,你能看出区别吗?我可以弄清楚,我必须使该文件与/usr/bin/pip3中的文件相同

因此,我复制了/.local/lib/python3.5/site-packages/pip中的代码来替换/usr/bin/pip3中的代码 问题就消失了!

注:pip3和pip在这个问题上没有区别。 如果我的解决方案解决了你的问题,我会很高兴!

检查pip是否已经缓存在另一个路径上,为此,调用$ which pip并检查该路径与错误提示中的路径不同,如果是这样的话,运行:

$ hash -r

当缓存被清除时,pip将再次工作。 参考:http://cheng.logdown.com/posts/2015/06/14/-usr-bin-pip-no-such-file-or-directory

我在一个系统上运行,我有sudoapt但没有sudopip。(并且没有su访问权限。)我听从了皮普的建议,陷入了同样的境地:

您使用的是pip 8.1.1版本,但是有18.0可用。您应该考虑通过'pip install——upgrade pip'命令进行升级。

其他修复对我都不起作用,因为我没有足够的管理权限。然而,在阅读这篇文章后,有几件事让我印象深刻:

我不应该这么做的。当然,皮普让我这么做的。它撒了谎。 使用——user通过关注仅用户目录解决了许多问题。

因此,我发现这个命令行可以将我恢复到原来的位置。如果您使用的是与8.1.1不同的版本,那么您显然希望更改该行的这一部分。

python -m pip install --force-reinstall pip==8.1.1 --user

这是唯一对我有效的方法,但它非常有效!

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

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