本文介绍了在< module>中的文件"/usr/bin/pip"第9行,从pip import main ImportError:无法导入名称main的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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提供的补丁pip8.对于系统范围的模块安装,请尽可能使用apt(除非您位于virtualenv中) ,更多内容请见下文.在较旧的Ubuntu/Debian版本中,在virtualenvs之外使用pip时,请始终添加--user标志(安装到~/.local/中,自2016年以来默认在python-pip和python3-pip中使用).

  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)-即可获得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,以使用新的pip10.
  • 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中,否则,您将始终需要向非debian提供的pip 10添加--user标志! 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

Details:
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:无法导入主名称使用点子.这是由pip install --upgrade pip命令引起的:它从OS发行版的python-pip debian软件包(系统Python安装)中安装了最新的pip版本10和Ubuntu的默认pip版本,完全绕开了Ubuntu apt子系统.它破坏了Ubuntu的默认pip:来自python-pip(系统安装到/usr/bin/pip *)的debian修补启动器脚本试图从新安装的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时,通过在$ PATH中搜索目录并记住该点,可以确定pip的完整路径名.任何先前记住的路径名都将被丢弃. -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命令(对于Python 3为pip3)代替pip来调用较早的系统安装的启动器/usr/bin/pip2,而位于 dir(pip,pip2,pip2.7)将调用您用户安装的新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版本,那将很方便.仅在virtualenv外部需要--user开关.

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.

或者卸载两个点中的一个用户安装或系统安装 –解决冲突:
$ python -m pip uninstall pip –从python-pip debian软件包(Python3的python3-pip)中删除手动安装的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软件包:
在旧版Ubuntu中$ apt search <python-package>#或apt-cache search
$ apt show <python-package>#例如python-flask
$ sudo apt install <python-package>#或sudo apt-get install
前缀为的软件包适用于Python 2; python3-的功能适用于Python3.
您可能需要标准的apt-get安装方法.例如,在您的情况下:
python-mysqldb-MySQL的Python接口<-MySQLdb的派生== MySQL-python
python-flask-sqlalchemy-SQL Alchemy支持
python-pymysql-纯Python MySQL驱动程序
实际上,Ubuntu存储库中的python软件包首选 >在可能的情况下,尤其是在严重依赖系统或使用系统范围.当然,与PyPI(只有一个版本)相比,Ubuntu存储库中的Python软件包数量(几千个!)相对较少,因为任何OS存储库都略微落后于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 组件,当系统使用的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.

这篇关于在&lt; module&gt;中的文件"/usr/bin/pip"第9行,从pip import main ImportError:无法导入名称main的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:15