Fixing Ubuntu Python-Pip

After you’ve upgraded your Ubuntu machine a few times, you might find that free disk space is low and some Python commands have issues. Programs like Bleachbit can help you clear old packages or cache that you don’t need, but sometimes you need a minor manual fix as documented here. As you may know, Pip is the main package management you’ll use to install packages you use, so this can be problematic.

After upgrading 16.04 to 18.04, I got an error on command line “pip”:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 574, in _build_master
    ws.require(__requires__)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 892, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 783, in resolve
    raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.VersionConflict: (pip 9.0.1 (/usr/lib/python3/dist-packages), Requirement.parse('pip==8.1.1'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/pip", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3088, in <module>
    @_call_aside
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3072, in _call_aside
    f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3101, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 576, in _build_master
    return cls._build_from_requirements(__requires__)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 589, in _build_from_requirements
    dists = ws.resolve(reqs, Environment())
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 778, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pip==8.1.1' distribution was not found and is required by the application

and pip3 (python3):

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 574, in _build_master
    ws.require(__requires__)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 892, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 783, in resolve
    raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.VersionConflict: (pip 9.0.1 (/usr/lib/python3/dist-packages), Requirement.parse('pip==8.1.1'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/pip3", line 5, in <module>
    from pkg_resources import load_entry_point
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3088, in <module>
    @_call_aside
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3072, in _call_aside
    f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3101, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 576, in _build_master
    return cls._build_from_requirements(__requires__)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 589, in _build_from_requirements
    dists = ws.resolve(reqs, Environment())
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 778, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pip==8.1.1' distribution was not found and is required by the application

Whereis command shows one command that works (and in fact you can see in properties in Synaptic that python-pip installs /usr/bin/pip3, /usr/bin/pip, /usr/bin/pip2):

$ whereis pip
pip: /usr/bin/pip /usr/local/bin/pip2.7 /usr/local/bin/pip3.5 /usr/local/bin/pip3.4 /usr/local/bin/pip /usr/share/man/man1/pip.1.gz
$ whereis pip3
pip3: /usr/bin/pip3 /usr/local/bin/pip3 /usr/local/bin/pip3.5 /usr/local/bin/pip3.4 /usr/share/man/man1/pip3.1.gz

Rather than remove these files, you should move them, which seems the same to the computer but allows you to move them back if you later find something on your system was using it:

sudo mv /usr/local/bin/pip3.4 ~/bkupUsrLocalBinPip3.4
sudo mv /usr/local/bin/pip3.5 ~/bkupUsrLocalBinPip3.5
sudo mv /usr/local/bin/pip ~/bkupUsrLocalBinPip
sudo mv /usr/local/bin/pip3 ~/bkupUsrLocalBinPip3

The linked virtualenv command also was old so I moved it:

$ ls -lah /usr/local/bin/virtualenv
-rwxr-xr-x 1 root root 319 May 10  2014 /usr/local/bin/virtualenv

As discussed here, there is a trend to use “python3 -m (module)” to be clear rather than using “virtualenv” or “pip” command by itself – so instead of running “virtualenv (arguments)” try “python3 -m virtualenv (arguments)”. And this works! So instead of the command in the example document:

virtualenv -p python3 $HOME/tmp/deepspeech-venv/

to start off a virtualenv (separating your module installations in a directory instead of installing system-wide), you would run

python3 -m virtualenv -p python3  $HOME/tmp/deepspeech-venv/

after “pip3 install virtualenv” is run, making sure that you have that module.

Leave a Reply

Your email address will not be published. Required fields are marked *

one + four =