Python 文件 "/usr/bin/pip", line 9, in <module> from pip import main ImportError: cannot import name main

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49964093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:19:59  来源:igfitidea点击:

File "/usr/bin/pip", line 9, in <module> from pip import main ImportError: cannot import name main

pythonflaskpip

提问by Tanisha Bansal

I am trying to run a webpage using python flask and connecting it with the database of MySQL and while installing MySQL packages I'm receiving this error.

我正在尝试使用 python flask 运行网页并将其与 MySQL 数据库连接,在安装 MySQL 包时我收到此错误。

enter image description here

在此处输入图片说明

I'm doing this on ec2 Linux AWS.

我正在 ec2 Linux AWS 上执行此操作。

回答by Alex C.

TL;DR

TL; 博士

  1. The 'ideal' solution (Ubuntu/Debian way):
    $ python -m pip uninstall pipto uninstall the new pip 10 and retain your Ubuntu/Debian-provided patched pip 8. For a system-wideinstallation of modules use aptwherever possible (unless you are in a virtualenv), more on it below. In older Ubuntu/Debian versions, always add --userflag when using pip outside of virtualenvs (installs into ~/.local/, default in python-pip and python3-pip since 2016).

  2. If you stillwant to use the new pip 10 exclusively, there are 3 quick workarounds:

    • simply re-open a new bash session (a new terminal tab, or type bash) - and pip 10 becomes available (see pip -V). debian's pip 8 remains installed but is broken; or
    • $ hash -d pip && pip -Vto refresh pip pathname in the $PATH. debian's pip 8 remains installed but is broken; or
    • $ sudo apt remove python-pip && hash -d pip(for Python 3 it's python3-pip) -- to uninstall debian's pip 8 completely, in favor of your new pip 10.
  1. “理想”解决方案(Ubuntu/Debian 方式):
    $ python -m pip uninstall pip卸载新的 pip 10 并保留 Ubuntu/Debian 提供的修补 pip 8。对于系统范围的模块安装,请apt尽可能使用(除非您在 virtualenv 中),更多关于它下面。在较旧的 Ubuntu/Debian 版本中,--user在 virtualenvs 之外使用 pip 时总是添加标志(安装到~/.local/,自 2016 年以来默认在 python-pip 和 python3-pip 中)。

  2. 如果您想专门使用新的 pip 10,有 3 个快速解决方法:

    • 只需重新打开一个新的 bash 会话(一个新的终端选项卡,或键入bash) - pip 10 变得可用(请参阅pip -V)。debian 的 pip 8 仍然安装但已损坏;或者
    • $ hash -d pip && pip -V刷新 $PATH 中的 pip 路径名。debian 的 pip 8 仍然安装但已损坏;或者
    • $ sudo apt remove python-pip && hash -d pip(对于 Python 3,它是 python3-pip)——完全卸载 debian 的 pip 8,以支持你的新 pip 10。

Note:You will always need to add --userflag to non-debian-provided pip 10, unless you are in a virtualenv! Your use of pip 10 system-wide, outside of virtualenv, is not really supported by Ubuntu/Debian. Never sudo pip!

注意:--user除非您在 virtualenv 中,否则您始终需要向非 debian 提供的 pip 10添加标志!Ubuntu/Debian 并不真正支持您在 virtualenv 之外在系统范围内使用 pip 10。从来没有sudo pip

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

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



So, here we have Python 2.7.12in Ubuntu 16.04ec2 machine, and get ImportError: cannot import name mainwhen trying to use pip. It's caused by the pip install --upgrade pipcommand: that installs the latest pip version 10 alongside Ubuntu's default pip version from python-pip debian package from OS distribution (the system Python installation), completely bypassing Ubuntu aptsubsystem. It breaks the Ubuntu's default pip: the debian-patched launcher script from python-pip (system-installed to /usr/bin/pip*) tries to do import main() from your newly installed pip 10 library, but with a different import path, so it fails.

所以,这里我们在Ubuntu 16.04ec2 机器上有Python 2.7.12,并且在尝试使用 pip 时得到ImportError: cannot import name main。这是由以下命令引起的:从操作系统分发(系统 Python 安装)的 python-pip debian 包中安装最新的 pip 版本 10 以及 Ubuntu 的默认 pip 版本,完全绕过了 Ubuntu子系统。它破坏了 Ubuntu 的默认 pip:来自 python-pip(系统安装到 /usr/bin/pip*)的 debian-patched 启动器脚本尝试从新安装的 pip 10 库中导入 main(),但导入不同路径,所以它失败了。pip install --upgrade pipapt

This error is discussed in more detail in a developer threadof the pip issue tracker, including a few proposed solutions, such as:

pip 问题跟踪器的开发人员线程中更详细地讨论了此错误,包括一些建议的解决方案,例如:

  • The $ hash -d pipcommand: when hashis invoked, the full pathname of pip is determined by searching the directories in $PATH and remembered. Any previously-remembered pathname is discarded. The -doption causes the shell to "forget" the remembered location of the given package name; or

  • Similarly, you can simply re-open a new bash session (a new terminal tab) to refresh pip pathname in $PATH; or

  • You could just use a versioned pip2command (or pip3for Python 3) instead of pipto invoke the older system-installed launcher /usr/bin/pip2, whereas any pip script located in $HOME/.local/bindir (pip, pip2, pip2.7) will invoke your new user-installed pip 10 version;

  • You can also use the versioned Python commands in combination with the -mswitch to run the appropriate copy of pip, for example:
    $ python2 -m pip install --user SomePackage# default Python 2
    $ python2.7 -m pip install --user SomePackage# specifically Python 2.7
    That is handy if you have several versions of Python and need an extension from PyPI, such as your MySQL-python module (MySQLdb) or a Flask-MySQL, for a specific Python version. The --userswitch is only required outside of virtualenv.

  • Or, uninstall oneof the two pips – either user-installed or system-installed– to resolve the conflict:
    $ python -m pip uninstall pip– to remove your manually-installed pip in favour of the previously installed Ubuntu-shipped version from python-pip debian package (python3-pip for Python 3); it is slightly older, but it finds and installs latest modules from PyPI just fine, and has a working pip command in the $PATH by default; or
    $ sudo apt-get remove python-pip– to uninstall Ubuntu-provided pip in favour of your latest pip 10; if it is not accessible via the short pipcommand, just add your $HOME/.local/bindirectory to your $PATH environment variable to use pipcommand (see above).
    Note:Ubuntu 16.04 pip v8.1.1 and the latest pip v10.0.1 produce exactly the same PyPI index search results and can pull the same module versions;

  • Finally, you could ignore both pips altogether in favor of APT, and install Python packages system-wide from Ubuntu repo with:
    $ apt search <python-package># or apt-cache searchin older Ubuntu
    $ apt show <python-package># e.g. python-flask
    $ sudo apt install <python-package># or sudo apt-get install
    Packages prefixed with python-are for Python 2; with python3-are for Python 3.
    Standard apt-get installation method may be what you need. For example, in your case:
    python-mysqldb- Python interface to MySQL <- a fork of MySQLdb == MySQL-python
    python-flask-sqlalchemy- SQL Alchemy support
    python-pymysql- pure Python MySQL driver
    In fact, python-packages from Ubuntu repository are preferredwhenever possible, particularly in case of heavy system dependencies or when used system-wide. Of course, the amount of Python packages in Ubuntu repository (few thousand!) is relatively smaller compared to PyPI (and have only one version of them), because any OS repository is lagging slightly behind PyPI versions. But the upside of APT is that all the Ubuntu-provided packages underwent integration testing within Ubuntu, plus apt-get quickly resolves heavy dependencies like C extensions automatically. You will always get the system libraries you need as part of the apt install, but with pip you have no such guarantees.
    APT may not be an option, however, if you really need only the latest (or certain older) package version, or when it can only be found at PyPI, or when modules need to be isolated; then pip is indeed more appropriate tool. If you have to use pip installcommand on Ubuntu instead of apt-get install, please ensure it runs in an isolated virtual development environment, such as with virtualenv(sudo apt-get install python-virtualenv), or using a built-in venvmodule (available in python3 only), or at a per-user level (pip install --usercommand option), but notsystem-wide (never sudo pip!).

  • $ hash -d pip命令:当hash被调用时,PIP的完整路径是由$ PATH搜索的目录和记忆确定。任何以前记住的路径名都将被丢弃。该-d选项会导致外壳“忘记”给定包名的记住位置;或者

  • 同样,您可以简单地重新打开一个新的 bash 会话(一个新的终端选项卡)来刷新 $PATH 中的 pip 路径名;或者

  • 您可以只使用版本化pip2命令(或pip3用于 Python 3)而不是pip调用旧的系统安装的 launcher /usr/bin/pip2,而位于$HOME/.local/bindir (pip, pip2, pip2.7) 中的任何 pip 脚本都将调用您的新用户安装的 pip 10 版本;

  • 您还可以将版本控制的 Python 命令与-mswitch结合使用来运行适当的 pip 副本,例如:
    $ python2 -m pip install --user SomePackage# default Python 2
    $ python2.7 -m pip install --user SomePackage# 特别是 Python 2.7
    如果您有多个 Python 版本并且需要 PyPI 的扩展,这很方便,例如作为特定 Python 版本的 MySQL-python 模块 (MySQLdb) 或 Flask-MySQL。该--user开关仅在 virtualenv 之外才需要。

  • 或者,卸载一个两个点子-无论是用户安装或系统安装-来解决冲突:
    $ python -m pip uninstall pip-删除您手动安装的点子有利于从python-PIP Debian软件包以前安装的Ubuntu的版本运(python3- pip for Python 3); 它稍旧,但它可以很好地从 PyPI 中找到并安装最新的模块,并且默认情况下在 $PATH 中有一个有效的 pip 命令;或者
    $ sudo apt-get remove python-pip– 卸载 Ubuntu 提供的 pip 以支持最新的 pip 10;如果无法通过短pip命令访问,只需将您的$HOME/.local/bin目录添加到 $PATH 环境变量以使用pip命令(见上文)。
    笔记:Ubuntu 16.04 pip v8.1.1 和最新的 pip v10.0.1 产生完全相同的 PyPI 索引搜索结果并且可以拉出相同的模块版本;

  • 最后,你可以赞成APT完全忽略这两个点子,并从Ubuntu的回购全系统的安装Python包:
    $ apt search <python-package>#或apt-cache search中老年的Ubuntu
    $ apt show <python-package>#如蟒蛇瓶
    $ sudo apt install <python-package>#或sudo apt-get install
    前缀套餐python-是为Python 2; with python3-are for Python 3.
    标准 apt-get 安装方法可能是您所需要的。例如,在您的情况下:
    python-mysqldb- MySQL 的 Python 接口 <- MySQLdb 的一个分支 == MySQL-python
    python-flask-sqlalchemy- SQL Alchemy 支持
    python-pymysql- 纯 Python MySQL 驱动程序
    事实上,只要有可能,Ubuntu 存储库中的 Python 包都是首选,特别是在以下情况下严重的系统依赖性或在系统范围内使用时. 当然,与 PyPI 相比,Ubuntu 存储库中 Python 包的数量(几千!)相对较少(并且只有一个版本),因为任何操作系统存储库都略微落后于 PyPI 版本。但是 APT 的好处是所有 Ubuntu 提供的软件包都在 Ubuntu 中进行了集成测试,而且 apt-get 可以快速自动解决像 C 扩展这样的重度依赖。作为 apt 安装的一部分,您将始终获得所需的系统库,但使用 pip 您就没有这样的保证
    然而,如果你真的只需要最新的(或某些较旧的)包版本,或者它只能在 PyPI 上找到,或者模块需要隔离时,APT 可能不是一个选择;那么pip确实是更合适的工具。如果你必须使用pip installUbuntu 上的命令而不是apt-get install,请确保它在隔离的虚拟开发环境中运行,例如使用virtualenv( sudo apt-get install python-virtualenv),或使用内置venv模块(仅在 python3 中可用),或在每个用户级别(pip install --user命令选项),但不是系统范围的(从不sudo pip!)。

Note:Using sudo pipcommand (with root access) on Ubuntu/Debian should be avoided, because it interfereswith the operation of the system package manager (apt) and may affect Ubuntu OS componentswhen a system-used python module is unexpectedly upgraded, particularly by dependencies on another pip package. It is advised to never use Pip to change system-wide Python packages, as these are managed by apt-get on Ubuntu.

注意:sudo pip应避免在 Ubuntu/Debian 上使用命令(具有 root 访问权限),因为它会干扰系统包管理器 (apt) 的运行,并且可能会在系统使用的 python 模块意外升级时影响 Ubuntu OS组件,特别是通过依赖于另一个 pip 包。建议永远不要使用 Pip 来更改系统范围的 Python 包,因为这些包在 Ubuntu 上由 apt-get 管理。

回答by Jose Serodio

These steps worked for me.

这些步骤对我有用。

1-Uninstall the pip update from python.
2-Uninstall pip package from your Ubuntu.
3-Check that pip binary is not longer in your system.

1-从python卸载pip更新。
2-从你的 Ubuntu 中卸载 pip 包。
3-检查 pip 二进制文件是否不再存在于您的系统中。

python -m pip uninstall pip
apt remove python-pip
whereis pip

4-Download and install pip. (credits for VanDragt.com)

4-下载并安装pip。(VanDragt.com 的积分

wget https://bootstrap.pypa.io/get-pip.py -O /tmp/get-pip.py
sudo python3 /tmp/get-pip.py
pip install --user pipenv
pip3 install --user pipenv
echo "PATH=$HOME/.local/bin:$PATH" >> ~/.profile
source ~/.profile
whereis pip

Now you should be able to install any pip package you want.

现在你应该可以安装任何你想要的 pip 包了。

回答by Fabiano Tarlao

My cent, I had the same ImportError: cannot import name main.

我的钱,我也有ImportError: cannot import name main

My system is a Linux Ubuntudistro, I have executed this command:

我的系统是Linux Ubuntu发行版,我执行了以下命令:

python -m pip uninstall pip

This has removed one local (for the user) pipversion.

这已经删除了一个本地(对于用户)pip版本。

I had already an older pip/pip2system executable (apt-getinstalled in ancient times) that worked like a charm.

我已经有一个旧的pip/pip2系统可执行文件(apt-get安装在古代),效果很好。

回答by markroxor

As suggested in pip's github issue

正如 pip 的 github 问题中所建议的

The temporary fix is -

临时修复是-

Edit your /usr/bin/pipfile and comment the line importing main and edit it

编辑您的/usr/bin/pip文件并注释导入 main 的行并进行编辑

#from pip import main
from pip._internal import main as main

Worked perfectly for me. Note - this is a temporary fix. Wait for team pip to fix this.

非常适合我。注意 -这是一个临时修复。等待团队 pip 解决此问题。

OR

或者

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

to this:

对此:

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

As suggested in this SO answer.

正如this SO answer中所建议的那样。

回答by Javeed Shakeel

Try this

尝试这个

Check the python versionyou use

检查您使用的python版本

# Python --version

and try installing for eg if your version is 2.7

并尝试安装例如如果您的版本是 2.7

#python2.7 <package name> 

Will work fine......

会工作得很好......

回答by Stone

I have faced the similar issue after pip 19 upgrade. So I did the following to fix the problem.

pip 19 升级后,我遇到了类似的问题。所以我做了以下来解决这个问题。

pip install --upgrade pip==9.0.3

pip install --upgrade pip==9.0.3

instead of

代替

pip install -U pip

pip 安装 -U pip