Getting a recent Python on Red Hat or CentOS
The IUS Community Project is a free and unsupported effort by the RPM engineers at Rackspace Hosting to provide recent editions of popular open source tools. Packages include PHP, MySQL and Python. Rackspace have made up-to-date packages available to their Enterprise customers for some time, but recently the idea of making packages available as “The Fedora of Rackspace RPMs” has emerged. Packages make the journey from the Testing IUS repository, to the Stable IUS repository, until eventually they make it into the Enterprise edition, which is only available to paying rackspace customers. IUS is not a Rackspace service - it’s a volunteer project. If you want to get involved, read the project FAQ here.
Naturally, using these packages carries with it the usual warnings - these packages are not supported by Red Hat, or the CentOS Project. They are bleeding edge, and for production use it is always best to use your vanilla distribution packages. However, if you have to upgrade, this is probably the best way to do it. But if it breaks, you get to keep the pieces.
Getting Set Up
First we need to obtain and install two RPMs - these provide entries in /etc/yum.repos.d for the Fedora EPEL repository, and the IUS repository.
$ wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/x86_64/epel-release-1-1.ius.el5.noarch.rpm
$ wget http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/x86_64/ius-release-1-2.ius.el5.noarch.rpm
$ yum localinstall epel-release-1-1.ius.el5.noarch.rpm ius-release-1-2.ius.el5.noarch.rpm --nogpgcheck
We then clean the yum repository metadata:
$ yum clean all
Now we can see new packages:
# yum list available python26*
Excluding Packages from CentOS-5 - Updates
Finished
Excluding Packages from CentOS-5 - Base
Finished
Available Packages
python26-debuginfo.x86_64 2.6.2-1.ius.el5 ius
python26-devel.x86_64 2.6.2-1.ius.el5 ius
python26-elixir.noarch 0.6.1-1.ius.el5 ius
python26-httplib2.noarch 0.5.0-1.ius.el5 ius
python26-jsonschema.noarch 0.2a-1.ius.el5 ius
python26-lxml.i386 2.0.11-1.ius.el5 ius
python26-lxml-debuginfo.i386 2.0.11-1.ius.el5 ius
python26-memcached.noarch 1.43-5.ius.el5 ius
python26-mysqldb.x86_64 1.2.3c1-1.ius.el5 ius
python26-mysqldb-debuginfo.x86_64 1.2.3c1-1.ius.el5 ius
python26-nose.noarch 0.11.1-1.ius.el5 ius
python26-setuptools.noarch 0.6c9-1.1.ius.el5 ius
python26-simplejson.i386 2.0.9-1.ius.el5 ius
python26-simplejson-debuginfo.i386 2.0.9-1.ius.el5 ius
python26-sqlalchemy.noarch 0.5.5-1.ius.el5 ius
python26-test.x86_64 2.6.2-1.ius.el5 ius
python26-tools.x86_64 2.6.2-1.ius.el5 ius
Now let’s install it and have a look:
# yum install python26
# rpm -ql python26 | grep '/usr/bin'
/usr/bin/pydoc2.6
/usr/bin/python2.6
/usr/bin/python2.6-config
Let’s give it a try:
# python2.6
Python 2.6.2 (r262:71600, Sep 26 2009, 03:35:59)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> blog_entry = {}
>>> blog_entry['title'] = 'Portable Serialisation with JSON'
>>> blog_entry['categories'] = ('programming', 'json', 'python')
>>> blog_entry['published'] = 'Yes'
>>> blog_entry['author'] = 'Stephen Nelson-Smith'
>>> with open('blog.json', mode='w') as file:
... json.dump(blog_entry, file)
...
# cat blog.json
{"published": "Yes", "author": "Stephen Nelson-Smith", "categories": ["programming", "json", "python"], "title": "Portable Serialisation with JSON"}
We’ve just tested two new features - a built-in JSON library, and Python’s new ‘with’ statement. Congratulations, you now have a modern Python.