Chapter 4. Handling interpreter directives in Python scripts
In Red Hat Enterprise Linux 9, 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.9 #!/usr/bin/python3.11 #!/usr/bin/python3.12
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
4.1. Modifying interpreter directives in Python scripts
Use the following procedure to modify interpreter directives in Python scripts that cause 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:
Use the following macro in the
%prep
section of your SPEC file:# %py3_shebang_fix SCRIPTNAME …
SCRIPTNAME can be any file, directory, or a list of files and directories.
As a result, all listed files and all
.py
files in listed directories will have their interpreter directives modified to point to%{python3}
. Existing flags from the original interpreter directive will be preserved and additional flags defined in the%{py3_shebang_flags}
macro will be added. You can redefine the%{py3_shebang_flags}
macro in your SPEC file to change the flags that will be added.Apply the
pathfix.py
script from thepython3-devel
package:# pathfix.py -pn -i %{python3} PATH …
You can specify multiple paths. If a
PATH
is a directory,pathfix.py
recursively scans for any Python scripts matching the pattern^[a-zA-Z0-9_]+\.py$
, not only those with an ambiguous interpreter directive. Add the command above to the%prep
section or at the end of the%install
section.-
Modify the packaged Python scripts so that they conform to the expected format. For this purpose, you can use the
pathfix.py
script outside the RPM build process, too. When runningpathfix.py
outside an RPM build, replace%{python3}
from the preceding example with a path for the interpreter directive, such as/usr/bin/python3
or/usr/bin/python3.11
.
Additional resources