How to upgrade python2.6 to python2.7 on CentOS 6.5?

I have problem running a python script developed using python2.7. My python environment is Python2.6.6 under CentOS 6.5. Some code is broken when running in old python version. For example, the new feature {key:0 for key in amap} does not work for python2.6. And I also meet the error:”ValueError: zero length field name in format“. This is because in python2.7 and later version, you do not need to specify a location number to format a string but in python2.6 you do need(see this post and this article).  I need to upgrade the old version of python to a later version. However, I could not do this using yum on my centos5 server. Python2.6.6 is the final and highest version available on centos 6.5 for yum.  I have to download the source code of python 2.7 and compile it from scratch.

yum -y groupinstall 'Development Tools'
yum -y install openssl-devel* ncurses-devel* zlib*
yum -y install bzip2 bzip2-devel bzip2-libs
wget http://python.org/ftp/python/2.7/Python-2.7.tgz
tar xfz Python-2.7.tgz
 cd Python-2.7
./configure
make
make install

Now python 2.7 is installed as /usr/local/bin/python. If you type “python” on the command line, shell will actually execute python 2.7. There is also a bin file called  /usr/local/bin/python2.7, which is exactly the same as /usr/local/bin/python. The highest version of python2.7 is 2.7.15. You can download it from http://python.org/ftp/python/2.7.15/Python-2.7.15.tgz and build from that source. But all 2.7 versions produce the same bin file name: python2.7, and the same lib name /usr/local/lib/python2.7/. It is good to install the upgraded python in /usr/local/bin and /usr/local/lib/ instead of /usr/bin/ and /usr/lib where the old version(2.6.6) of python was installed by the system. Otherwise, the new binaries would overwrite the old ones, which would break other software(such as yum) that are dependent on the old version of python.

It seems we have successfully upgraded python from 2.6.6 to 2.7. But do not  be too optimistic. If you try to install third party python libraries using pip. You may encounter problems saying the library has already been installed.

Requirement already satisfied: tqdm in /usr/local/lib/python2.6/site-packages

Yes, but the libraries were installed for the old version of python(2.6.6). Why pip cannot install the libraries for the upgraded python version(2.7)? The old python is not erased actually. You can see /usr/lib/python2.6/ still exists. And you cannot remove the python packages using yum(yum erase python) because yum is dependent on python and protected to be removed. Upgrading pip does not work either. “yum upgrade pip” can update the pip version to pip-18.0  but the latest pip still refers to the old python version.
If you use the following command as stated in this post, you will get the error “module pip not found”

python -m pip install tdqm

The solution is also  in one of the answers in that post:

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip2.7 install tdqm

The newly installed pip2.7 will download the libraries and install them to /usr/local/lib/python2.7/

Another way to install packages for multiple python versions, not tried it by myself

 

Comments are closed, but trackbacks and pingbacks are open.