升级pip后出错:无法导入名称“ main”

问题:升级pip后出错:无法导入名称“ main”

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

之前它运行良好,我不确定为什么会引发此错误。我已经搜索了此错误,但找不到任何可解决的错误。

如果您需要更多详细信息,请告诉我,我将更新我的问题。

Whenever I am trying to install any package using pip, I am getting this import error:

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())

It was working fine earlier, I am not sure why it is throwing this error. I have searched about this error, but can’t find anything to fix it.

Please let me know if you need any further detail, I will update my question.


回答 0

您必须不经意间升级了系统pip(可能通过sudo pip install pip --upgrade

pip 10.x调整其内部位置。pip3您看到的命令是您的软件包维护者提供的(这里大概是基于debian的?),而不是pip管理的文件。

您可以在pip的问题跟踪器上了解有关此内容的更多信息

你可能会想升级系统PIP和改为使用的virtualenv。

要恢复pip3二进制文件,您需要sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall

如果您想继续在“不受支持的地区”(在系统软件包管理器之外升级系统软件包),则可以选择使用python3 -m pip ...代替pip3

You must have inadvertently upgraded your system pip (probably through something like sudo pip install pip --upgrade)

pip 10.x adjusts where its internals are situated. The pip3 command you’re seeing is one provided by your package maintainer (presumably debian based here?) and is not a file managed by pip.

You can read more about this on pip’s issue tracker

You’ll probably want to not upgrade your system pip and instead use a virtualenv.

To recover the pip3 binary you’ll need to sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall.

If you want to continue in “unsupported territory” (upgrading a system package outside of the system package manager), you can probably get away with python3 -m pip ... instead of pip3.


回答 1

我们可以通过修改pip文件来清除错误。

检查文件的位置:

$ which pip

路径-> / usr / bin / pip

转到该位置(/ usr / bin / pip)并打开终端

输入: $ sudo nano pip

您可以看到:

import sys
from pip import main
if __name__ == '__main__':
     sys.exit(main())

改成:

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

然后按Ctrl + o写入更改并退出

希望能做到!

We can clear the error by modifying the pip file.

Check the location of the file:

$ which pip

path -> /usr/bin/pip

Go to that location(/usr/bin/pip) and open terminal

Enter: $ sudo nano pip

You can see:

import sys
from pip import main
if __name__ == '__main__':
     sys.exit(main())

Change to:

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

then ctrl + o write the changes and exit

Hope this will do!!


回答 2

对于Ubuntu系列,Debian和Linux Mint用户

多亏了Anthony的上述说明,您可以保留原始系统pip(位于/ usr / bin /和dist-packages /中)并删除手动安装的pip(位于〜/ .local /中)以解决冲突:

$ python3 -m pip uninstall pip

来自python3-pipdebian软件包的Ubuntu / Debian pip v8.1.1(16.04)(请参阅参考资料$ pip3 -V)显示的搜索结果与最新的pip v10.0.1相同,并且可以从PyPI安装最新的模块。它具有有效的pip命令(已在$ PATH中),以及--user自2016年以来默认修补的nice 选项。查看pip发行说明,较新的版本主要是针对用例特定的错误修复和某些新功能,因此不是每个人都必须赶紧升级点子。无论如何,新的pip 10可以部署到Python virtualenvs。

但是,无论使用哪种pip,您的操作系统都可以通过APT快速安装常见的Python模块(包括numpy),而无需使用pip,例如:(
$ sudo apt install python3-numpy python3-scipy具有系统依赖性)
$ sudo apt install 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>

前缀python-为的软件包名称适用于Python 2;并带有前缀python3-用于Python 3(例如python3-pandas)。有成千上万个,它们在Debian和Ubuntu中进行集成测试。除非您寻求在每个用户级别(pip install --user选件)或在virtualenv / venv中安装,否则可能会需要apt。这些系统程序包也可以从虚拟环境访问,因为如果您的环境没有给定模块的副本,则virtualenv将在使用时优雅地转而使用系统库。您自定义安装的(带有pip --user)每用户模块~/.local/lib也会覆盖它们。

请注意,由于这是系统范围的安装,因此您几乎不需要删除它们(需要注意OS依赖性)。这对于具有许多系统依赖性的软件包(例如,使用scipy或matplotlib)很方便,因为APT会跟踪并提供所有必需的系统库和C扩展名,而使用pip则无法保证

实际上,对于系统范围的Python软件包(与按用户,主目录级别或更低级别的软件包相反),Ubuntu 希望使用APT软件包管理器(而不是sudo pip)来避免破坏OS:sudo pip3/usr/lib/python3/dist-packagesAPT存储OS的同一目录为目标敏感模块。Debian / Ubuntu的最新发行版在很大程度上依赖于Python 3,因此其预装模块由apt且不应该更改。

因此,如果您使用pip3 install命令,请确保它在隔离的虚拟开发环境中运行,例如virtualenvsudo apt install python3-virtualenv)或Python3内置(-m venv)或在每个用户级别运行(--userpip选项,在Ubuntu提供的默认选项中自2016年以来一直是pip),但不是系统范围的(从来没有sudo pip3!),因为pip会干扰 APT软件包管理器的操作,并且在意外更改系统使用的python模块时可能会影响Ubuntu OS 组件。祝好运!


P. S. 以上都是针对“理想”解决方案的(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。

注意:--user除非您处于virtualenv中,否则您将始终需要将标记添加到任何非debian提供的pip中!(~/.local/自2016年起,它将python软件包部署到,默认为debian / ubuntu提供的python3-pip和python-pip)。Ubuntu / Debian并不真正支持您在virtualenv之外使用系统范围内的pip 10。永不sudo pip3

更多详细信息:
https : //github.com/pypa/pip/issues/5221#issuecomment-382069604
https://github.com/pypa/pip/issues/5240#issuecomment-381673100

For Ubuntu family, Debian, Linux Mint users

Thanks to Anthony’s explanation above, you can retain your original system pip (in /usr/bin/ and dist-packages/) and remove the manually-installed pip (in ~/.local/) to resolve the conflict:

$ python3 -m pip uninstall 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.

But regardless of pips, your OS allows to quickly install common Python modules (including numpy) with APT, without the need for pip, for example:
$ sudo apt install python3-numpy python3-scipy (with system dependencies)
$ sudo apt install python3-pip (Debian-patched pip, slightly older but it doesn’t matter)

Quick apt syntax reminder (please see man apt for details):
$ sudo apt update (to resync Ubuntu package index files from up-to-date sources)
$ apt search <python-package-name> (full text-search on all available packages)
$ apt show <python-package-name> (displays the detailed package description)
$ 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.

Note, since this is a system-wide installation, you’d rarely need to remove them (need to be mindful about OS dependencies). This is convenient for packages with many system dependencies (such as with scipy or matplotlib), as APT will keep track and provide all required system libs and C extensions, while with pip you have no such guarantees.

In fact, for system-wide Python packages (in contrast to per-user, home dir level, or lower), Ubuntu expects using the APT package manager (rather than sudo pip) to avoid breaking OS: sudo pip3 targets the very same /usr/lib/python3/dist-packages directory where APT stores OS-sensitive modules. Recent Debian/Ubuntu releases depend heavily on Python 3, so its pre-installed modules are managed by apt and shouldn’t be changed.

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!


P. S. All the above is for the ‘ideal’ solution (Debian/Ubuntu way).

If you still want to use the new pip3 v10 exclusively, there are 3 quick workarounds:

  • simply open a new bash session (a new terminal tab, or type bash) – and pip3 v10 becomes available (see pip3 -V). debian’s pip3 v8 remains installed but is broken; or
  • the command $ hash -d pip3 && pip3 -V to refresh pip3 pathname in the $PATH. debian’s pip3 v8 remains installed but is broken; or
  • the command $ sudo apt remove python3-pip && hash -d pip3 to uninstall debian’s pip3 v8 completely, in favor of your new pip3 v10.

Note: You will always need to add --user flag to any non-debian-provided pip, unless you are in a virtualenv! (it deploys python packages to ~/.local/, default in debian/ubuntu-provided python3-pip and python-pip since 2016). Your use of pip 10 system-wide, outside of virtualenv, is not really supported by Ubuntu/Debian. Never sudo pip3!

Further details:
https://github.com/pypa/pip/issues/5221#issuecomment-382069604
https://github.com/pypa/pip/issues/5240#issuecomment-381673100


回答 3

仅一步解决。

我也曾经遇到过这个问题,但是可以简单地通过1条命令解决它,而不会打扰和浪费时间,而且我已经在多个系统上进行了尝试,这是解决此问题的最干净的方法。那就是:

对于python3:- sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall

这样,您可以简单地使用安装软件包pip3。检查使用pip3 --version

对于旧版本,请使用:sudo python -m pip uninstall pip && sudo apt install python-pip --reinstall

这样,您现在可以使用来简单地安装软件包pip。检查使用pip --version

resolved in one step only.

I too faced this issue, But this can be resolved simply by 1 command without bothering around and wasting time and i have tried it on multiple systems it’s the cleanest solution for this issue. And that’s:

For python3:- sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall.

By this , you can simply install packages using pip3. to check use pip3 --version.

For older versions, use : sudo python -m pip uninstall pip && sudo apt install python-pip --reinstall.

By this, now you can simply install packages using pip. to check use pip --version.


回答 4

使用python -m pip install代替pip install

例:

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

pip(相应地pip3)执行是由你的发行版提供的(python-pip在Ubuntu 16.04封装)和位于/usr/bin/pip

因此,pip在升级pip时,它不会与软件包本身保持最新状态,并且可能会损坏。

如果您只是python -m pip直接使用,例如:

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

它会通过您的Python路径,找到最新版本的pip并执行该文件。

它依赖于这样的事实,即文件可以通过来执行import,但这是一种非常标准的接口类型,因此比骇客的Debian脚本更不可能被破坏。

然后,我建议将以下别名添加到您的.bashrc

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

Ubuntu 18.04 /usr/bin/pip3文件执行以下操作:

from pip import main

大概mainpip在某个时候被破坏了。

中断的pip提交似乎是:95bcf8c5f6394298035a7332c441868f3b0169f4“将所有内部API移至pip._internal”已进入pip 18.0。

pip39.0.1升级到18.0 后,在Ubuntu 16.04中进行了测试。

pyenv

但是,最终,对于认真的Python开发,我只建议您使用pyenv + virtualenv安装自己的本地Python,这也可以解决以下Ubuntu错误:https ://askubuntu.com/questions/682869/how-do-i- 安装一个不同的python-version-using-apt-get / 1195153#1195153

Use python -m pip install instead of pip install

Example:

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

The pip (resp. pip3) executable is provided by your distro (python-pip package on Ubuntu 16.04) and located at /usr/bin/pip.

Therefore, it is not kept up-to date with the pip package itself as you upgrade pip, and may break.

If you just use python -m pip directly, e.g. as in:

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

it goes through your Python path, finds the latest version of pip and executes that file.

It relies on the fact that file is executable through import, but that is a very standard type of interface, and therefore less likely to break than the hackier Debian script.

Then I recommend adding the following aliases to your .bashrc:

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

The Ubuntu 18.04 /usr/bin/pip3 file does:

from pip import main

and presumably main was removed from pip at some point which is what broke things.

The breaking pip commit appears to be: 95bcf8c5f6394298035a7332c441868f3b0169f4 “Move all internal APIs to pip._internal” which went into pip 18.0.

Tested in Ubuntu 16.04 after an update from pip3 9.0.1 to 18.0.

pyenv

Ultimately however, for serious Python development I would just recommend that you install your own local Python with pyenv + virtualenv, which would also get around this Ubuntu bug: https://askubuntu.com/questions/682869/how-do-i-install-a-different-python-version-using-apt-get/1195153#1195153


回答 5

您可以通过重新安装pip解决此问题。

使用以下命令行命令之一重新安装pip:

Python2:

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

Python3:

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

You can resolve this issue by reinstalling pip.

Use one of the following command line commands to reinstall pip:

Python2:

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

Python3:

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

回答 6

检查pip是否已缓存在另一路径上,为此,请调用$ which pip并检查该路径是否与错误中提示的路径不同(如果是这种情况):

$ hash -r

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

Check if pip has been cached on another path, to do so, call $ which pip and check that the path is different from the one prompted in the error, if that’s the case run:

$ hash -r

When the cache is clear, pip will be working again. reference: http://cheng.logdown.com/posts/2015/06/14/-usr-bin-pip-no-such-file-or-directory


回答 7

我在有sudo apt但没有sudo pip的系统上运行。(并且没有su访问权限。)我按照pip的建议进入了同样的情况:

您正在使用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

那是唯一对我有用的东西,但是效果很好!

I’m running on a system where I have sudo apt but no sudo pip. (And no su access.) I got myself into this same situation by following the advice from pip:

You are using pip version 8.1.1, however 18.0 is available. You should consider upgrading via the ‘pip install –upgrade pip’ command.

None of the other fixes worked for me, because I don’t have enough admin privileges. However, a few things stuck with me from reading up on this:

  • I shouldn’t have done this. Sure, pip told me to. It lied.
  • Using –user solves a lot of issues by focusing on the user-only directory.

So, I found this command line to work to revert me back to where I was. If you were using a different version than 8.1.1, you will obviously want to change that part of the line.

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

That’s the only thing that worked for me, but it worked perfectly!


回答 8

使用python3 -m pip install --user pip==9.0.1(或可用的版本)进行恢复

Recover with python3 -m pip install --user pip==9.0.1 (or the version that worked)


回答 9

使用新的LXC(strech)在Pixelbook上发生了同样的事情。此解决方案与公认的解决方案非常相似,只是有一个细微的区别,即我固定了pip3。

sudo python3 -m pip install --upgrade pip

颠覆了版本,现在可以正常工作了。

我在这里找到了它。Python.org:确保pip是最新的

Same thing happened to me on Pixelbook using the new LXC (strech). This solution is very similar to the accepted one, with one subtle difference, whiched fixed pip3 for me.

sudo python3 -m pip install --upgrade pip

That bumped the version, and now it works as expected.

I found it here … Python.org: Ensure pip is up-to-date


回答 10

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

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

I met the same problem on my Ubuntu 16.04 system. I managed to fix it by re-installing pip with the following command:

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


回答 11

上面的命令对我不起作用,但这些命令非常有用:

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

The commands above didn’t work for me but those were very helpful:

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

回答 12

在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)

In ubuntu 18.04.1 Bionic Beaver, you need to log out and log back in (restart not necessary) to get the proper environment.

$ 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)

回答 13

我用 sudo apt remove 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]

I use sudo apt remove python3-pip then pip works.

 ~ 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]

回答 14

对于Python 2.7版,@ Anthony解决方案可以完美实现,方法是将python3更改为python,如下所示:

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

For Python version 2.7 @Anthony solution works perfect, by changing python3 to python as follows:

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

回答 15

对我来说使用修复错误的原因pip3是:

sudo cp -v /usr/local/bin/pip3 /usr/bin/pip3

一切正常:

 demon@UbuntuHP:~$ pip -V
 pip 10.0.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

 demon@UbuntuHP:~$ pip2 -V
 pip 10.0.1 from /home/demon/.local/lib/python2.7/site-packages/pip (python 2.7)

 demon@UbuntuHP:~$ pip3 -V
 pip 10.0.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

也许新的10.0.1版本的pip不会更新/ usr / bin中的二进制文件?(似乎没有)

编辑:在Ubuntu 18.04中会发生相同的问题。我发现的最佳解决方案是将pip二进制文件从符号链接/home/<user/.local/bin/usr/local/bin/usr/bin(取决于您的偏好),如下所示:

ln -sv /home/<user>/.local/bin/pip /usr/local/bin/pip
ln -sv /home/<user>/.local/bin/pip2 /usr/local/bin/pip2
ln -sv /home/<user>/.local/bin/pip2.7 /usr/local/bin/pip2.7
ln -sv /home/<user>/.local/bin/pip3 /usr/local/bin/pip3
ln -sv /home/<user>/.local/bin/pip3.6 /usr/local/bin/pip3.6

注意:替换 <user> 为当前运行的用户

关联的版本(最新)位于:

版本3.6:

/home/demon/.local/lib/python3.6/site-packages/pip(python 3.6)

2.7版:

/home/demon/.local/lib/python2.7/site-packages/pip(python 2.7)

What worked for me to fix the error with using pip3 was:

sudo cp -v /usr/local/bin/pip3 /usr/bin/pip3

Everything works:

 demon@UbuntuHP:~$ pip -V
 pip 10.0.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

 demon@UbuntuHP:~$ pip2 -V
 pip 10.0.1 from /home/demon/.local/lib/python2.7/site-packages/pip (python 2.7)

 demon@UbuntuHP:~$ pip3 -V
 pip 10.0.1 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)

Maybe the new 10.0.1 version of pip doesn’t update the binary in /usr/bin ? (which seems it does not)

EDIT: the same issue occurs in Ubuntu 18.04. The best solution I’ve found is to symlink the pip binaries from /home/<user/.local/bin to /usr/local/bin or /usr/bin (depending on your preference), as follows:

ln -sv /home/<user>/.local/bin/pip /usr/local/bin/pip
ln -sv /home/<user>/.local/bin/pip2 /usr/local/bin/pip2
ln -sv /home/<user>/.local/bin/pip2.7 /usr/local/bin/pip2.7
ln -sv /home/<user>/.local/bin/pip3 /usr/local/bin/pip3
ln -sv /home/<user>/.local/bin/pip3.6 /usr/local/bin/pip3.6

NOTE: replace <user> with your current running user

The associated versions (latest) are in:

Version 3.6:

/home/demon/.local/lib/python3.6/site-packages/pip (python 3.6)

Version 2.7:

/home/demon/.local/lib/python2.7/site-packages/pip (python 2.7)


回答 16

绝招

须藤-H pip install lxml

Trick and works too

sudo -H pip install lxml


回答 17

我也遇到了同样的错误,但python -m pip仍在工作,因此我使用了核选项解决了该问题sudo python -m pip install --upgrade pip。它为我做到了。

I had this same error, but python -m pip was still working, so I fixed it with the nuclear option sudo python -m pip install --upgrade pip. It did it for me.


回答 18

对于它的价值,我遇到了pip(不是pip2pip3)问题:

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

$ pip2 -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

$ pip3 -V
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

不知何故(我不记得如何),我在~/.local目录中安装了python东西。从那里删除pip目录后,pip再次开始工作。

$ rm -rf /home/precor/.local/lib/python2.7/site-packages/pip
$ pip -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

For what it’s worth, I had the problem with pip (not pip2 or pip3):

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

$ pip2 -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

$ pip3 -V
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

Somehow (I can’t remember how) I had python stuff installed in my ~/.local directory. After I removed the pip directory from there, pip started working again.

$ rm -rf /home/precor/.local/lib/python2.7/site-packages/pip
$ pip -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

回答 19

软件包有问题,当它生成文件/ usr / bin / pip时,必须更改导入:

from pip import main

from pip._internal import main

这就解决了问题,我不确定它为什么产生,但是在以下问题中说得很对:

在pyenv上进行pip 10升级后,“导入错误:无法导入名称’main’”

Is something wrong with the packages, when it generating de file /usr/bin/pip, you have to change the import:

from pip import main

to

from pip._internal import main

That solves the problem, I’m not sure why it generated, but it saids somthing in the following issue:

After pip 10 upgrade on pyenv “ImportError: cannot import name ‘main'”


回答 20

您可以尝试以下方法:

sudo ln -sf $( type -P pip ) /usr/bin/pip

You can try this:

sudo ln -sf $( type -P pip ) /usr/bin/pip

回答 21

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

运行后pip3 install --upgrade pippip版本变为19.2.3。但main()已移至pip._internal最新版本,但已pip3损坏。

因此,在文件中/usr/bin/pip3,替换line 9from pip import mainfrom pip._internal import main。该问题将得到解决,适用于python2-pip。(在Ubuntu 18.04发行版上测试)

根据@Vincent H.的回答

I also run into this problem when I wanted to upgrade system pip pip3 from 9.0.1 to 19.2.3.

After running pip3 install --upgrade pip, pip version becomes 19.2.3. But main() has been moved in pip._internal in the latest version, which leaves pip3 broken.

So in file /usr/bin/pip3, replace line 9: from pip import main with from pip._internal import main. The issue will be fixed, works the same for python2-pip. (Tested on Ubuntu 18.04 distribution)

According to @Vincent H.’s answer


回答 22

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

hash -r pip

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

Please run the following commands to do the fix. After running python3 -m pip install --upgrade pip, please run the following command.

hash -r pip

Source: https://github.com/pypa/pip/issues/5221


回答 23

您可以简单地使用以下方法修复pip和pip3路径 update-alternatives

您应该检查的第一件事是当前$PATH 运行情况,echo $PATH然后您可以找到/usr/local/binpip3和pip通常在哪里

有一个变化,您的系统正在这里寻找/bin/pip/bin/pip3 所以我会说通过添加到您的~/.bash_profile文件中使其持久存在

export PATH=$PATH:/usr/local/bin 然后用which pip和检查它是否固定which pip3

如果没有update-alternatives,最后用它来修复

update-alternatives --install /bin/pip3 pip3 /usr/local/bin/pip3 30

如果您想将pip指向pip3,则

update-alternatives --install /bin/pip pip /usr/local/bin/pip3 30

you can simply fix the pip and pip3 paths using update-alternatives

first thing you should check is your current $PATH run echo $PATH and see is you can find /usr/local/bin which is where pip3 and pip usually are

there is a change your system is looking here /bin/pip and /bin/pip3 so i will say fix the PATH by adding to your ~/.bash_profile file so it persists

export PATH=$PATH:/usr/local/bin and then check is its fixed with which pip and which pip3

if not then use update-alternatives to fix it finally

update-alternatives --install /bin/pip3 pip3 /usr/local/bin/pip3 30

and if you want to point pip to pip3 then

update-alternatives --install /bin/pip pip /usr/local/bin/pip3 30

回答 24

这对我有用!

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

希望能帮助到你!

This Worked for me !

hash -r pip # or hash -d pip

Now, uninstall the pip installed version and reinstall it using the following commands.

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

If pip is broken, use:

python -m pip install --force-reinstall pip

Hope it helps!


回答 25

从pip._internal导入main

from pip._internal import main

编辑来自的点子代码

sudo nano /usr/bin/pip3

import main from pip._internal

from pip._internal import main

Edit the pip code from

sudo nano /usr/bin/pip3

回答 26

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

As @cryptoboy said – check what pip/python version you have installed

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

and then check for no-needed libraries in your .local/lib/ folder.

I did backup of settings when I was migrating to newer Kubuntu and in had .local/lib/python2.7/ folder in my home directory. Installed python 3.6. I just removed the old folder and now everything works great!


回答 27

在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”,则您的点会损坏并显示“找不到主要”错误。

On Debian you will need to update apt first….

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

If you skip ‘sudo apt-get update -qq’ your pip will become corrupt and display the ‘cannot find main’ error.


回答 28

此错误可能是权限错误。因此,测试使用-H标志执行命令:

sudo -H pip3 install numpy

This error may be a permission one. So, test executing the command with -H flag:

sudo -H pip3 install numpy

回答 29

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

hash -d pip

会工作的

Use the following command before the execution of any pip command

hash -d pip

It will work