I recently was installing Django for a test project and needed to upgrade Python on a CentOS server to 2.6. The current revisions available via yum on CentOS are 2.4. The yum package manager requires Python 2.4 still to function properly so upgrading Python to 2.6 manually will break the CentOS yum package manager. After a little bit of research I found a yum repository that would allow you to install Python version 2.6 in parallel to Python version 2.4. Below I explain how to install the new Python version, not break the yum package manager, and modify a Django project that references Python to use the 2.6 version instead of the 2.4 version.
**UPDATE** When I initially wrote this article the server I installed the CHL repository on already had the EPEL repository installed. You can read this article on installing the EPEL repository. Thanks to Tom for noting the EPEL repository is required in the comments section of this article.
Install CHL Yum Repo That Provides Python 2.6 On CentOS 5:
First I want to say thanks to Chris Lea for providing the yum repository that makes this whole process easy. The below to commands will install a yum repository and the required GPG key for the repo.
Use The RPM Command To Install The CHL Yum Repo And CHL Yum RPM GPG Key:
- rpm -Uvh http://yum.chrislea.com/centos/5/i386/chl-release-5-3.noarch.rpm
- rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CHL
The above will install the CHL yum repository by adding chl.repo to the /etc/yum.repos.d directory. It will add the ability to search yum.chrislea.com to your current list of repositories. The Python packages available via CHL are titled python26-* instead of python-*. This allows you to install a completely separate version of Python and run both on your system. You will leave the current version of Python how it is so the other packages on the server that require python don’t break.
Now Install Python Version 2.6, Mod_Python (v2.6), Django, And MySQL-Python (v2.60):
The below command will install Python 2.6 and supporting packages that allow Django to run on a server using Apache, MySQL, and Python. Notice the “26” on the end of each package that will literally install the 2.6 version of each one.
Install python26, mod_python26, django, & mysql-python26 on CentOS:
- yum install python26 mod_python26 Django MySQL-python26
Once installed you will now have the ability to run Python version 2.6 by issuing the python26 from the command line. The above packages will also allow interaction with Apache and MySQL so a fully functional dynamic Django site can now be configured.
Modify Django Environment Variables To Use Python 2.6:
One other thing you will need to modify is the default Django project files you will eventually install. The files will specify at the top of them that the code below is Python. You will want to modify that configuration line and make sure that it references python26 instead so the application knows to process the code using the 2.6 version of Python.
Default Python Application Environment Specification:
- #!/usr/bin/env python
Modified Python Application Environment Specification:
- #!/usr/bin/env python26
So search through the Django project and check for any files with the default Python specification. Modify that default to the modified one that tells the Django project to use Python version 2.6.