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
1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
Create a new question rather than ask in a comment. Supply all details related to your situation in that new question.
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

8 Comments

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.
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.
@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.
@LinMa <stackoverflow.com/questions/19256127/…> is better than my method.
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.
|
1

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.