Churchyard (talk | contribs) (current guidelines) |
Churchyard (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
== Manual byte compilation == | == Manual byte compilation == | ||
{{admon/warning|Fedora 29|The following guidelines has been changed in Fedora 29 and won't work in older Fedoras (or EPELs). See [https://fedoraproject.org/w/index.php?title=Packaging:Python_Appendix&oldid=419140#Manual_byte_compilation previous version of the guideline] the for older Fedoras.}} | |||
When byte compiling a .py file, python embeds a magic number in the byte compiled files that correspond to the runtime. Files in <code>%{python?_sitelib}</code> and <code>%{python?_sitearch}</code> must correspond to the runtime for which they were built. For instance, a pure python module compiled for the 3.4 runtime needs to be below <code>%{_usr}/lib/python3.4/site-packages</code> | When byte compiling a .py file, python embeds a magic number in the byte compiled files that correspond to the runtime. Files in <code>%{python?_sitelib}</code> and <code>%{python?_sitearch}</code> must correspond to the runtime for which they were built. For instance, a pure python module compiled for the 3.4 runtime needs to be below <code>%{_usr}/lib/python3.4/site-packages</code> | ||
Line 8: | Line 10: | ||
<li>what directory is the module installed in? If it's <code>/usr/lib{,64}/pythonX.Y</code>, then <code>pythonX.Y</code> is used to byte compile the module. If <code>pythonX.Y</code> is not installed, then an error is returned and the rpm build process will exit on an error so remember to <code>BuildRequire</code> the proper python package.</li> | <li>what directory is the module installed in? If it's <code>/usr/lib{,64}/pythonX.Y</code>, then <code>pythonX.Y</code> is used to byte compile the module. If <code>pythonX.Y</code> is not installed, then an error is returned and the rpm build process will exit on an error so remember to <code>BuildRequire</code> the proper python package.</li> | ||
<li>the script interpreter defined in <code>%{__python}</code> is used to compile the modules. This defaults | <li>by default the script interpreter defined in <code>%{__python}</code> is used to compile the modules outside of those directories. This defaults to <code>/usr/bin/python</code> (that's Python 2.7 on Fedora). This only happens for backwards compatibility reasons. | ||
</li> | </li> | ||
</ol> | </ol> | ||
If you have <code>*.py</code> files outside of the <code>/usr/lib(64)?/pythonX.Y/</code> directories, you '''MUST''' disable their automatic compilation, and compile them explicitly by the <code>%py_byte_compile</code> macro if and only if the bytecompilation is desired (i.e. it's an importable Python module). | |||
< | |||
% | |||
Example for package that has both Python versions: | |||
# Manually invoke the python byte compile macro for each path that needs byte | # Disable automatic compilation of Python files in extra directories | ||
# compilation. | %global _python_bytecompile_extra 0 | ||
%py_byte_compile %{__python2} %{buildroot}%{_datadir}/mypackage/foo | |||
%py_byte_compile %{__python3} %{buildroot}%{_datadir}/mypackage/bar | # Buildrequire both python2 and python3 | ||
BuildRequires: python2-devel python3-devel | |||
%install | |||
# Installs a python2 private module into %{buildroot}%{_datadir}/mypackage/foo | |||
# and installs a python3 private module into %{buildroot}%{_datadir}/mypackage/bar | |||
make install DESTDIR=%{buildroot} | |||
# Manually invoke the python byte compile macro for each path that needs byte | |||
# compilation. | |||
%py_byte_compile %{__python2} %{buildroot}%{_datadir}/mypackage/foo | |||
%py_byte_compile %{__python3} %{buildroot}%{_datadir}/mypackage/bar | |||
Note that this does not disable the compilation of files in <code>/usr/lib(64)?/pythonX.Y/</code>. | |||
The <code>%py_byte_compile</code> macro takes two arguments. The first is the python interpreter to use for byte compiling. The second is a file or directory to byte compile. If the second argument is a directory, the macro will recursively byte compile any *.py file in the directory. | The <code>%py_byte_compile</code> macro takes two arguments. The first is the python interpreter to use for byte compiling. The second is a file or directory to byte compile. If the second argument is a directory, the macro will recursively byte compile any *.py file in the directory. | ||
{{admon/warning|No %{} for py_byte_compile|RPM macros can only take arguments when they do not have curly braces around them. Therefore, py_byte_compile won't work correctly if you write: <code>%{py_byte_compile} %{__python2}</code>}} | {{admon/warning|No %{} for py_byte_compile|RPM macros can only take arguments when they do not have curly braces around them. Therefore, py_byte_compile won't work correctly if you write: <code>%{py_byte_compile} %{__python2}</code>}} |
Revision as of 09:57, 7 June 2018
Manual byte compilation
When byte compiling a .py file, python embeds a magic number in the byte compiled files that correspond to the runtime. Files in %{python?_sitelib}
and %{python?_sitearch}
must correspond to the runtime for which they were built. For instance, a pure python module compiled for the 3.4 runtime needs to be below %{_usr}/lib/python3.4/site-packages
The brp-python-bytecompile
script tries to figure this out for you. The script determines which interpreter to use when byte compiling the module by following these steps:
- what directory is the module installed in? If it's
/usr/lib{,64}/pythonX.Y
, thenpythonX.Y
is used to byte compile the module. IfpythonX.Y
is not installed, then an error is returned and the rpm build process will exit on an error so remember toBuildRequire
the proper python package. - by default the script interpreter defined in
%{__python}
is used to compile the modules outside of those directories. This defaults to/usr/bin/python
(that's Python 2.7 on Fedora). This only happens for backwards compatibility reasons.
If you have *.py
files outside of the /usr/lib(64)?/pythonX.Y/
directories, you MUST disable their automatic compilation, and compile them explicitly by the %py_byte_compile
macro if and only if the bytecompilation is desired (i.e. it's an importable Python module).
Example for package that has both Python versions:
# Disable automatic compilation of Python files in extra directories %global _python_bytecompile_extra 0 # Buildrequire both python2 and python3 BuildRequires: python2-devel python3-devel %install # Installs a python2 private module into %{buildroot}%{_datadir}/mypackage/foo # and installs a python3 private module into %{buildroot}%{_datadir}/mypackage/bar make install DESTDIR=%{buildroot} # Manually invoke the python byte compile macro for each path that needs byte # compilation. %py_byte_compile %{__python2} %{buildroot}%{_datadir}/mypackage/foo %py_byte_compile %{__python3} %{buildroot}%{_datadir}/mypackage/bar
Note that this does not disable the compilation of files in /usr/lib(64)?/pythonX.Y/
.
The %py_byte_compile
macro takes two arguments. The first is the python interpreter to use for byte compiling. The second is a file or directory to byte compile. If the second argument is a directory, the macro will recursively byte compile any *.py file in the directory.