Skip to content

Add: text on Hatch backend compatibility #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions package-structure-code/complex-python-package-builds.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,40 @@ In the future, we want to provide resources for packaging workflows that require
## Pure Python Packages vs. packages with extensions in other languages

You can classify Python package complexity into three general categories. These
categories can in turn help you select the correct package front-end and
back-end tools.
categories can in turn help you select the correct package frontend and
backend tools.

1. **Pure-python packages:** these are packages that only rely on Python to function. Building a pure Python package is simpler. As such, you can chose a tool below that has the features that you want and be done with your decision!

2. **Python packages with non-Python extensions:** These packages have additional components called extensions written in other languages (such as C or C++). If you have a package with non-python extensions, then you need to select a build back-end tool that allows you to add additional build steps needed to compile your extension code. Further, if you wish to use a front-end tool to support your workflow, you will need to select a tool that supports additional build setups. In this case, you could use setuptools. However, we suggest that you chose build tool that supports custom build steps such as Hatch with Hatchling or PDM. PDM is an excellent choice as it allows you to also select your build back-end of choice. We will discuss this at a high level on the complex builds page.
2. **Python packages with non-Python extensions:** These packages have additional components called extensions written in other languages (such as C or C++). If you have a package with non-Python extensions, then you need to select a build backend tool that allows additional build steps needed to compile your extension code. Further, if you wish to use a frontend tool to support your workflow, you will need to select a tool that supports additional build setups. We suggest that you chose build tool that supports custom build steps like Hatch.

3. **Python packages that have extensions written in different languages (e.g. Fortran and C++) or that have non Python dependencies that are difficult to install (e.g. GDAL)** These packages often have complex build steps (more complex than a package with just a few C extensions for instance). As such, these packages require tools such as [scikit-build](https://scikit-build.readthedocs.io/en/latest/)
or [meson-python](https://mesonbuild.com/Python-module.html) to build. NOTE: you can use meson-python with PDM.

## Mixing frontend and backend projects

It is sometimes necessary or desirable to use a build frontend with an alternative build-backend.
This is because some frontends do not have a default backend (`build`), and this choice is placed on the maintainer.
Other backends (`hatch`) have a preferred backend (`hatchling`) but allow the maintainer to migrate to another, while
some backends (`poetry`) only work with a single backend (`poetry-core`). Refer to (#python-package-build-tools) for
more information about frontend and backend compatibility.

In this packaging guide we recommend using `hatch` along with its preferred backend `hatchling`. While this will be
suitable for most packages, an alternate backend may be used with Hatch if needed when creating an extension module.
A Python extension module is one that is made up, either in part or entirely, of compiled code. In this case the
backend chosen (such as `meson-python`) must know how to compile the extension language and bind it to Python.
`hatchling` does not know how to do this all on its own and must either make use of
[plugins](https://hatch.pypa.io/1.9/plugins/about/) or be replaced by a backend that is already capable of building
extension modules.

In order to use a different backend you will need to edit your project's `pyproject.toml`. If you have a
`pyproject.toml` generated by the `hatch` command, or from following the packaging tutorial, you may have
to make a change like this

```diff
[build-system]
-requires = ["hatchling"]
+requires = ["meson-python"]
-build-backend = "hatchling.build"
+build-backend = "mesonpy"
```
4 changes: 2 additions & 2 deletions package-structure-code/python-package-build-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ as building your documentation locally. This means that you could potentially dr
:widths: 20,5,50
:delim: "|"

Use Other Build Backends|✅ | Switching out build back-ends is not currently an option with Hatch. However, this feature is planned for a future release.
Use Other Build Backends|✅ | Hatch is used with the backend Hatchling by default, but allows you to use another backend by switching the declaration in pyproject.toml.
Dependency management|✖| Currently you have to add dependencies manually with Hatch. However a feature to support dependencies management may be added in a future release.
Environment Management |✅ | Hatch supports Python virtual environments. If you wish to use other types of environments such as Conda, you will need to [install a plugin such as hatch-conda for conda support](https://github.com/OldGrumpyViking/hatch-conda).
Publish to PyPI and test PyPI|✅|Hatch supports publishing to both test PyPI and PyPI
Expand All @@ -355,7 +355,7 @@ Install your package in editable mode|✅| Hatch will install your package into
Build your sdist and wheel distributions|✅| Hatch will build the sdist and wheel distributions
✨Matrix environment creation to support testing across Python versions✨|✅| The matrix environment creation is a feature that is unique to Hatch in the packaging ecosystem. This feature is useful if you wish to test your package locally across Python versions (instead of using a tool such as tox).
✨[Nox / MAKEFILE like functionality](https://hatch.pypa.io/latest/environment/#selection)✨| ✅| This feature is also unique to Hatch. This functionality allows you to create workflows in the **pyproject.toml** configuration to do things like serve docs locally and clean your package build directory. This means you may have one less tool in your build workflow.
✨A flexible build back-end: **hatchling**✨| ✅| **The hatchling build back-end offered by the maintainer of Hatch allows developers to easily build plugins to support custom build steps when packaging.
✨A flexible build backend: **hatchling**✨| ✅| **The hatchling build backend offered by the maintainer of Hatch allows developers to easily build plugins to support custom build steps when packaging.

```

Expand Down
2 changes: 1 addition & 1 deletion package-structure-code/python-package-versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ your package version.
To use **hatch_vcs** you will need to use the **hatchling** build back end.

```{tip}
Hatchling can also be used any of the modern build tools
Hatchling can also be used with any of the modern build tools
including **Flit** and **PDM** if you prefer those for your day to
day workflow.
```
Expand Down
Loading