APKBUILD examples:Python: Difference between revisions
(modernise) |
|||
Line 1: | Line 1: | ||
A lot of Python packages use the ''setuptools'' or ''distutils'' framework. This mean that the build() and the package() section looks a bit different compared to an application which uses ''make''. | A lot of Python packages use the ''setuptools'' or ''distutils'' framework. This mean that the build() and the package() section looks a bit different compared to an application which uses ''make''. | ||
== Considerations == | == Considerations == | ||
=== pkgname === | === pkgname === | ||
Package name for a Python ''library'' must be prefixed with | Package name for a Python ''library'' must be prefixed with ''py3-''. | ||
For an 'executable' (for example, <code>black</code>, <code>binwalk</code>), you generally don't need to prefix it. | |||
There’s no exact rule if the prefix should be used for tools and applications written in Python, it varies. | There’s no exact rule if the prefix should be used for tools and applications written in Python, it varies. | ||
Line 23: | Line 13: | ||
=== arch === | === arch === | ||
; noarch : Use for pure Python packages (i.e. without compiled code). Also add | ; noarch : Use for pure Python packages (i.e. without compiled code). Also add python3 to <tt>depends=</tt>. | ||
; all (and others) : Use for packages with native extensions (i.e. with compiled code). '''Do not''' add | ; all (and others) : Use for packages with native extensions (i.e. with compiled code). '''Do not''' add python3 to <tt>depends=</tt> (it's auto-detected via dynamic linking to python library). | ||
=== source === | === source === | ||
Most Python packages are published in [https://pypi.python.org/pypi PyPI] (the Python Package Index). | |||
If the package has a source tarball available in PyPI (that’s true for most packages), you should reference it in <tt>source=</tt> as: | If the package has a source tarball available in PyPI (that’s true for most packages), and it contains tests (some explicitly remove them from PyPI), you should reference it in <tt>source=</tt> as: | ||
<pre> | <pre> | ||
Line 35: | Line 25: | ||
</pre> | </pre> | ||
where <tt>_pyname</tt> is | where <tt>_pyname</tt> is the real name of the Python package. | ||
Otherwise, use the normal upstream git tarballs. | |||
== Examples == | == Examples == | ||
=== Package for setuptools/setup.py build === | |||
=== Package for | |||
If the project | If the project has a setup.py, then the APKBUILD is very simple: | ||
<pre> | <pre> | ||
Line 53: | Line 41: | ||
depends="python3" | depends="python3" | ||
makedepends="python3-dev" | makedepends="python3-dev" | ||
checkdepends="py3-pytest" | |||
... | ... | ||
build() { | build() { | ||
python3 setup.py build | python3 setup.py build | ||
} | } | ||
check() { | check() { | ||
pytest | |||
} | } | ||
package() { | package() { | ||
python3 setup.py install --skip-build --root="$pkgdir" | |||
python3 setup.py install -- | |||
} | } | ||
</pre> | </pre> | ||
Note that PyPI tarballs can contain a generated setup.py that does not exist in the upstream repo - this means you must use the pyproject.toml method if you change sources. | |||
=== | === Package for pyproject.toml build === | ||
<pre> | <pre> | ||
Line 81: | Line 65: | ||
_pyname="PyFoo" | _pyname="PyFoo" | ||
... | ... | ||
depends="python3 py3-bar py3-baz" | |||
makedepends="py3-build py3-installer python3-dev py3-wheel" | |||
makedepends=" | |||
... | ... | ||
build() { | build() { | ||
python3 -m build --no-isolation --wheel | |||
python3 | |||
} | } | ||
check() { | check() { | ||
pytest | |||
} | } | ||
package() { | package() { | ||
python3 -m installer -d "$pkgdir" \ | |||
dist/PyFoo-$pkgver-py3-none-any.whl | |||
} | } | ||
</pre> | </pre> | ||
Depending on the <code>build-backend</code> in the pyproject.toml, you might need py3-setuptools or py3-flit-core or py3-poetry-core or py3-hatchling at build time. | |||
Sometimes you might have to pass the path to the built package to run tests: | |||
<pre> | <pre> | ||
check() { | |||
python3 -m installer -d testenv \ | |||
dist/PyFoo-$pkgver-py3-none-any.whl | |||
local sitedir="$(python3 -c 'import site;print(site.getsitepackages()[0])')" | |||
PYTHONPATH="$PWD/testenv/$sitedir" pytest | |||
python3 | |||
local | |||
} | } | ||
</pre> | </pre> | ||
[[Category:Development]] [[Category:Python]] | [[Category:Development]] [[Category:Python]] |
Revision as of 12:39, 30 June 2022
A lot of Python packages use the setuptools or distutils framework. This mean that the build() and the package() section looks a bit different compared to an application which uses make.
Considerations
pkgname
Package name for a Python library must be prefixed with py3-.
For an 'executable' (for example, black
, binwalk
), you generally don't need to prefix it.
There’s no exact rule if the prefix should be used for tools and applications written in Python, it varies.
arch
- noarch
- Use for pure Python packages (i.e. without compiled code). Also add python3 to depends=.
- all (and others)
- Use for packages with native extensions (i.e. with compiled code). Do not add python3 to depends= (it's auto-detected via dynamic linking to python library).
source
Most Python packages are published in PyPI (the Python Package Index). If the package has a source tarball available in PyPI (that’s true for most packages), and it contains tests (some explicitly remove them from PyPI), you should reference it in source= as:
https://files.pythonhosted.org/packages/source/${_pyname%${_pyname#?}}/$_pyname/$_pyname-$pkgver.tar.gz
where _pyname is the real name of the Python package.
Otherwise, use the normal upstream git tarballs.
Examples
Package for setuptools/setup.py build
If the project has a setup.py, then the APKBUILD is very simple:
pkgname="py3-foo" _pyname="foo" ... depends="python3" makedepends="python3-dev" checkdepends="py3-pytest" ... build() { python3 setup.py build } check() { pytest } package() { python3 setup.py install --skip-build --root="$pkgdir" }
Note that PyPI tarballs can contain a generated setup.py that does not exist in the upstream repo - this means you must use the pyproject.toml method if you change sources.
Package for pyproject.toml build
pkgname="py-foo" _pyname="PyFoo" ... depends="python3 py3-bar py3-baz" makedepends="py3-build py3-installer python3-dev py3-wheel" ... build() { python3 -m build --no-isolation --wheel } check() { pytest } package() { python3 -m installer -d "$pkgdir" \ dist/PyFoo-$pkgver-py3-none-any.whl }
Depending on the build-backend
in the pyproject.toml, you might need py3-setuptools or py3-flit-core or py3-poetry-core or py3-hatchling at build time.
Sometimes you might have to pass the path to the built package to run tests:
check() { python3 -m installer -d testenv \ dist/PyFoo-$pkgver-py3-none-any.whl local sitedir="$(python3 -c 'import site;print(site.getsitepackages()[0])')" PYTHONPATH="$PWD/testenv/$sitedir" pytest }