From Fedora Project Wiki

Revision as of 14:49, 23 January 2018 by Churchyard (talk | contribs) (proposed changes to macros)

Macros

The following macros are defined for you:

Macro Normal Definition Notes
__python /usr/bin/python Prohibited (see the note bellow this macro list)
__python2 /usr/bin/python2 Python 2 interpreter
__python3 /usr/bin/python3 Python 3 interpreter
python_provide (Lua script) Given a package name, evaluates to either Provides: python-example or nothing at all depending on the version of the system runtime. See here for an example.
py2_dist (Lua script) Introduced in Fedora 25. Given a standardized name (i.e. dist name, name on PyPI) of Python software, it will convert it to a canonical format, and evaluates to python2dist(CANONICAL_NAME), which is useful when listing dependencies. See above for more information.
python2_sitelib /usr/lib/python2.X/site-packages Where pure python2 modules are installed
python2_sitearch /usr/lib64/python2.X/site-packages on x86_64
/usr/lib/python2.X/site-packages on x86
Where python2 extension modules that are compiled C are installed
python3_sitelib /usr/lib/python3.X/site-packages Where pure python3 modules are installed
python3_sitearch /usr/lib64/python3.X/site-packages on x86_64
/usr/lib/python3.X/site-packages on x86
Where python3 extension modules that are compiled C are installed
py3_dist (Lua script) Introduced in Fedora 25. Given a standardized name (i.e. dist name, name on PyPI) of Python software, it will convert it to a canonical format, and evaluates to python3dist(CANONICAL_NAME), which is useful when listing dependencies. See above for more information.
py_byte_compile (script) Defined in python3-devel. See the byte compiling section for usage
python3_version 3.X Defined in python3-devel. Useful when running programs with Python version in filename, such as nosetest-%{python3_version}
python3_version_nodots 3X Defined in python3-devel since Fedora 21 / Python 3.4. Useful when listing files explicitly in %files section , such as %{python3_sitelib}/foo/*.cpython-%{python3_version_nodots}.pyo
py_dist_name (Lua script) Introduced in Fedora 25. Given a standardized name (i.e. dist name, name on PyPI) of Python software, it will convert it to a canonical format. See above for more information.
The generic %{_python} macros
The unversioned macros, %{__python}, %{python_sitelib}, and %{python_sitearch} are generic macros that point to or use /usr/bin/python. Since /usr/bin/python invocation is prohibited and deprecated in RPM build, you shall never use those macros unless you manually set %{__python} to either %{__python2} or %{__python3}. You should use %{__python2}, %{python2_sitelib}, and %{python2_sitearch} to explicitly reference the python2 interpreter instead.

During %install or when listing %files you can use the python2_sitearch and python2_sitelib macros to specify where the installed modules are to be found. For instance:

%files
# A pure python2 module
%{python2_sitelib}/foomodule/
# A compiled python2 extension module
%{python2_sitearch}/barmodule/
# A compiled python3 extension module
%{python3_sitearch}/bazmodule/

Use of the macros has several benefits:

  • It ensures that the packages are installed correctly on multilib architectures.
  • Using these macros instead of hardcoding the directory in the specfile ensures your spec remains compatible with the installed python version even if the directory structure changes radically (for instance, if python2_sitelib moves into %{_datadir}).