Python 2.7 in Debian Squeeze 6.0

19 May 2011
Posted by stylesen

In work I wanted python2.7 for using some latest modules. I run Debian 6.0 ie., squeeze in my laptop, but python2.7 is available in Wheezy or Debian testing. In order to install python2.7 from the testing repository add the following in your sources.list file ie., /etc/apt/sources.list

deb http://mirror.cse.iitk.ac.in/debian/ testing main contrib

After adding the testing repository, do an update and then install python2.7 package as follows:

$ sudo aptitude update

$ sudo aptitude install python2.7

Now we will have python2.7 installed in our debian squeeze system, but wait! This will give more problems since all other softwares installed in the system are dependent on python2.6 which is the default in Debian 6.0. One of the first problems is 'update-manager' will not work, since it depends on python2.6. I also suggest to remove the testing repository added to 'sources.list' if you do not plan to 'dist-upgrade' to the testing (wheezy) release.

It will be better to have a way to switch between python versions (that too Debian way would be great!). Following is how I acheieved this using update-alternatives.

Add all the python versions and create a 'update-alternatives' link:

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 2

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.6 1

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.5 0

NOTE: The 2, 1, 0 at the end of the commands above are priorities and it is required to define the priorities in 'auto-mode'.

Once we have added all the python versions, switching between these python versions is very easy like the following:

$ sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python2.7   2         auto mode
  1            /usr/bin/python2.5   0         manual mode
* 2            /usr/bin/python2.6   1         manual mode
  3            /usr/bin/python2.7   2         manual mode

Press enter to keep the current choice[*], or type selection number: 3
update-alternatives: using /usr/bin/python2.7 to provide /usr/bin/python (python) in manual mode.
stylesen@dejas:~$ python
Python 2.7.1+ (default, Apr 20 2011, 22:33:39)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
stylesen@dejas:~$ sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python2.7   2         auto mode
  1            /usr/bin/python2.5   0         manual mode
  2            /usr/bin/python2.6   1         manual mode
* 3            /usr/bin/python2.7   2         manual mode

Press enter to keep the current choice[*], or type selection number: 1
update-alternatives: using /usr/bin/python2.5 to provide /usr/bin/python (python) in manual mode.
stylesen@dejas:~$ python
Python 2.5.5 (r255:77872, Nov 28 2010, 19:00:19)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
stylesen@dejas:~$ sudo update-alternatives --config python
There are 3 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python2.7   2         auto mode
* 1            /usr/bin/python2.5   0         manual mode
  2            /usr/bin/python2.6   1         manual mode
  3            /usr/bin/python2.7   2         manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/python2.6 to provide /usr/bin/python (python) in manual mode.
stylesen@dejas:~$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Thus we can have python2.7 installed in a debian squeeze system.

Update: Another non-debian way of installing multiple versions of python using 'pythonbrew' is available in this link - http://www.howopensource.com/2011/05/how-to-install-and-manage-different-versions-of-python-in-linux/

This is extremely poor advice

This breaks package updates, any python module and extension packages in debian will not work with your python2.7 and you will break scripts that rely on /usr/bin/python pointing to the defined and tested version of python for that release. Introducing a new version of python 2.x into debian is a long a difficult process, changing the default /usr/bin/python is even harder. These steps take a huge amount of work by the debian developers... why do you think you're going to pull that off on your own with so little effort? Worse, your instructions will also end up upgrading libc6 (on i386) and a number of other system libraries on the machine. You've created some frankendebian for yourself and you can expect it to be a painful thing to deal with... but please don't inflict those bad choices on others by encouraging other people to do this too ... who will then end up on irc or on mailing lists trying to unbreak their systems.


Will break python package updates

Just a note that using update-alternatives will cause problems when you go to update packages.During the update process python packages check that the version of the interpreter is correct by using os.readlink and then using some string manipulation to look for the python version. This assumes that the symlink /usr/bin/python points at a versioned file for example /usr/bin/python2.6The fix is to modify /usr/share/pycentral-data/pyversions.py around row 151 apply the following:>        try:>             _default_version = link = os.readlink('/usr/bin/python')+             # Check for alternatives+             if 'alternatives' in link:+                _default_version = link = os.readlink(link)>        except OSError:Now if you are using debian alternatives the version can be correctly resolved and package updates will again work.


question

Hello, is there a way to make them all automatic?


nice!

thx for that! I didn't know something about configuration with update-alternatives... its really convenient! :) Kim


Great tutorial!

Thank you, it was very helpful for me!Greetings from Germany :)


fanx - nice writeup

I just started using debian (coming from windows via ubuntu) and this really helped me - much nicer than compiling from source - I didn't know I could wheel testing packages in also.fanx again!


great

Thank you!of Brazil


thx

great tip!  thank you!


Virtualenv can provide further isolation

You could create a virtualenv where the default interpreter will be the 2.7 interpreter. This way you needn't run update-alternatives everytime.


Really useful

This blog post is really useful to me. I remember doing something like this earlier for another package. But if I face similar problem again I would have been googling to find the solution again. Also this post explains the use of update-alternatives feature of Debian. 


Great help

Hi i really appreciated this help to configure my debian squeeze server ;-P