Open-source News

Package a new Python module in 4 steps

opensource.com - Thu, 06/30/2022 - 15:00
Package a new Python module in 4 steps Sumantro Mukherjee Thu, 06/30/2022 - 03:00 Register or Login to like Register or Login to like

When you install an application, you're usually installing a package that contains the executable code for an application and important files such as documentation, icons, and so on. On Linux, applications are commonly packaged as RPM or DEB files, and users install them with the dnf or apt commands, depending on the Linux distribution. However, new Python modules are released virtually every day, so you could easily encounter a module that hasn't yet been packaged. And that's exactly why the pyp2rpm command exists.

Recently, I tried to install a module called python-concentration. It didn't go well:

$ sudo dnf install python-concentration
Updating Subscription Management repositories.
Last metadata expiration check: 1:23:32 ago on Sat 11 Jun 2022 06:37:25.
No match for argument: python-concentration
Error: Unable to find a match: python-concentration

It’s a PyPi package, but it's not yet available as an RPM package. The good news is that you can build an RPM yourself with a relatively simple process using pyp2rpm.

You'll need two directories to get started:

$ mkdir rpmbuild
$ cd rpmbuild && mkdir SPECS

You'll also need to install pyp2rpm:

$ sudo dnf install pyp2rpm

More Python resources What is an IDE? Cheat sheet: Python 3.7 for beginners Top Python GUI frameworks Download: 7 essential PyPI libraries Red Hat Developers Latest Python articles 1. Generate the spec file

The foundation of any RPM package is a file called the spec file. This file contains all the information about how to build the package, which dependencies it needs, the version of the application it provides, what files it installs, and more. When pointed to a Python module, pyp2rpm generates a spec file for it, which you can use to build an RPM.

Using python-concentration as an arbitrary example, here's how to generate a spec file:

$ pyp2rpm concentration > ~/rpmbuild/SPECS/concentration.spec

And here's the file it generates:

# Created by pyp2rpm-3.3.8
%global pypi_name concentration
%global pypi_version 1.1.5

Name:           python-%{pypi_name}
Version:        %{pypi_version}
Release:        1%{?dist}
Summary:        Get work done when you need to, goof off when you don't

License:        None
URL:            None
Source0:        %{pypi_source}
BuildArch:      noarch

BuildRequires:  python3-devel
BuildRequires:  python3dist(setuptools)

%description
Concentration [![PyPI version]( [![Test Status]( [![Lint Status]( [![codecov](

%package -n     python3-%{pypi_name}
Summary:        %{summary}
%{?python_provide:%python_provide python3-%{pypi_name}}

Requires:       (python3dist(hug) >= 2.6.1 with python3dist(hug) < 3~~)
Requires:       python3dist(setuptools)
%description -n python3-%{pypi_name}
Concentration [![PyPI version]( [![Test Status]( [![Lint Status]( [![codecov](


%prep
%autosetup -n %{pypi_name}-%{pypi_version}

%build
%py3_build

%install
%py3_install

%files -n python3-%{pypi_name}
%license LICENSE
%doc README.md
%{_bindir}/concentration
%{python3_sitelib}/%{pypi_name}
%{python3_sitelib}/%{pypi_name}-%{pypi_version}-py%{python3_version}.egg-info

%changelog
*  - 1.1.5-1
- Initial package.2. Run rpmlint

To ensure that the spec file is up to standards, run the rpmlint command on the file:

$ rpmlint ~/rpmbuild/SPEC/concentration.spec
error: bad date in %changelog: - 1.1.5-1
0 packages and 1 specfiles checked; 0 errors, 0 warnings.

It seems the changelog entry requires a date.

%changelog
* Sat Jun 11 2022 Tux <tux@example.com> - 1.1.5-1

Try rpmlint again:

$ rpmlint ~/rpmbuild/SPEC/concentration.spec
0 packages and 1 specfiles checked; 0 errors, 0 warnings.

Success!

3. Download the source code

To build an RPM package, you must download the code you're packaging up. The easy way to do this is to parse your spec file to find the source code's location on the Internet.

First, install the spectool command with dnf:

$ sudo dnf install spectool

Then use it to download the source code:

$ cd ~/rpmbuild
$ spectool -g -R SPEC/concentration.spec
Downloading: https://files.pythonhosted.org/...concentration-1.1.5.tar.gz
   6.0 KiB / 6.0 KiB    [=====================================]
Downloaded: concentration-1.1.5.tar.gz

This creates a SOURCES directory and places the source code archive into it.

4. Build the source package

Now you have a valid spec file, so it's time to build the source package with the rpmbuild command. If you don't have rpmbuild yet, install the rpm-build package with dnf (or accept your terminal's offer to install that package when you attempt to use the rpmbuild command).

$ cd ~/rpmbuild
$ spectool -g -R SPEC/concentration.spec
Downloading: https://files.pythonhosted.org/...concentration-1.1.5.tar.gz
   6.0 KiB / 6.0 KiB    [=====================================]
Downloaded: concentration-1.1.5.tar.gz

The -bs option stands for build source. This option gives you an src.rpm file, an all-purpose package that must be rebuilt for a specific architecture.

Build an installable RPM for your system:

$ rpmbuild –rebuild SRPMS/python-concentration-1.1.5-1.el9.src.rpm
error: Failed build dependencies:
        python3-devel is needed by python-concentration-1.1.5-1.el9.noarch

It looks like this package requires the development libraries of Python. Install them to continue with the build. This time the build succeeds and renders a lot more output (which I abbreviate here for clarity):

$ sudo dnf install python3-devel -y
$ rpmbuild –rebuild SRPMS/python-concentration-1.1.5-1.el9.src.rpm
[...]
Executing(--clean): /bin/sh -e /var/tmp/rpm-tmp.TYA7l2
+ umask 022
+ cd /home/bogus/rpmbuild/BUILD
+ rm -rf concentration-1.1.5
+ RPM_EC=0
++ jobs -p
+ exit 0

Your RPM package has been built in the RPMS subdirectory. Install it as usual with dnf:

$ sudo dnf install RPMS/noarch/python3-concentration*rpmWhy not just use PyPi?

It's not absolutely necessary to make a Python module into an RPM. Installing a module with PyPi is also acceptable, but PyPi adds another package manager to your personal list of things to check and update. When you install an RPM using dnf, you have a complete listing of what you've installed on your system. Thanks to pyp2rpm, the process is quick, easy, and automatable.

The pyp2rpm command makes it possible to create an RPM package and automate the process.

Image by:

WOCinTech Chat. Modified by Opensource.com. CC BY-SA 4.0

Python What to read next How to install pip to manage PyPI packages easily This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License. Register or Login to post a comment.

Harvard Business Review Analytics Services Report - Digital Transformation Refocused: New Goals Require New Strategies

Red Hat News - Thu, 06/30/2022 - 12:00

The Covid-19 pandemic has been a catalyst for many organizations in their digital transformations, leading companies to expedite technology initiatives, design dramatic changes to processes and reexamine culture and the role it plays in today’s new business world. As some organizations find success in their digital transformation strategies, they are able to provide additional bandwidth to their teams to focus on improving customer experience through the collection and analysis of data.

How to Install Fedora 36 XFCE Desktop Edition

Tecmint - Thu, 06/30/2022 - 11:50
The post How to Install Fedora 36 XFCE Desktop Edition first appeared on Tecmint: Linux Howtos, Tutorials & Guides .

Many Fedora users are unaware of the fact that you do get an option to choose other Desktop Environments apart from the default GNOME which we are used to downloading directly from their download

The post How to Install Fedora 36 XFCE Desktop Edition first appeared on Tecmint: Linux Howtos, Tutorials & Guides.

Fedora 37 Proposing To Allow Unrestricted Access To Flathub

Phoronix - Thu, 06/30/2022 - 02:45
To this point Fedora out-of-the-box has been restricted to a filtered subset of Flathub packages when enabled via GNOME Software or GNOME Initial Setup. However, legal has now cleared Fedora for allowing unfiltered/unrestricted access to Flathub, allowing a far greater selection of Flatpaks to become available on Fedora Linux with the plan for this to begin with Fedora 37...

Ubuntu Touch OTA-23 Released - Still Based On Ubuntu 16.04, Adds Support For FM Radios

Phoronix - Thu, 06/30/2022 - 01:53
Ubuntu Touch OTA-23 is out today as the newest Ubuntu mobile operating system update for smartphones from the folks at UBports that continues maintaining the code-base left by Canonical and now pushing ahead in their own direction...

Pages