Building and Installing Python 2.7 RPMs on CentOS 5.7


I was asked today to install Python 2.7 on a CentOS based node and I thought I’d take this oportunity to add a companion article to my Python 2.6 article.

We’re all well aware that CentOS is pretty backwards when it comes to having the latest and greatest sotware packages and is particularly finicky when it comes to Python since so much of RHEL depends on it.

As a rule, I refuse to rush in and install anything in production that isn’t in a manageable package format such as RPM. I need to be able to predictably reproduce software installs across a large number of nodes.

The following steps will not clobber your default Python 2.4 install and will keep both CentOS and your developers happy.

So, here we go.

Install the dependancies.

sudo yum -y install rpmdevtools tk-devel tcl-devel expat-devel db4-devel \
                    gdbm-devel sqlite-devel bzip2-devel openssl-devel \
                    ncurses-devel readline-devel

Setup you RPM build envirnoment.

rpmdev-setuptree

Grab my spec file.

wget https://raw.github.com/nmilford/specfiles/master/python-2.7/python27-2.7.2.spec \
     -O ~/rpmbuild/SPECS/python27-2.7.2.spec 
wget http://www.python.org/ftp/python/2.7.2/Python-2.7.2.tar.bz2 \
     -O ~/rpmbuild/SOURCES/Python-2.7.2.tar.bz2

Build RPM. (FYI, the QA_RPATHS variable tells the rpmbuild to skip some file path errors).

QA_RPATHS=$[ 0x0001|0x0010 ] rpmbuild -bb ~/rpmbuild/SPECS/python-2.7.2.spec

Install the RPMs.

sudo rpm -Uvh ~/rpmbuild/RPMS/x86_64/python27*.rpm

Now on to the the setuptools.

Grab my spec file.

wget https://raw.github.com/nmilford/specfiles/master/python-2.7/python27-setuptools-0.6c11.spec \
     -O ~/rpmbuild/SPECS/python27-setuptools-0.6c11.spec 

Grab the source.

wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz \
     -O ~/rpmbuild/SOURCES/setuptools-0.6c11.tar.gz

Build the RPMs.

rpmbuild -bb ~/rpmbuild/SPECS/python27-setuptools-0.6c11.spec

Install the RPMs.

sudo rpm -Uvh ~/rpmbuild/RPMS/noarch/python27-setuptools-0.6c11-milford.noarch.rpm

Now, we’ll install MySQL-python as an example.

Grab the mysql-dev package

yum -y install mysql-devel

Grab, build and install the MySQL-python package.

curl http://superb-sea2.dl.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz | tar zxv
cd MySQL-python-1.2.3
python2.7 setup.py build
python2.7 setup.py install

Like with the previous Python 2.6 article, note how I called the script explicitly using the following python binary: /usr/bin/python2.7

Now we’re good to give it the old test thus:

python2.7 -c "import MySQLdb"

If it doesn’t puke out some error message, you’re all set.

Happy pythoning.

Share
  • Guest

    There’s an error while installing setuptools:
    RPM build errors:
        File not found: /root/rpmbuild/BUILDROOT/python27-setuptools-0.6c11-milford.i386/usr/lib/python2.7/site-packages/easy_install.pyo
        File not found: /root/rpmbuild/BUILDROOT/python27-setuptools-0.6c11-milford.i386/usr/lib/python2.7/site-packages/pkg_resources.pyo
        File not found: /root/rpmbuild/BUILDROOT/python27-setuptools-0.6c11-milford.i386/usr/lib/python2.7/site-packages/site.pyo

  • Benjamin Clark

    Thank you very much for all this.  I have found that these spec files work almost unchanged for centos6.
    I had to add this to the spec:
    Provides: python-abi = %{libvers}
    Provides: python(abi) = %{libvers}
    in order to get devel, debug and tkinter to install.  I hope that’s not going to mess up the ‘alt’ aspect of the install.

    One question.  I think this probably didn’t originate with you, but why does setuptools need python-devel?  Why not just python?  Isn’t one of the nice things about working with easy_install the fact that you can use platform-specific eggs and not have to have your build tools on all your servers?

    • Alex Rodenberg

      where exactly did you add those lines?

      • Coregun

        it works for me after I added listed links after Summary” and before “%description”
        Summary: An interpreted, interactive, object-oriented programming language.Name: python27Version: %{version}Release: %{release}License: PSFGroup: Development/LanguagesSource: Python-%{version}.tar.bz2%if %{include_docs}Source1: %{name}-%{version}-docs-html.tar.bz2%endifBuildRoot: %{_tmppath}/%{name}-%{version}-rootBuildRequires: gcc make expat-devel db4-devel gdbm-devel sqlite-devel readline-devel zlib-devel bzip2-devel openssl-develAutoReq: noPrefix: %{__prefix}Vendor: Sean Reifschneider Packager: Nathan Milford Provides: python-abi = %{libvers}Provides: python(abi) = %{libvers}%description

  • http://www.fotografiacotidiana.com.br Rafael Lopes

    I have a huge problem on a server, and don’t know how to fix.
    I would like to remove py2.4 and install py2.7 on my CentOS, so I unistalled python via yum erase (–no-dependency), and now i can’t use yum, since it’s written in python. Ok, i got the py2.7 source, compilled it and installed as well.

    I can use py2.7 cli, it’s on the machine, BUT there is a thing called python-rpm (or rpm-python) that is not working, so yum can’t “import rpm” nicely.

    Can you help me to re-integrate python 2.7 with my system again? Not using yum to do nothing, since it’s not working? :D

    I was dumb, and only thought using virtualenv when the mess was done, lol.

  • Guest

    I have the same issue as below,please let me know what i can do,thank you very much!
    There’s an error while installing setuptools:
    RPM build errors:
       
    File not found:
    /root/rpmbuild/BUILDROOT/python27-setuptools-0.6c11-milford.i386/usr/lib/python2.7/site-packages/easy_install.pyo
       
    File not found:
    /root/rpmbuild/BUILDROOT/python27-setuptools-0.6c11-milford.i386/usr/lib/python2.7/site-packages/pkg_resources.pyo
        File not found: /root/rpmbuild/BUILDROOT/python27-setuptools-0.6c11-milford.i386/usr/lib/python2.7/site-packages/site.pyo

    • Guest

      I just fix it myself, as i install python2.7 on CentOS 6.2, commented out easy_install.pyo,pkg_resources.pyo and site.pyo in ~/rpmbuild/SPECS/python27-setuptools-0.6c11.spec

      #/usr/lib/python2.7/site-packages/easy_install.pyo
      #/usr/lib/python2.7/site-packages/pkg_resources.pyo
      #/usr/lib/python2.7/site-packages/site.pyo

      then run rpmbuild -bb ~/rpmbuild/SPECS/python27-setuptools-0.6c11.spec,it works.

  • Evaggelos Balaskas

    Very useful post. Thanks.
    Just added python2.7.2 to my repo

  • Jay

    Hi,

    thanks for this guide.

    I ran into a few issues while following it.

    ——Build RPM. (FYI, the QA_RPATHS variable tells the rpmbuild to skip some file path errors).# QA_RPATHS=$[ 0x0001|0x0010 ] rpmbuild -bb ~/rpmbuild/SPECS/python-2.7.2.specshould be # QA_RPATHS=$[ 0x0001|0x0010 ] rpmbuild -bb ~/rpmbuild/SPECS/python27-2.7.2.spec——Build the RPMs.# rpmbuild -bb ~/rpmbuild/SPECS/python27-setuptools-0.6c11.specfailed, needed to modify the file: ~/rpmbuild/SPECS/python27-setuptools-0.6c11.specunder “%files” added a “local/” after “/user” in the path of each fileFor Example:/usr/lib/python2.7/site-packages/setuptools/changed to:/usr/local/lib/python2.7/site-packages/setuptools/after that the package was created——Install the RPMs.——# sudo rpm -Uvh ~/rpmbuild/RPMS/noarch/python27-setuptools-0.6c11-milford.noarch.rpmerror: Failed dependencies: /usr/local/bin/python2.7 is needed by python27-setuptools-0.6c11-milford.noarchok, tried again with “–nodeps”# sudo rpm -Uvh –nodeps ~/rpmbuild/RPMS/noarch/python27-setuptools-0.6c11-milford.noarch.rpmand that installed justfine.——after that I could use # python2.7Now, if I could just get archipel to install… that still gives me a headache, and it was the reason I needed 2.7 in the first place :)
    Feel like giving that a try? :)

    Thanks for the guide, without it I would have been lost :)

  • http://www.facebook.com/profile.php?id=875830272 Joyce Chan

    How would I install python-devel pacakge for python 2.7?

  • Paul

    Very useful, thanks. I’d just like to note that the downloaded file is named ‘python27-2.7.2.spec’, while the rpmbuild command line specifies ‘python-2.7.2.spec’.

  • Guest

    When trying to install any RPM other than python-2.7 I get a failed dependency error for python(abi) = 2.7. I have 2.6, but not sure where to get the new version

  • http://profile.yahoo.com/6HOHQEOWC2ZUFYDKOBV5KN3CCY Raul

    You should review your spec file and modify

    %define libdirname lib

    with

    %define libdirname %(( uname -m | egrep -q ‘_64$’ && [ -d /usr/lib64 ] && echo lib64 ) || echo lib)

    If not, it will install always as a 32 bit platform and this causes problems when installing mod_wsgi on 64 bit systems
     

  • David Grant

    This is really useful – thanks.  I’ve written your instructions up in a puppet manifest here:

    https://github.com/seize-the-dave/python27-on-centos5-puppet