54

I'm wanting to use some newer software that requires Python 2.6, and we currently have both 2.4 and 2.6 installed on our dedicated CentOS server, which looks like this:

$ which python
/usr/local/bin/python
$ which python2.6
/usr/bin/python2.6
$ which python2.4
/usr/local/bin/python2.4
$ ls -l /usr/local/bin/py*
-rwxr-xr-x 1 root root      81 Aug  9  2007 /usr/local/bin/pydoc
-rwxr-xr-x 2 root root 3394082 Aug  9  2007 /usr/local/bin/python
-rwxr-xr-x 2 root root 3394082 Aug  9  2007 /usr/local/bin/python2.4

How can I switch it to start using 2.6 as the default python?

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
Kristopher Ives
  • 5,838
  • 7
  • 42
  • 67
  • possible duplicate of [Two versions of python on linux. how to make 2.7 the default](http://stackoverflow.com/questions/19256127/two-versions-of-python-on-linux-how-to-make-2-7-the-default) – AncientSwordRage Mar 18 '15 at 14:24
  • 1
    That was asked 3 years later and is for 2.7 not 2.6, so it's not a duplicate. – Kristopher Ives Mar 19 '15 at 15:33
  • But I think it has a better answer, and the version discussed isn't irrelevant - it's the fact you have two, and want to change the default. " *You probably don't actually want to change your default Python......On top of that, monkeying with /usr/bin can break your package manager's ability to manage packages.* " - That stands apart from any version issues you have, and shows why the highest voted answer doesn't provide enough info. Also, presumably, you want people to be pointed at the latest answer, so the date thing doesn't matter either. – AncientSwordRage Mar 19 '15 at 15:37

6 Answers6

65

As root:

ln -sf /usr/bin/python2.6 /usr/local/bin/python

This will make a symbolic link from /usr/local/bin/python --> /usr/bin/python2.6 (replacing the old hardlink).

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 2
    according to the question 2.6 is in /usr/bin – SiggyF Jul 26 '10 at 22:55
  • and if it seems not to work (i noticed on SLC5), try logging out/in – roman Jan 08 '12 at 14:48
  • but you still have problems with sudo, i think this answer is not complete. use: update-alternatives --config python see this post: http://codeghar.wordpress.com/2009/01/27/update-alternatives-in-debian/ – roman Jan 08 '12 at 15:35
  • 4
    roman, this solution is not meant for any OS in any situation. Changing the default Python in Ubuntu for example, may break your system since package updates are likely to place updates in a place where your alternative python is not going to find them. For this particular question, the OP's default Python is in /usr/local, so it was not an OS default installation. Changing it to point to /usr/bin/python2.6 was therefore safe. Also note that `update-alternatives` adds symlinks in /usr/bin. If his default is in /usr/local, adding symlinks to /usr/bin won't make a difference. – unutbu Jan 08 '12 at 15:52
  • I would make it a symlink instead of a hard link, for clarity. – ʇsәɹoɈ Aug 18 '13 at 06:32
16

As an alternative, you can also just add an alias for the command "python" in the your bash shell's startup file.

so open the startup file: emacs ~/.bashrc

in the editor u append: alias "python" "python2.6"

and restart the shell.

Jian
  • 161
  • 1
  • 2
11

rm /usr/local/bin/python
ln -s /usr/local/bin/python2.6 /usr/local/bin/python

C Walker
  • 793
  • 2
  • 9
  • 19
5

Add an alias for the command "python" in the your bash shell's startup file. DON'T change a symbolic link from /usr/bin/python, because changing the default Python (in Ubuntu or Linux Mint for example) may break your system

P.S.: read other answers

Linux user
  • 51
  • 1
  • 1
4

In CentOS

ln -sf /usr/local/bin/python2.6 /usr/local/bin/python
ln -sf /usr/local/bin/python2.6 /usr/bin/python

To check version do:

python -V

Then to fix yum "No module named yum", you should do:

vi `which yum`

and modify #!/usr/bin/python to #!/usr/bin/python2.4

Slipstream
  • 13,455
  • 3
  • 59
  • 45
1

I had similar problem when using meld, I simply renamed the one under local and it worked. Not a good solution I know, but I can always take it back.

sudo mv /usr/local/bin/python /usr/local/bin/re_python
gezgingun
  • 462
  • 5
  • 11