每当我试图使用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 -m pip仍然在工作,所以我用核选项sudo python -m pip install -upgrade pip来修复它。它为我做到了。

其他回答

您一定是无意中升级了您的系统pip(可能是通过sudo pip install pip—upgrade之类的程序)

皮普10。X调整它内部的位置。您看到的pip3命令是由您的包维护者提供的(想必debian基于这里?),而不是由pip管理的文件。

您可以在pip的问题跟踪器上阅读更多有关此的信息

您可能不希望升级您的系统pip,而是使用virtualenv。

要恢复pip3二进制文件,您需要sudo python3 -m pip uninstall pip && sudo apt install python3-pip—重新安装。

如果您想继续在“不受支持的领域”(在系统包管理器之外升级系统包),您可能可以使用python3 -m pip…而不是pip3。

我也有同样的问题,但我已经解决了。这是我的解决方案。 首先,当我运行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在这个问题上没有区别。 如果我的解决方案解决了你的问题,我会很高兴!

上面的命令对我没用,但它们非常有用:

sudo apt purge python3-pip
sudo rm -rf '/usr/lib/python3/dist-packages/pip'  
sudo apt install python3-pip
cd
cd .local/lib/python3/site-packages
sudo rm -rf pip*  
cd
cd .local/lib/python3.5/site-packages
sudo rm -rf pip*  
sudo pip3 install jupyter

当我想将系统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。的回答

适用于Ubuntu系列、Debian、Linux Mint用户

感谢Anthony上面的解释,您可以保留您原来的系统pip(在/usr/bin/和dist-packages/中),并删除手动安装的pip(在~/.local/中)来解决冲突:

$ python3 -m PIP卸载PIP

Ubuntu/Debian pip v8.1.1 (16.04) from python3-pip debian package (see$ pip3 -V) shows the same search results as the latest pip v10.0.1, and installs latest modules from PyPI just fine. It has a working pip command (already in the $PATH), plus the nice --user option patched-in by default since 2016. Looking at pip release notes, the newer versions are mostly about use-case specific bug fixes and certain new features, so not everyone has to rush upgrading pip just yet. And the new pip 10 can be deployed to Python virtualenvs, anyway.

但不管有没有pip,你的操作系统都允许使用APT快速安装常用的Python模块(包括numpy),而不需要pip,例如: python3-scipy(包含系统依赖项) $ sudo apt安装python3-pip (debian补丁的pip,稍微旧一点,但没关系)

快速apt语法提示(详情请参阅man apt): $ sudo apt update(从最新的源代码中重新同步Ubuntu包索引文件) $ apt search <python-package-name>(所有可用包的完整文本搜索) $ apt show <python-package-name>(显示详细的包描述) $ sudo apt install <python-package-name>

Package names prefixed with python- are for Python 2; and prefixed with python3- are for Python 3 (e.g. python3-pandas). There are thousands, and they undergo integration testing within Debian and Ubuntu. Unless you seek to install at per-user level (pip install --user option) or within virtualenv/venv, apt could be what you needed. These system packages are accessible from virtual envs too, as virtualenv will gracefully fall back to using system libs on import if your envs don't have given copies of modules. Your custom-installed (with pip --user) per-user modules in ~/.local/lib will override them too.

注意,由于这是一个系统范围的安装,您很少需要删除它们(需要注意OS依赖关系)。这对于具有许多系统依赖关系的包(例如scipy或matplotlib)来说非常方便,因为APT将跟踪并提供所有必需的系统库和C扩展,而使用pip则没有这样的保证。

事实上,对于系统级的Python包(相对于每个用户、主目录级别或更低级别),Ubuntu希望使用APT包管理器(而不是sudo pip)来避免破坏操作系统:sudo pip3的目标是相同的/usr/lib/python3/dist-packages目录,APT存储对操作系统敏感的模块。最近的Debian/Ubuntu版本严重依赖于Python 3,所以它的预安装模块是由apt管理的,不应该被改变。

So if you use pip3 install command, please ensure that it runs in an isolated virtual dev environment, such as with virtualenv (sudo apt install python3-virtualenv), or with Python3 built-in (-m venv), or at a per-user level (--user pip option, default in Ubuntu-provided pip since 2016), but not system-wide (never sudo pip3!), because pip interferes with the operation of the APT package manager and may affect Ubuntu OS components when a system-used python module is unexpectedly changed. Good luck!


附注:以上所有都是“理想的”解决方案(Debian/Ubuntu方式)。

如果您仍然想只使用新的pip3 v10,有3个快速解决方案:

只需打开一个新的bash会话(一个新的终端选项卡,或者键入bash)——pip3 v10就可用了(参见pip3 - v)。Debian的pip3 v8仍在安装,但已损坏;或 使用$ hash -d pip3 && pip3 -V命令刷新$PATH中的pip3路径名。Debian的pip3 v8仍在安装,但已损坏;或 使用命令$ sudo apt remove python3-pip && hash -d pip3来完全卸载debian的pip3 v8,以支持新的pip3 v10。

注意:你总是需要在任何非debian提供的pip中添加——user标志,除非你在virtualenv中!(它将python包部署到~/。Local /,自2016年起debian/ubuntu提供的python3-pip和python-pip中的默认值)。Ubuntu/Debian并不真正支持在virtualenv之外的系统范围内使用pip 10。永远不要!

进一步的细节: https://github.com/pypa/pip/issues/5221#issuecomment-382069604 https://github.com/pypa/pip/issues/5240#issuecomment-381673100