https://fedoraproject.org/wiki/Changes/PythonMacroError

== Summary ==
The <code>%{__python}</code> RPM macro (currently defined to <code>/usr/bin/python</code> for backwards compatibility reasons) will be defined to raise an error when used. Any derived macros (<code>%{python}</code>, <code>%{python_version}</code>, <code>%{python_sitleib}</code> etc.) will propagate the error. Packagers can redefine the macro to any actual value to suppress the error. This is consistent with RHEL 8 behavior. Using  <code>/usr/bin/python</code> in Fedora packages remains forbidden.

== Owner ==
* Name: [[User:Churchyard|Miro Hrončok]]
* Email: <mhroncok@redhat.com>


== Detailed Description ==
For years, the unversioned <code>/usr/bin/python</code> Python interpreter MUST not be used when building RPM packages in Fedora. However, for backwards compatibility reasons, the <code>%{__python}</code> macro was defined to <code>/usr/bin/python</code>. As a direct consequence, all derived macros:

* <code>%{python}</code>
* <code>%{python_version}</code>
* <code>%{python_version_nodots}</code>
* <code>%{python_sitelib}</code>
* <code>%{python_sitearch}</code>
* <code>%py_shebang_fix</code>
* <code>%py_build</code> variants
* <code>%py_install</code> variants

used <code>/usr/bin/python</code> as well, unless redefined to custom value different than <code>/usr/bin/python</code>. Some of the macros unfortunately evaluated to empty string when <code>/usr/bin/python</code> was not installed in the buildroot.

We wanted to define <code>%{__python}</code> to an error previously, but unfortunately, this was not yet possible due to backwards compatibility wrt automagic byte-compilation. Hence we have done:

* [[Changes/No_more_automagic_Python_bytecompilation]]
* [[Changes/No_more_automagic_Python_bytecompilation_phase_2]]
* [[Changes/No_more_automagic_Python_bytecompilation_phase_3]]

Now, we can define the macro to an error by default. Packagers can still define it to any custom value.

We will define the macro as follows:

 %__python %{error:attempt to use unversioned python, define %%__python to %{__python2} or %{__python3} explicitly}

This is technically consistent with RHEL 8.

We will also define <code>%{python}</code> to <code>%{__python}</code> (we will drop the current Lua logic that is designed to prevent <code>%{python}</code> usage when <code>%{__python}</code> is <code>/usr/bin/python</code>).

The default behavior will be an error:

 $ rpm --eval '%__python'
 error: attempt to use unversioned python, define %__python to /usr/bin/python2 or /usr/bin/python3 explicitly
 
 $ rpm --eval '%python'
 error: attempt to use unversioned python, define %__python to /usr/bin/python2 or /usr/bin/python3 explicitly
 
 $ rpm --eval '%python_version'
 error: attempt to use unversioned python, define %__python to /usr/bin/python2 or /usr/bin/python3 explicitly
 
 $ rpm --eval '%python_sitelib'
 error: attempt to use unversioned python, define %__python to /usr/bin/python2 or /usr/bin/python3 explicitly

As advised, when redefined, the macros continue to work as currently:

 $ rpm --define '__python %__python3' --eval '%python'
 /usr/bin/python3
 
 $ rpm --define '__python %__python3' --eval '%python_version'
 3.9

Despite the error message not actually promoting this, packagers can even explicitly define the macro to <code>/usr/bin/python</code> to mimic the previous behavior. However, this remains forbidden in Fedora.

 $ rpm --define '__python /usr/bin/python' --eval '%python'
 /usr/bin/python
 
 $ rpm --define '__python /usr/bin/python' --eval '%python_sitelib'
 /usr/lib/python3.9/site-packages

== Feedback ==
* More consistent behavior between RHEL and Fedora.
* Avoids hard to debug mistakes when <code>/usr/bin/python</code> is not present and macros like <code>%{python_sitelib}</code> are used.
* Doing the wrong thing is not the easiest default any more.

== Scope ==
* Proposal owners:
** Redefine <code>%__python</code> and <code>%python</code>
* Other developers: nothing, AFAIK packages in Fedora already dropped this construct, however when not, packagers will need to define <code>%__python</code> in spec to make it work. We believe the error message is self-explanatory.
* Release engineering: no impact
* Policies and guidelines: Mostly already exist. The [https://docs.fedoraproject.org/en-US/packaging-guidelines/Python/#_macros macro definition] will need to be updated in the Python guidelines to match reality.
* Trademark approval: not needed


== Upgrade/compatibility impact ==
No user impact. Some spec files might start to fail to build with this change, but the error is self-explanatory.

== How To Test ==
See examples in Detailed Description.

== User Experience ==
No user impact. Some spec files might start to fail to build with this change, but the error is self-explanatory.

== Dependencies ==
[[Changes/No_more_automagic_Python_bytecompilation_phase_3]]

== Contingency Plan ==
* Contingency mechanism: the change owners can revert the changes
* Contingency deadline: beta freeze
* Blocks release? No

== Documentation ==
This page is the documentation. The updated [https://docs.fedoraproject.org/en-US/packaging-guidelines/Python/#_macros macro list] will also serve as documentation.

--
Ben Cotton
He / Him / His
Senior Program Manager, Fedora & CentOS Stream
Red Hat
TZ=America/Indiana/Indianapolis