I want to package a python application which can run on both python2 and python3 for we have still old systems running python2.

the default should be python3 but in case of python3 is not installed. I would like to build it for python2. without changing the name:

i have followed the instruction here: https://docs.fedoraproject.org/en-US/packaging-guidelines/Python_Appendix/#_conditionalizing_the_python_2_parts

and I tried this:

%global srcname example

# Disable python2 by default
%bcond_with python2

Name:           python-%{srcname}
Version:        1.2.3
Release:        1%{?dist}
Summary:        An example python module

License:        MIT
URL:            https://pypi.python.org/pypi/%{srcname}
Source0:        %pypi_source

BuildArch:      noarch

%global _description %{expand:
A python module which provides a convenient example.}

%description %_description

%if %{with python2}
%package -n python2-%{srcname}
Summary:        %{summary}
BuildRequires:  python2-devel

%description -n python2-%{srcname} %_description

%package -n python3-%{srcname}
Summary:        %{summary}
BuildRequires:  python3-devel

%description -n python3-%{srcname} %_description

%autosetup -n %{srcname}-%{version}

%if %{with python2}

# Must do the python2 install first because the scripts in /usr/bin are
# overwritten with every setup.py install, and in general we want the
# python3 version to be the default.
%if %{with python2}

%if %{with python2}
%{python2} setup.py test
%{python3} setup.py test

%if %{with python2}
%files -n python2-%{srcname}
%license COPYING
%doc README.rst

%files -n python3-%{srcname}
%license COPYING
%doc README.rst


but It always tries to build both which fails furthermore i need to change the package name eg python2-%{srcname}. I need them to have the same names it is not a python module it is a standalone program.

  • 3
    Why not just build two packages? a legacy "packagename-python2" package for python2 and the other just "packagename" for python3 (or whatever's the appropriate naming convention for python packages on Fedora). It's a bad idea to have two packages with exactly the same name but with very different contents and/or dependencies.
    – cas
    Nov 30, 2021 at 14:16
  • I just need to build for python2 in rhel7 and for python3 in redhat8. I just don't know how to properly use the %bcond_with/without %if 0%{?rhel} && 0%{?rhel} <= 7 %bcond_with python2 %else %bcond_without python3 %endif I tried this but how to configure it?
    – danidar
    Dec 6, 2021 at 14:16

1 Answer 1


There are two different approaches

The first you've sort of outlined, but it requires forethought. Namely doing something like

%if 0%{?rhel_version} < 800
Requires: policycoreutils-python
Requires: policycoreutils-python-utils

Where in you build 1 RPM for RHEL-7.X and one for RHEL-8.X, at build time you'll pass in a version variable to rpmbuild, during the build process it'll perform templating to determine which of these Requires statements is written in the RPM.

This means you'll have 2 RPMs and those 2 RPMs will have to be hosted seperately.

The other approach is to use Boolean Dependencies RPM (link) RHEL (link), this feature isn't "new" (it was released years ago, CentOS/RHEL-7 platforms should support it).

This works like (we're going to pretend to target AMAZON Linux 2023 because its python-unversioned-command package makes this a lot easier than on RHEL).

Requires: ( Py3-Dep or Py2-Dep )
Requires: ( ( python-unversioned-command >= 3.0.0 and Py3-Dep ) or ( python-unversioned-command < 3.0.0 and Py2-Dep ) )

This in-essence spells out that you can't have both Py3-Dep and Py2-Dep installed together and that if some package is >= another package.

This brings a separate set of complications has you'll probably need to find a single package to build this conditional around.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .