16

I want to completely replace python 3 with python 2 in arch linux. I have already read https://wiki.archlinux.org/index.php/Python but it only provides a temporary fix. I need to ensure that when I call

#!/usr/bin/python

My program is using python 2 instead of python 3.

user1876508
  • 12,864
  • 21
  • 68
  • 105
  • 1
    you should really change your shebang to `#!/usr/bin/python2` if that's what you mean. – Eevee May 07 '15 at 23:40

2 Answers2

39

In Arch, /usr/bin/python is actually a symlink to python3. Assuming you've already installed python2, as root, change the symlink to point to python2:

cd /usr/bin
ls -l python
    lrwxrwxrwx 1 root root 7  5 sept. 07:04 python -> python3
ln -sf python2 python
ls -l python
    lrwxrwxrwx 1 root root 7 Dec 11 19:28 python -> python2

If you're using the python2-virtualenv package, then do the same for /usr/bin/virtualenv:

cd /usr/bin
ln -sf virtualenv2 virtualenv
Christopher Neylan
  • 8,018
  • 3
  • 38
  • 51
  • Thank you. I added this to the Arch wiki. – user1876508 Mar 14 '13 at 06:19
  • 5
    Just want to add that this seems really dangerous. Doesn't this just mean that all scripts using python on the base install (since python3 is default) will just start breaking? – deepelement Aug 27 '15 at 13:39
  • 2
    @ToddMorrison Totally possible, if they rely on something deprecated by v3! But it's what the OP asked for and Arch is all about ripping off that band-aid and python 2.7 is the end of the road for v2. – Christopher Neylan Aug 28 '15 at 17:33
  • 1
    Just a note that this may not be as permanent as it seems - after an update to python3 I found that the symlink /usr/bin/python was restored to link to python3 – Claudiu Jan 19 '17 at 11:42
10

Changing the default symlink is a bad idea, and it gets recreated on python3 updates. Instead, create a local python override:

sudoedit /usr/local/bin/python

Paste this inside and save the file:

#!/bin/bash
exec python2 "$@"

Don't forget to make it executable:

sudo chmod +x /usr/local/bin/python

Ivan Semkin
  • 179
  • 1
  • 14