SoFunction
Updated on 2024-10-28

Upgrading the Python version in CentOS in detail

CentOS Upgrade Python 2.6 to Pythno 2.7

Recently I was messing around with Python on Linode and got an error like ValueError: zero length field name in format;

After looking through the documentation, I found that I could solve the problem by upgrading Python to version 2.7 or higher.

Used Pythno -V to see that the current version on Centos is 2.6.

Python 2.6 is installed by default in CentOS 6 series.

So we might get an error like this

ValueError: zero length field name in format

At this point we need to upgrade Pythno 2.6 to Python 2.7 or higher

Upgrading Python

The default Python installed on the system is 2.7, we need to upgrade to Python 2.7, use the wget command to download the source file from the official, and then unzip it to compile it.

wget /ftp/python/2.7.13/Python-2.7.
unxz Python-2.7.
tar -vxf Python-2.7.

After executing the above commands, you will unzip the folder Python-2.7.13, go to this directory and execute the following commands to configure it

./configure --enable-shared --enable-loadable-sqlite-extensions --with-zlib

fulfillment

vim ./Modules/Setup

Find #zlib -I$(prefix)/include -L$(exec_prefix)/lib -lz uncomment it and save it, then compile and install it

make && make install

After installing Python 2.7 we need to make a backup of Python 2.6 first.

Execute the following commands to make a backup of Python 2.6, then create a soft link to Python 2.7 and use Python 2.7 as the default Python

mv /usr/bin/python /usr/bin/python2.6
ln -s /usr/local/bin/python2.7 /usr/bin/python


Then edit /usr/bin/yum and change the first line of #! /usr/bin/python to #! /usr/bin/python2.6.6

Executing the yum command now doesn't give you the same error message as before.

We run python -V to see the version information, and if an error occurs

error while loading shared libraries: libpython2..1.0: cannot open shared object file: No such file or directory

Edit Configuration File

vim /etc/

Check if there is /usr/local/lib, if not, add a line, save and exit;

Then refresh the configuration

/sbin/ldconfig 
/sbin/ldconfig -v

PS: If you have installed Pip in python 2.6, you need to reinstall it.

PS2: If you installed third-party libraries with pip install xxx, you will need to reinstall them as well.