Ubuntu – how to upgrade pip to version >> 9.0.1 ? (pip install –upgrade pip gives ‘cannot import name main’ error)

aptpippythonupgrade

I have pip 8.1 installed and I wanted to upgrade to > 9.0.1 (to install matplotlib for python 2.7). I saw How to upgrade pip to latest? and did

sudo -H pip3 install --upgrade pip
sudo -H pip2 install --upgrade pip

but after than, if I type pip --version, I get

ckim@chan-ubuntu:~$ pip --version
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main

This had I experienced before and I knew the solution(Hmm. sounds like German word order..:)) , so I did

sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
sudo python -m pip uninstall pip && sudo apt install python-pip --reinstall

and then I type pip --version and get

pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

This is back to 8.1.1. How can I upgrade to newest version?

ADD : I'm using 16.04.5 LTS. This in system environment.(ok in python 2.7 virtual environment)

Best Answer

The issue here is that the pip installed by apt conflicts with the standard pip package

The easiest solution is to only install the standard pip:

  1. Uninstall pip by running the following until nothing happens:

    sudo python -m pip uninstall pip
    
  2. Uninstall apt pip

    sudo apt-get remove python-pip
    
  3. Download the pip boot-strap from https://bootstrap.pypa.io/get-pip.py

  4. Install standard pip
    python get-pip.py
    

NB: replace python with python3 if you're using Python 3, but the OP wants matplotlib for Python 2


The main difference between the apt pip and the standard pip is that --user is the default on apt, meaning packages will be installed to the user's packages directory: eg "~/.local/lib/python2.7/site-packages". Standard pip will default to installing to the system packages directory. Edit: pip v20 will install to the user directory if it doesn't have permissions to edit the system directory

Also, the apt pip tends to be many versions behind, lacking many features and bug-fixes that are in the current standard pip (such as installation from private repositories, PEP-517 support, command-line autocompletion)

Related Question