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

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

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


当前回答

在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将变得腐败,并显示'无法找到主'错误。

其他回答

在ubuntu 18.04.1 Bionic Beaver中,您需要登出并重新登录(不需要重新启动)以获得适当的环境。

$ sudo apt install python-pip

$ pip --version
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)

$ pip install --upgrade pip

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

$ exit
<login>

$ pip --version
pip 18.1 from /home/test/.local/lib/python2.7/site-packages/pip (python 2.7)

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

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

适用于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

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

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

请运行以下命令进行修复。执行python3 -m pip install——upgrade pip命令后,请执行以下命令。

hash -r pip

来源:https://github.com/pypa/pip/issues/5221