how to upgrade python setuptools > 12.2 on ubuntu 15.04

The apt package seems to be 12.2

If I run sudo pip install -U setuptools the version seems to still be stuck at 12.2

$ python
>>> import pkg_resources
>>> r = pkg_resources.require(["setuptools"])[0]
>>> print("setuptools version: %s" % r.version)
setuptools version: 12.2

[EDIT]

Just noticed it's won't overwrite the OS version of setuptools:

Downloading/unpacking pip from https://pypi.python.org/packages/py2.py3/p/pip/pip-7.1.2-py2.py3-none-any.whl#md5=5ff9fec0be479e4e36df467556deed4d
  Downloading pip-7.1.2-py2.py3-none-any.whl (1.1MB): 1.1MB downloaded
Downloading/unpacking setuptools from https://pypi.python.org/packages/3.4/s/setuptools/setuptools-18.3.2-py2.py3-none-any.whl#md5=58c1e15fe0c124ab0880a2691f232434
  Downloading setuptools-18.3.2-py2.py3-none-any.whl (462kB): 462kB downloaded
Installing collected packages: pip, setuptools
  Found existing installation: pip 1.5.6
    Not uninstalling pip at /usr/lib/python2.7/dist-packages, owned by OS
  Found existing installation: setuptools 12.2
    Not uninstalling setuptools at /usr/lib/python2.7/dist-packages, owned by OS
Successfully installed pip setuptools
Cleaning up...

[/EDIT]


Solution 1:

  1. Remove the repository version

    sudo apt-get remove python-setuptools
    
  2. If necessary, install pip again

    wget https://bootstrap.pypa.io/get-pip.py
    sudo -H python get-pip.py
    
  3. Install setuptools via pip

    sudo -H pip install -U pip setuptools
    

And now, start you test again

% python
Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg_resources
>>> r = pkg_resources.require(["setuptools"])[0]
>>> print("setuptools version: %s" % r.version)
setuptools version: 18.3.2

Note

Installing any package that depends on either python-setuptools or python-pip will bring these packages back, so you may have to repeat this procedure!

Solution 2:

The solution proposed by A.B. may not quite be enough: in recent version of setuptools, pkg_resources is a package, whereas previously it was just a single module.

Updating setuptools in the way described will leave a stale pkg_resources.py{,c} around, which may lead to the following error when importing setuptools:

AttributeError: 'module' object has no attribute 'packaging'

To remove it, do the following:

  1. Find out where the outdated pkg_resources module is located:

    $ python -c 'import pkg_resources; print(pkg_resources.__file__)'
    /usr/lib/python2.7/dist-packages/pkg_resources.pyc
    
  2. Remove this file and its .py file:

    $ sudo rm /usr/lib/python2.7/dist-packages/pkg_resources.py*
    

Warning

This file might have been installed via the python-pkg-resources package. Therefore updating or reinstalling this package will reinstate the stale module! Also be aware that you're messing with a file which is supposed to be controlled by apt.