Kenil Vasani 646 Questions 567 Answers 77 Best Answers 26 Points View Profile 2 Kenil Vasani Asked: December 10, 20202020-12-10T22:51:09+00:00 2020-12-10T22:51:09+00:00In: Python File “/usr/bin/pip”, line 9, in from pip import main ImportError: cannot import name main 2 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. I’m doing this on ec2 Linux AWS. flaskpippython Share Facebook 1 Answer Voted Kenil Vasani 646 Questions 567 Answers 77 Best Answers 26 Points View Profile Best Answer Kenil Vasani 2020-12-10T22:47:00+00:00Added an answer on December 10, 2020 at 10:47 pm TL;DR 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). 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. 9 Share Share Share on Facebook Share on Twitter Share on LinkedIn Share on WhatsApp You must login to add an answer. Username or email* Password* Remember Me! Forgot Password?