文件“/usr/bin/pip",第 9 行,在 <module> 中.from pip import main ImportError: 无法导入名称 main

文件“/usr/bin/pip

问题描述:

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

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.

我在 ec2 Linux AWS 上执行此操作.

I'm doing this on ec2 Linux AWS.

TL;DR

  1. 理想"解决方案(Ubuntu/Debian 方式):
    $ python -m pip uninstall pip 卸载新的 pip 10 并保留 Ubuntu/Debian 提供的补丁 pip 8.对于 系统范围 模块的安装,请使用 apt 尽可能(除非您在 virtualenv 中),更多信息请参见下文.在旧的 Ubuntu/Debian 版本中,在 virtualenvs 之外使用 pip 时总是添加 --user 标志(安装到 ~/.local/,默认在 python-pip 和 python3-自 2016 年以来的点数).

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

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

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

  • 只需重新打开一个新的 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.
  • 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 -V to 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.

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

Note: You will always need to add --user flag 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!

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

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

So, here we have Python 2.7.12 in Ubuntu 16.04 ec2 machine, and get ImportError: cannot import name main when trying to use pip. It's caused by the pip install --upgrade pip command: 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 apt subsystem. 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.

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

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

  • $ hash -d pip 命令:当调用hash 时,pip 的完整路径名通过搜索$PATH 中的目录确定并记住.任何先前记住的路径名都将被丢弃.-d 选项使外壳程序忘记"给定包名的记住位置;或

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

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

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

您可以只使用版本化的 pip2 命令(或 pip3 for Python 3)而不是 pip 来调用旧系统-已安装启动器 /usr/bin/pip2 ,而位于 $HOME/.local/bin 目录(pip、pip2、pip2.7)中的任何 pip 脚本将调用您的新的用户安装的 pip 10 版本;

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

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

You can also use the versioned Python commands in combination with the -m switch 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 --user switch is only required outside of virtualenv.

或者,卸载两个 pip 中的一个用户安装或系统安装 - 解决冲突:
$ python -m pip uninstall pip – 从 python-pip debian 包(python3-pip for Python 3)中删除手动安装的 pip 以支持以前安装的 Ubuntu 版本;它稍旧,但它可以很好地从 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 索引搜索结果并且可以拉出相同的模块版本;

Or, uninstall one of 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 pip command, just add your $HOME/.local/bin directory to your $PATH environment variable to use pip command (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;

最后,您可以完全忽略这两个 pip 以支持 APT,并使用以下命令从 Ubuntu 存储库在系统范围内安装 Python 包:
$ apt search <python-package> # 或 apt-cache search 在较旧的 Ubuntu 中
$ apt show # 例如蟒蛇烧瓶
$ sudo apt install <python-package> # 或 sudo apt-get install
python- 为前缀的包适用于 Python 2;python3- 适用于 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确实是更合适的工具.如果您必须在 Ubuntu 上使用 pip install 命令而不是 apt-get install,请确保它在隔离的虚拟开发环境中运行,例如使用 virtualenv (sudo apt-get install python-virtualenv),或使用内置的 venv 模块(仅在 python3 中可用),或在每个用户级别(pip install --user 命令选项)),但不是系统范围的(永远不要sudo pip!).

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 search in 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 preferred whenever 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 install command 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 venv module (available in python3 only), or at a per-user level (pip install --user command option), but not system-wide (never sudo pip!).

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

Note: Using sudo pip command (with root access) on Ubuntu/Debian should be avoided, because it interferes with the operation of the system package manager (apt) and may affect Ubuntu OS components when 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.