2008-11-21
Tags: Programming

Python 2.6.1 on Ubuntu

Python 2.6.1 is the new stable Python version, but Ubuntu 8.10 Intrepid Ibex does not come with it. Here is how to install it though.

First of all, you must learn about Python 2.6.

Excited about the new features? Then now realize that while Python 2.6 prepares you for 3.0, it also makes your code incompatible with 2.5 if you use any of the new features! You must keep this in mind when you decide what to use!

Most library authors won't bother updating their code with Python 2.6 features, as this would mean incompatibility with Python 2.4 and 2.5 (as well as 3.0). Python 2.6 is a version that is only compatible with itself.

In other words, there are two antagonic forces here: On the one hand, you want to use the latest features of the language and write modern code that won't have to be heavily rewritten/updated soon (and using Python 2.6 is the best option for that right now, because nobody is using Python 3.0 yet); on the other hand, the lack of libraries and batteries that you can use, as well as other people using your code, may dictate that you must stay in previous versions for a long time.

We would be talking about using Python 3.0 here if only there were any libraries for it already...

Latest Zope/Plone still use Python 2.4!

In these times, simple libraries that are more likely to be ported sooner to 3.0 make a good choice.

If you have to stay in Python 2.5... you still can import these:

from __future__ import with_statement
from __future__ import absolute_import
# http://docs.python.org/whatsnew/pep-328.html

On the other hand, if you are targeting Python 2.6, why would you not use all its features? Therefore, add this to the top of your files:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
# http://docs.python.org/whatsnew/pep-328.html
from __future__ import print_function   # deletes the print statement
from __future__ import unicode_literals # unicode by default

To sum up, Python 2.6 is for people who can get away with using Python 2.6. These will benefit from having fewer changes to make when the time to upgrade to 3.0 comes, and from using new language features right now. But there is a small price to pay in library installation complexity, as we shall see soon. (Keep reading.)

Installing Python 2.6.1 on Ubuntu Intrepid Ibex

Download the sources:

wget http://python.org/ftp/python/2.6.1/Python-2.6.1.tar.bz2

Decompress them:

tar -xjf Python-2.6.1.tar.bz2
cd Python-2.6.1/

Install dependencies:

sudo apt-get install checkinstall build-essential libc6-dev tk8.4-dev libgdbm-dev
sudo apt-get install libdb-dev libreadline-dev libsqlite3-dev libncurses5-dev
sudo apt-get install libbz2-dev libssl-dev # and possibly a few others

Configure, make, test:

# I install it at /progs because I am afraid of messing up other directories
mkdir /progs
./configure --prefix=/progs --enable-ipv6  # --enable-shared
make
# It finishes with this complaint:
# Failed to find the necessary bits to build these modules:
# bsddb185           sunaudiodev
# These seem not to be a problem. I continue anyway.
make test # (optional, takes 15 minutes or so)
# 327 tests OK.
# 33 tests skipped: (...)
# Those skips are all expected on linux2.

Now you can do one of two things. Either create a .deb package:

checkinstall -D --pkgname=python2.6.1 --pkgversion=2.6.1 --maintainer=nandoflorestan@gmail.com--inspect --backup=yes --install=no make altinstall

And make sure you answer YES when asked to create a default set of docs!

...or install directly:

make install

The final step of the installation is to make your new python and python2.6 binaries available in the path, such that they will be found before python2.5. However, you had better do this for your user only, lest your whole Ubuntu operating system suffer (since all Linux distros depend heavily on Python). Being cautious this way, I create symbolic links to python and python2.6 from the bin directory of my user:

cd /home/nando/python/bin
ln -s /progs/bin/python2.6
ln -s /progs/bin/python

Now you can test the interpreter:

python

This tutorial is based on these.

Now let us install a couple libraries so that our new Python 2.6 may be minimally useful:


Library hell

Now if you decide to go ahead and use 2.6, you are in library hell -- at least when the library you need is not pure Python. For instance, to be able to manipulate images, you must install PIL yourself, and it may not be trivial:

sudo apt-get install libjpeg62 libjpeg62-dev libfreetype6 libfreetype6-dev tk-dev tcl-dev
wget http://effbot.org/downloads/Imaging-1.1.6.tar.gz
tar xvzf Imaging-1.1.6.tar.gz
cd Imaging-1.1.6
sudo /progs/bin/python2.6 setup.py build_ext -i

The last command fails in Ubuntu Intrepid Ibex, saying: "_imagingtk.c:20:16: error: tk.h: No such file or directory"

If you run "locate tk.h", you discover the missing file is really at: /usr/include/tcl8.4/tk.h

Because this is a gcc problem and I am just a Python guy, I don't know how to solve it. I wonder if I really need this _imagingtk stuff... I don't use TK... So I just uninstall the python-tk and tk packages. This way it succeeds and just says:

*** TKINTER support not available

An alternative solution to the above problem might be to just turn TKINTER support off by commenting out these lines in setup.py (starting at 328):

    '''
    elif feature.tcl and feature.tk:
        exts.append(Extension(
            "_imagingtk", ["_imagingtk.c", "Tk/tkImaging.c"],
            libraries=[feature.tcl, feature.tk]
            ))
    '''

So now I test PIL:

/progs/bin/python2.6 selftest.py # runs the tests

Unfortunately one important test fails, showing a new problem for me to solve:

  File "PIL/Image.py", line 375, in _getdecoder
    raise IOError("decoder %s not available" % decoder_name)
IOError: decoder jpeg not available
1 items had failures:
   1 of  57 in selftest.testimage
***Test Failed*** 1 failures.
*** 1 tests of 57 failed.

Since all required libraries are installed, I don't know what is going on. Googling the problem, I see people have solved this by repeating the whole process after everything is in place. Yeah I know, this is stupid. Anyway, I go ahead and delete the whole directory and repeat the steps and this time it works:

cd ..
rm -r Imaging-1.1.6
tar xvzf Imaging-1.1.6.tar.gz
cd Imaging-1.1.6/
/progs/bin/python2.6 setup.py build_ext -i
/progs/bin/python2.6 selftest.py # runs the tests
/progs/bin/python2.6 setup.py install

Library hell indeed. If it took one working day to install each library. Fortunately, pure Python libraries written for 2.5 seem to work just as well with 2.6:

Setuptools

Installing setuptools is quite easy:

wget http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c9-py2.6.egg
sh setuptools-0.6c9-py2.6.egg

After this, I find that I can run both bpython and ipython interactive interpreters, just reusing their old eggs that were already installed in my system for Python 2.5. I just change the shebang at the top of the commands to this:

#!/usr/bin/env python

This way, our Python 2.6 can be picked automatically (depending on $PATH).

cherrypy, genshi and most other packages just work too.

Conclusion

  • If you are writing an application: make an effort to use Python 2.6 right now.
  • If you are writing a library: (very hard) try to support both Python 2.5 and Python 3.0.

← voltar ao blog