Build a Python Version Agnostic RPM For Your Python Module

I often use the python setup.py bdist_rpm command on RHEL to build RPMs of the python modules I'm developing. This makes it easy for me to distribute my module via the yum repos internally at the company I work for. At my company, most of our production servers are currently RHEL 6, but we are starting to deploy RHEL 7. When I took one of my recently built RPMs of a python module (that runs under python 2.6 and python 2.7), I was surprised to find that it did not install on RHEL7 because of missing dependencies. It turns out, that my RPMs "required" python 2.6 from RHEL 6 (RHEL 7 moves to python 2.7 as default). <

1$ rpm -qpR python-mymodule-2.8-1.noarch.rpm
2/usr/bin/python
3python(abi) = 2.6
4python-requests
5rpmlib(CompressedFileNames) <= 3.0.4-1
6rpmlib(PayloadFilesHavePrefix) <= 4.0-1

Even though I don't have a specific version of python listed in the requirements for the package, the rpmbuild called by setuptools automatically adds a dependency on the python version installed on the system I'm using. This is because when rpmbuild scans through my source, it then finds the shebang that points to #!/usr/bin/env python, which in turn finds the python 3.6 on the server where I build the RPM. Obviously, I could rebuild the rpm on a RHEL 7 host and it would then find python 2.7, but I don't want to have separate RPMs for different RHEL versions when the code is the same. I wanted to be able to produce an RPM for my python module and code that installs and runs on RHEL 6 and RHEL 7 (when the python code is tested to run under python 2.6+) Now this isn't particularly well documented, but I did a little digging and found that to do this, you can simply add the --no-autoreq option to setup.py

1$ python setup.py bdist_rpm --no-autoreq

This will then tell bdist_rpm to add the "AutoReq: no" option to the spec file used to generate the RPM. RPM build will then see this and the resulting RPM will then no longer require a specific version of python.

1$ rpm -qpR python-mymodule-2.8-2.noarch.rpm
2python-requests
3rpmlib(CompressedFileNames) <= 3.0.4-1
4rpmlib(PayloadFilesHavePrefix) <= 4.0-1

You can then add the option to your setup.cfg file for the module, so that you don't forget it next time and inadvertently distribute a python version dependent module. <

1[bdist_rpm]
2no-autoreq = yes

Useful References: