Below is an example of an RPM .spec file for a Python project where the Python module is located in a subdirectory (e.g., python/) rather than the project root. This example demonstrates the use of modern Python RPM macros, including %pyproject_wheel, and handles the case of a non-standard project structure.
Assumptions:
- The project is named
my-python-project.
- The Python module is located in a
python/ subdirectory (e.g., python/my_python_project/).
- The project uses a
pyproject.toml file for configuration (located in the root directory).
- The project is built using modern Python build tools (e.g.,
build or hatchling).
Example .spec File:
# Define metadata
Name: python-my-python-project
Version: 1.0.0
Release: 1%{?dist}
Summary: A sample Python project with module in a subdirectory
License: MIT
URL: https://example.com/my-python-project
Source0: %{name}-%{version}.tar.gz
BuildArch: noarch
BuildRequires: python3-devel
BuildRequires: python3-rpm-macros
BuildRequires: python3-setuptools
BuildRequires: python3-pip
BuildRequires: python3-wheel
# Runtime dependencies (example, adjust as needed)
Requires: python3
# Define the Python package name (used for Provides/Obsoletes)
%global python_pkg_name my_python_project
%description
This is a sample Python project demonstrating how to package a Python module
located in a subdirectory using modern RPM macros and pyproject.toml.
%package -n python3-%{python_pkg_name}
Summary: Python 3 module for my-python-project
%{?python_provide:%python_provide python3-%{python_pkg_name}}
%description -n python3-%{python_pkg_name}
This package provides the Python 3 module for my-python-project, located in a
subdirectory of the source tree.
%prep
%autosetup -n %{name}-%{version}
%build
# Ensure the build system knows where the Python module is
# (e.g., by setting PYTHONPATH or adjusting pyproject.toml if needed)
export PYTHONPATH=$(pwd)/python:$PYTHONPATH
# Build the Python wheel using the pyproject.toml configuration
%pyproject_wheel
%install
# Install the wheel into the buildroot
%pyproject_install
# Ensure the installed files are correctly placed
# (e.g., site-packages, scripts, etc.)
%pyproject_save_files -l %{python_pkg_name}
%check
# Optional: Run tests if applicable (adjust as needed)
# Example using pytest, assuming tests are in python/tests/
%if 0%{?with_tests}
export PYTHONPATH=$(pwd)/python:$PYTHONPATH
%pytest python/tests/
%endif
%files -n python3-%{python_pkg_name} -f %{pyproject_files}
# No additional files need to be listed here, as %pyproject_save_files handles it
%changelog
* Wed Oct 25 2023 Your Name <your.email@example.com> - 1.0.0-1
- Initial package release
Explanation of Key Sections and Macros:
-
Metadata:
Name, Version, Release, Summary, etc., define the package metadata.
BuildArch: noarch is used because Python packages are typically architecture-independent.
BuildRequires includes dependencies needed to build the package, such as python3-devel, python3-rpm-macros, and tools like pip and wheel.
-
Python Package Naming:
- The
%global python_pkg_name variable defines the Python package name (e.g., my_python_project), which is used in the Provides and file listing.
- The subpackage
python3-%{python_pkg_name} follows Fedora/RHEL naming conventions for Python packages.
-
Prep Section:
%autosetup unpacks the source tarball (Source0) and sets up the working directory.
-
Build Section:
export PYTHONPATH=$(pwd)/python:$PYTHONPATH ensures the build system can find the Python module in the python/ subdirectory. This is necessary if the module is not in the root of the project.
%pyproject_wheel builds a Python wheel using the pyproject.toml configuration. This macro uses the modern Python build system (e.g., build).
-
Install Section:
%pyproject_install installs the wheel into the buildroot (%{buildroot}).
%pyproject_save_files -l %{python_pkg_name} generates a list of installed files for the package, ensuring only the relevant Python module files are included in the RPM. The -l flag lists the files for debugging purposes.
-
Check Section:
- The
%check section is optional and runs tests if applicable. In this example, it uses %pytest to run tests located in python/tests/.
- The
PYTHONPATH adjustment ensures the tests can find the module in the python/ subdirectory.
- The
%if 0%{?with_tests} conditional allows tests to be skipped if desired (e.g., during quick builds).
Notes on Project Structure:
To make this .spec file work, your project structure should look something like this:
my-python-project-1.0.0/
├── pyproject.toml
├── python/
│ ├── my_python_project/
│ │ ├── __init__.py
│ │ └── some_module.py
│ └── tests/
│ ├── __init__.py
│ └── test_some_module.py
└── README.md
Example pyproject.toml:
Here’s an example pyproject.toml that works with the above structure:
[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "my_python_project"
version = "1.0.0"
description = "A sample Python project with module in a subdirectory"
readme = "README.md"
requires-python = ">=3.6"
license = { text = "MIT" }
dependencies = []
[tool.setuptools]
package-dir = { "" = "python" }
- The
package-dir setting in [tool.setuptools] tells the build system to look for the Python module in the python/ subdirectory.
Building the RPM:
To build the RPM, you would:
- Create a source tarball (
my-python-project-1.0.0.tar.gz) containing the project files.
- Place the
.spec file and tarball in the appropriate RPM build directories (e.g., ~/rpmbuild/SOURCES/ and ~/rpmbuild/SPECS/).
- Run
rpmbuild -ba my-python-project.spec to build the RPM.
Additional Notes:
- If your project uses a different build backend (e.g.,
hatchling, flit), ensure the appropriate BuildRequires are included and adjust the pyproject.toml accordingly.
- The
%pyproject_* macros are part of the python3-rpm-macros package, which is available in Fedora, RHEL, and CentOS Stream. Ensure this package is installed on your build system.
- If your project has additional files (e.g., documentation, configuration files), you may need to list them explicitly in the
%files section.
This .spec file provides a solid foundation for packaging Python projects with non-standard directory structures using modern RPM macros.