Here's a sample RPM .spec file for a Python project where the Python module is located in a subdirectory called "python/":
%global pypi_name myproject
%global module_name mymodule
Name: python-%{pypi_name}
Version: 1.0.0
Release: 1%{?dist}
Summary: Sample Python project with module in subdirectory
License: MIT
URL: https://github.com/example/%{pypi_name}
Source0: %{pypi_source}
BuildArch: noarch
# Build dependencies
BuildRequires: python3-devel
BuildRequires: python3-pip
BuildRequires: python3-wheel
BuildRequires: pyproject-rpm-macros
# Runtime dependencies
%generate_buildrequires
%pyproject_buildrequires -t
%description
This is a sample Python project where the Python module is located in
a python/ subdirectory instead of the project root.
%package -n python3-%{pypi_name}
Summary: %{summary}
%{?python_provide:%python_provide python3-%{pypi_name}}
%description -n python3-%{pypi_name}
This is a sample Python project where the Python module is located in
a python/ subdirectory instead of the project root.
%prep
%autosetup -n %{pypi_name}-%{version}
# If pyproject.toml is not in the root, copy it there
if [ -f python/pyproject.toml ]; then
cp python/pyproject.toml .
fi
%build
# Change to python directory if needed
cd python
%pyproject_wheel
%install
%pyproject_install
# If there are any scripts or executables
%python_fix_shebang %{buildroot}%{_bindir}/*
# Remove tests if they were installed
%py3_delete_tests %{buildroot}%{python3_sitelib}/%{module_name}/
%files -n python3-%{pypi_name}
%license LICENSE
%doc README.md
%{python3_sitelib}/%{module_name}/
%{python3_sitelib}/%{module_name}-%{version}.dist-info/
# If your package includes executables, uncomment the following line
#%{_bindir}/*
%changelog
* Wed Jan 31 2024 Your Name <your.email@example.com> - 1.0.0-1
- Initial package version
This spec file assumes the following project structure:
myproject-1.0.0/
├── LICENSE
├── README.md
└── python/
├── mymodule/
│ ├── __init__.py
│ └── core.py
├── pyproject.toml
└── setup.py (optional)
And a sample pyproject.toml might look like:
[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "myproject"
version = "1.0.0"
description = "Sample Python project with module in subdirectory"
authors = [{name = "Your Name", email = "your.email@example.com"}]
license = {text = "MIT"}
requires-python = ">=3.6"
dependencies = [
"requests>=2.25.0",
]
[tool.setuptools]
package-dir = {"" = "."}
packages = ["mymodule"]
Key points about this spec file:
-
It uses modern Python packaging macros like %pyproject_wheel and %pyproject_install
-
It handles the case where the Python module is in a subdirectory by changing to that directory before building
-
It uses %generate_buildrequires and %pyproject_buildrequires to automatically generate build requirements
-
It includes proper handling of Python 3 packaging conventions
-
It includes common cleanup tasks like removing tests and fixing shebangs
-
It uses %python_provide to handle Python package naming conventions
To build the RPM:
rpmbuild -ba python-myproject.spec
Remember to adjust paths, dependencies, and other details according to your specific project needs. Also, make sure your pyproject.toml correctly specifies the package location and other build requirements.