File “/usr/bin/pip”, line 9, in from pip import main ImportError: cannot import name main

后端 未结 6 437
轻奢々
轻奢々 2020-12-07 17:58

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.

相关标签:
6条回答
  • 2020-12-07 17:59

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

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

    python -m pip uninstall pip
    

    This has removed one local (for the user) pip version.

    I had already an older pip/pip2 system executable (apt-get installed in ancient times) that worked like a charm.

    0 讨论(0)
  • 2020-12-07 18:04

    Try this

    Check the python version you use

    # Python --version
    

    and try installing for eg if your version is 2.7

    #python2.7 <package name> 
    

    Will work fine......

    0 讨论(0)
  • 2020-12-07 18:07

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

    pip install --upgrade pip==9.0.3

    instead of

    pip install -U pip

    0 讨论(0)
  • 2020-12-07 18:17

    TL;DR

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

    2. If you still want 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 -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.

    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!

    Details:
    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.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.

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

    • 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

    • 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 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;

    • 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.

    • 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;

    • 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!).

    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.

    0 讨论(0)
  • 2020-12-07 18:17

    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.

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

    4- Download and install pip. (credits for 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.

    0 讨论(0)
  • 2020-12-07 18:20

    As suggested in pip's github issue

    The temporary fix is -

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

    #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.

    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.

    0 讨论(0)
提交回复
热议问题