[HOWTO] Fix broken pip/pip3 in Ubuntu – f”ERROR: {exc}”

So the other day I was upgrading some old test instances and setting up some roles using Ansible and the role geerlingguy.pip, and ran into an issue. The issue was that pip simply wouldn’t work, throwing the below error message:

# pip3 install docker
 Traceback (most recent call last):
   File "/usr/local/bin/pip3", line 7, in 
     from pip._internal.cli.main import main
   File "/usr/local/lib/python3.5/dist-packages/pip/_internal/cli/main.py", line 60
     sys.stderr.write(f"ERROR: {exc}")

The reason for this is that the installed version of pip, version 21, is incompatible with Python versions lower than 3.6. Luckily, fixing this was easy. On all the instances I had deployed getting this update were all running Python 3.5, so I used the below to fix the problem.

update-alternatives --install /usr/bin/python python /usr/bin/python3.5 1
curl -O https://bootstrap.pypa.io/pip/3.5/get-pip.py
python get-pip.py
python -m pip install --upgrade "pip < 21.0"

This ensure that python is linked to the python3 executable, downloading a bootstrapper script for Python 3.5, executes the bootstrapper that removes pip version 21 and then installs pip again with the latest version below version 21.

You should now be able to use pip as normal.

[HOWTO] Fix broken pip/pip3 in Ubuntu – f”ERROR: {exc}”
Tagged on:                 

Leave a Reply