How to fix an AssertionError when trying to list Python packages with the ‘pip list’ command

The Problem

Getting the following assertion error when trying to list the Python packages:

pip list

...
Exception:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 134, in main
    status = self.run(options, args)
  File "/usr/lib/python2.7/dist-packages/pip/commands/list.py", line 80, in run
    self.run_listing(options)
  File "/usr/lib/python2.7/dist-packages/pip/commands/list.py", line 127, in run_listing
    self.output_package_listing(installed_packages)
  File "/usr/lib/python2.7/dist-packages/pip/commands/list.py", line 136, in output_package_listing
    if dist_is_editable(dist):
  File "/usr/lib/python2.7/dist-packages/pip/util.py", line 347, in dist_is_editable
    req = FrozenRequirement.from_dist(dist, [])
  File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 194, in from_dist
    assert len(specs) == 1 and specs[0][0] == '=='
AssertionError

means that either pip is outdated on the host machine or the distribute package.

The Solution

Upgrading pip

pip can be upgraded using the following set of commands:

# download get-pip.py via curl
curl -O https://bootstrap.pypa.io/get-pip.py
or
# download get-pip.py via wget
wget https://bootstrap.pypa.io/get-pip.py

# installing pip
sudo python get-pip.py

or

pip install -U pip

More information about pip installation and upgrade can be found here.

Upgrading the distribute package

pip install --upgrade distribute