Red Hat Training
A Red Hat training course is available for RHEL 8
Chapter 40. Handling interpreter directives in Python scripts
In Red Hat Enterprise Linux 8, executable Python scripts are expected to use interpreter directives (also known as hashbangs or shebangs) that explicitly specify at a minimum the major Python version. For example:
#!/usr/bin/python3 #!/usr/bin/python3.6 #!/usr/bin/python3.8 #!/usr/bin/python3.9 #!/usr/bin/python3.11 #!/usr/bin/python2
The /usr/lib/rpm/redhat/brp-mangle-shebangs buildroot policy (BRP) script is run automatically when building any RPM package, and attempts to correct interpreter directives in all executable files.
The BRP script generates errors when encountering a Python script with an ambiguous interpreter directive, such as:
#!/usr/bin/python
or
#!/usr/bin/env python
40.1. Modifying interpreter directives in Python scripts
Modify interpreter directives in the Python scripts that cause the build errors at RPM build time.
Prerequisites
- Some of the interpreter directives in your Python scripts cause a build error.
Procedure
To modify interpreter directives, complete one of the following tasks:
Apply the
pathfix.pyscript from theplatform-python-develpackage:# pathfix.py -pn -i %{__python3} PATH …Note that multiple
PATHscan be specified. If aPATHis a directory,pathfix.pyrecursively scans for any Python scripts matching the pattern^[a-zA-Z0-9_]+\.py$, not only those with an ambiguous interpreter directive. Add this command to the%prepsection or at the end of the%installsection.-
Modify the packaged Python scripts so that they conform to the expected format. For this purpose,
pathfix.pycan be used outside the RPM build process, too. When runningpathfix.pyoutside an RPM build, replace%{__python3}from the example above with a path for the interpreter directive, such as/usr/bin/python3.
If the packaged Python scripts require a version other than Python 3.6, adjust the preceding commands to include the required version.
40.2. Changing /usr/bin/python3 interpreter directives in your custom packages
By default, interpreter directives in the form of /usr/bin/python3 are replaced with interpreter directives pointing to Python from the platform-python package, which is used for system tools with Red Hat Enterprise Linux. You can change the /usr/bin/python3 interpreter directives in your custom packages to point to a specific version of Python that you have installed from the AppStream repository.
Procedure
To build your package for a specific version of Python, add the
python*-rpm-macrossubpackage of the respectivepythonpackage to the BuildRequires section of the SPEC file. For example, for Python 3.6, include the following line:BuildRequires: python36-rpm-macros
As a result, the
/usr/bin/python3interpreter directives in your custom package are automatically converted to/usr/bin/python3.6.
To prevent the BRP script from checking and modifying interpreter directives, use the following RPM directive:
%undefine __brp_mangle_shebangs