2

Using CentOS and upgrade from Python 2.6 to Python 2.7.8, I upgraded successfully by building from source. But it seems python command (/usr/bin/python) still points to old python 2.6 version? Wondering what is the safe way to change default python command to point to python2.7 location (which is /usr/local/bin/python2.7)?

[root@iZrj9aehttqhrnhsvyccszZ Python-2.7.8]# which python
/usr/bin/python
[root@iZrj9aehttqhrnhsvyccszZ Python-2.7.8]# ls -l /usr/bin/python
-rwxr-xr-x 2 root root 4864 Aug 18 23:14 /usr/bin/python
[root@iZrj9aehttqhrnhsvyccszZ Python-2.7.8]# uname -a
Linux iZrj9aehttqhrnhsvyccszZ 2.6.32-573.22.1.el6.x86_64 #1 SMP Wed Mar 23 03:35:39 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[root@iZrj9aehttqhrnhsvyccszZ Python-2.7.8]# which python2.7
/usr/local/bin/python2.7
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 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) – matias elgart Nov 16 '16 at 02:46

3 Answers3

3

For CentOS, you are better of using the pre-built Python version from the software collections library:

This is specifically built so as to be able to safely coexist with the system Python. The binaries are put together by Red Hat so as to provide more up to date versions of Python specifically for developers and running non system applications.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • So, should I not install python 3.x in my centos as it comes up with python 2.7? And if not so how can I use django 2.0.3 in it? – Siraj Alam May 12 '18 at 13:31
  • Create a new question rather than ask in a comment. Supply all details related to your situation in that new question. – Graham Dumpleton May 13 '18 at 03:27
2

You can create a symbolic link to solve it:

$ /usr/bin/python -V
Python 2.6.x
$ move /usr/bin/python /usr/bin/python2.6.x
$ ln -sf /usr/local/bin/python2.7 /usr/bin/python
$ which yum
/usr/bin/yum
$ head -1 /usr/bin/yum
#!/usr/bin/python
$ sed -i -e 's|python|python2.6.x|' /usr/bin/yum
$ head -1 /usr/bin/yum
#!/usr/bin/python2.6.x

However, I found this answer is better than mine: Two versions of python on linux. how to make 2.7 the default

Community
  • 1
  • 1
zjyExcelsior
  • 63
  • 1
  • 6
  • 1
    Replacing the system Python binary like that is a very bad idea. System tools may depend on the system Python being a specific version and for certain Python packages to be installed. Doing what you suggest can break your operating system in bad ways, to the extent that it may not start up. – Graham Dumpleton Nov 16 '16 at 03:07
  • Thanks zjyExcelsior, I know `ln` command, my initiative of asking this question is more about how to safely replace default `python` command, you can see comments from @GrahamDumpleton. Any thoughts? – Lin Ma Nov 16 '16 at 19:03
  • @GrahamDumpleton, good point, do you have any good ideas to do in a safe way? – Lin Ma Nov 16 '16 at 19:03
  • 1
    The only safe way to update the system Python in Linux distributions these days to a completely new version is to upgrade your operating system to a version which has the newer Python version you want as the default. The dependency on the system Python by applications related to the running operating system makes it too dangerous to replace the system Python. Perhaps you should be explaining why you need to update the system Python. You may be wanting to do it for the wrong reasons and there is a better solution to your problem. – Graham Dumpleton Nov 16 '16 at 19:42
  • 1
    @GrahamDumpleton I think, before create a symbolic link, we can move /usr/bin/python to /usr/bin/python2.6.x, and make yum and others to use /usr/bin/python2.6.x, it's enough. – zjyExcelsior Nov 18 '16 at 07:09
  • 1
    @LinMa is better than my method. – zjyExcelsior Nov 18 '16 at 07:13
  • 1
    No, you simply shouldn't replace it. It will cause you problems. As described in that other issue, don't use ``#!/usr/bin/python`` in your own scripts but ``#!/usr/bin/env python`` and preferably use Python virtual environments as well. If you had explained why you wanted to do this in the first place then could have provided that advice. – Graham Dumpleton Nov 18 '16 at 07:34
  • @GrahamDumpleton, good ask. I upgrade to 2.7 (from 2.6) since I need to use some Python lib (e.g. tensorflow) which requires 2.7. :) – Lin Ma Nov 19 '16 at 07:20
1

The simple way upgrade python2.6 to 2.7 on centOS: install-python-2-7-on-centos-rhel

nepaul
  • 11
  • 3
  • Thanks nepaul, I follow similar steps, but the question I am asking is how to safely replace default `python` command, in the guide you posted, it generate executable `python2.7` other than `python` -- the same situation as mine. – Lin Ma Nov 16 '16 at 19:02