|
| 1 | +# The uv build backend |
| 2 | + |
| 3 | +!!! note |
| 4 | + |
| 5 | + The uv build backend is currently in preview and may change without warning. |
| 6 | + |
| 7 | + When preview mode is not enabled, uv uses [hatchling](https://pypi.org/project/hatchling/) as the default build backend. |
| 8 | + |
| 9 | +A build backend transforms a source tree (i.e., a directory) into a source distribution or a wheel. |
| 10 | +While uv supports all build backends (as specified by PEP 517), it includes a `uv_build` backend |
| 11 | +that integrates tightly with uv to improve performance and user experience. |
| 12 | + |
| 13 | +The uv build backend currently only supports Python code. An alternative backend is required if you |
| 14 | +want to create a |
| 15 | +[library with extension modules](../concepts/projects/init.md#projects-with-extension-modules). |
| 16 | + |
| 17 | +To use the uv build backend as [build system](../concepts/projects/config.md#build-systems) in an |
| 18 | +existing project, add it to the `[build-system]` section in your `pyproject.toml`: |
| 19 | + |
| 20 | +```toml |
| 21 | +[build-system] |
| 22 | +requires = ["uv_build>=0.6.13,<0.7"] |
| 23 | +build-backend = "uv_build" |
| 24 | +``` |
| 25 | + |
| 26 | +!!! important |
| 27 | + |
| 28 | + The uv build backend follows the same [versioning policy](../reference/policies/versioning.md), |
| 29 | + setting an upper bound on the `uv_build` version ensures that the package continues to build in |
| 30 | + the future. |
| 31 | + |
| 32 | +You can also create a new project that uses the uv build backend with `uv init`: |
| 33 | + |
| 34 | +```shell |
| 35 | +uv init --build-backend uv |
| 36 | +``` |
| 37 | + |
| 38 | +`uv_build` is a separate package from uv, optimized for portability and small binary size. The `uv` |
| 39 | +command includes a copy of the build backend, so when running `uv build`, the same version will be |
| 40 | +used for the build backend as for the uv process. Other build frontends, such as `python -m build`, |
| 41 | +will choose the latest compatible `uv_build` version. |
| 42 | + |
| 43 | +## Include and exclude configuration |
| 44 | + |
| 45 | +To select which files to include in the source distribution, uv first adds the included files and |
| 46 | +directories, then removes the excluded files and directories. This means that exclusions always take |
| 47 | +precedence over inclusions. |
| 48 | + |
| 49 | +When building the source distribution, the following files and directories are included: |
| 50 | + |
| 51 | +- `pyproject.toml` |
| 52 | +- The module under `tool.uv.build-backend.module-root`, by default |
| 53 | + `src/<module-name or project_name_with_underscores>/**`. |
| 54 | +- `project.license-files` and `project.readme`. |
| 55 | +- All directories under `tool.uv.build-backend.data`. |
| 56 | +- All patterns from `tool.uv.build-backend.source-include`. |
| 57 | + |
| 58 | +From these, `tool.uv.build-backend.source-exclude` and the default excludes are removed. |
| 59 | + |
| 60 | +When building the wheel, the following files and directories are included: |
| 61 | + |
| 62 | +- The module under `tool.uv.build-backend.module-root`, by default |
| 63 | + `src/<module-name or project_name_with_underscores>/**`. |
| 64 | +- `project.license-files` and `project.readme`, as part of the project metadata. |
| 65 | +- Each directory under `tool.uv.build-backend.data`, as data directories. |
| 66 | + |
| 67 | +From these, `tool.uv.build-backend.source-exclude`, `tool.uv.build-backend.wheel-exclude` and the |
| 68 | +default excludes are removed. The source dist excludes are applied to avoid source tree to wheel |
| 69 | +source builds including more files than source tree to source distribution to wheel build. |
| 70 | + |
| 71 | +There are no specific wheel includes. There must only be one top level module, and all data files |
| 72 | +must either be under the module root or in the appropriate |
| 73 | +[data directory](../reference/settings.md#build-backend_data). Most packages store small data in the |
| 74 | +module root alongside the source code. |
| 75 | + |
| 76 | +## Include and exclude syntax |
| 77 | + |
| 78 | +Includes are anchored, which means that `pyproject.toml` includes only |
| 79 | +`<project root>/pyproject.toml`. For example, `assets/**/sample.csv` includes all `sample.csv` files |
| 80 | +in `<project root>/assets` or any child directory. To recursively include all files under a |
| 81 | +directory, use a `/**` suffix, e.g. `src/**`. |
| 82 | + |
| 83 | +!!! note |
| 84 | + |
| 85 | + For performance and reproducibility, avoid patterns without an anchor such as `**/sample.csv`. |
| 86 | + |
| 87 | +Excludes are not anchored, which means that `__pycache__` excludes all directories named |
| 88 | +`__pycache__` and its children anywhere. To anchor a directory, use a `/` prefix, e.g., `/dist` will |
| 89 | +exclude only `<project root>/dist`. |
| 90 | + |
| 91 | +All fields accepting patterns use the reduced portable glob syntax from |
| 92 | +[PEP 639](https://peps.python.org/pep-0639/#add-license-FILES-key). |
0 commit comments