Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"ImportError: cannot import name main" after upgrading to pip 10.0.0 for Python version 2.7.12 - Only one version of Python is installed

I got a message in my terminal while install a software : You are using pip version 8.1.1, however version 10.0.0 is available. You should consider upgrading via the 'pip install --upgrade pip' command.

So I upgraded pip version: $ pip install --upgrade pip

Now I am getting this error:

~$ pip install ipython
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main
like image 537
Deba Avatar asked Apr 17 '18 14:04

Deba


3 Answers

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


Ubuntu 16.04 with Python 2.7.12

Introduction:
Ironically, despite being suggested by pip itself to do such an upgrade via the pip install --upgrade pip command explicitely in the terminal (ugh!), upgrading it is not recommended in prepackaged GNU/Linux distributions. Ubuntu generally expects using APT package manager for any system-wide Python module updates/installs (including of pip itself), for example:
$ sudo apt-get update (resync Ubuntu package index files from sources)
$ apt-cache search <python-package-name> (full text-search on all available packages)
$ apt-cache show <python-package-name> (displays the package description)
$ sudo apt-get install python-numpy python-scipy python-matplotlib (easily installs heavy modules for data science, resolving all system dependencies automatically)
$ sudo apt-get install ipython (installs the IPython-notebook you were looking for)
$ sudo apt-get install python-pip (installs/upgrades pip to the latest version available in Ubuntu repository – usually slightly behind pypi.org, but it doesn't matter)
If you ever have to use pip install command on Ubuntu/Debian instead of apt-get install, please make sure it runs isolated and does not change the default system-wide Python packages (never use sudo with pip) – more on this below.

ImportError: cannot import name main
The error is 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 the 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 pip2 command (or pip3 for Python 3) instead of pip to invoke the older system-installed pip script /usr/bin/pip2 , whereas any pip launcher script located in $HOME/.local/bin dir (pip, pip2, pip2.7) will start 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 for a specific Python version. The --user switch is only required when pip is used 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> # e.g. python-pandas
    $ apt show <python-package> # e.g. python-flask
    $ sudo apt install <python-package>
    Packages prefixed with python- are for Python 2; with python3- are for Python 3.
    Installation via apt-get may be what you need, 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 any required system libraries 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 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 use pip install command on Ubuntu instead of apt-get install, please ensure that it runs in an isolated virtual development environment, such as with virtualenv (sudo apt-get install python-virtualenv), or 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) 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 your system-wide Python packages, as these are managed by apt-get on Ubuntu.

like image 179
Alex C. Avatar answered Nov 15 '22 02:11

Alex C.


I implemmented @catalinpopescu response from ImportError: cannot import name main when running pip --version command in windows7 32 bit

Find pip's path:

$ which pip

Modify file (choose your favorite editor):

$ sudo nano `which pip`

Then modify lines @catalinpopescu said: Comment/replace lines:

from pip import main
sys.exit(main())

to:

from pip import __main__
sys.exit(__main__._main())

Immediately I upgrade to Pip Version 10.0.1, which appears it hasn't have this error.

like image 27
L. San Avatar answered Nov 15 '22 02:11

L. San


try to upgrade the system pip

sudo pip install --upgrade pip

pip install --upgrade pip

this may be useful

like image 20
user9664112 Avatar answered Nov 15 '22 02:11

user9664112