| 1 |
################### |
| 2 |
API Documentation |
| 3 |
################### |
| 4 |
|
| 5 |
build provides a Python API for programmatic use in build tools, CI systems, and automation scripts. |
| 6 |
|
| 7 |
.. note:: |
| 8 |
|
| 9 |
When not using an isolated environment, the build backend must be installed in the current environment. When using |
| 10 |
:class:`DefaultIsolatedEnv <build.env.DefaultIsolatedEnv>`, you must explicitly install build dependencies into the |
| 11 |
isolated environment via :meth:`~build.env.DefaultIsolatedEnv.install` before calling |
| 12 |
:meth:`~build.ProjectBuilder.build` — the environment is created empty. |
| 13 |
|
| 14 |
************* |
| 15 |
Basic Usage |
| 16 |
************* |
| 17 |
|
| 18 |
Building a package programmatically: |
| 19 |
|
| 20 |
.. code-block:: python |
| 21 |
|
| 22 |
from build import ProjectBuilder |
| 23 |
|
| 24 |
builder = ProjectBuilder("path/to/project") |
| 25 |
builder.build("wheel", "dist/") |
| 26 |
|
| 27 |
Building both sdist and wheel: |
| 28 |
|
| 29 |
.. code-block:: python |
| 30 |
|
| 31 |
from build import ProjectBuilder |
| 32 |
|
| 33 |
builder = ProjectBuilder(".") |
| 34 |
builder.build("sdist", "dist/") |
| 35 |
builder.build("wheel", "dist/") |
| 36 |
|
| 37 |
Using isolated environments (default): |
| 38 |
|
| 39 |
``DefaultIsolatedEnv`` creates a clean virtual environment, but it is initially empty. You must install the project's |
| 40 |
build dependencies before invoking the backend: |
| 41 |
|
| 42 |
.. code-block:: python |
| 43 |
|
| 44 |
from build import ProjectBuilder |
| 45 |
from build.env import DefaultIsolatedEnv |
| 46 |
|
| 47 |
with DefaultIsolatedEnv() as env: |
| 48 |
builder = ProjectBuilder.from_isolated_env(env, ".") |
| 49 |
# Install build-system.requires (e.g., flit-core, setuptools, ...) |
| 50 |
env.install(builder.build_system_requires) |
| 51 |
# Install additional backend dependencies for the target distribution |
| 52 |
env.install(builder.get_requires_for_build("wheel")) |
| 53 |
builder.build("wheel", "dist/") |
| 54 |
|
| 55 |
Pass ``path`` to create the environment at a fixed location instead of a temporary directory. The location must be |
| 56 |
empty; ``DefaultIsolatedEnv`` removes it on a clean exit and keeps it when the block raises: |
| 57 |
|
| 58 |
.. code-block:: python |
| 59 |
|
| 60 |
with DefaultIsolatedEnv(path=".build-env") as env: |
| 61 |
builder = ProjectBuilder.from_isolated_env(env, ".") |
| 62 |
env.install(builder.build_system_requires) |
| 63 |
builder.build("wheel", "dist/") |
| 64 |
|
| 65 |
Disabling isolation: |
| 66 |
|
| 67 |
.. code-block:: python |
| 68 |
|
| 69 |
from build import ProjectBuilder |
| 70 |
|
| 71 |
builder = ProjectBuilder(".", runner=lambda cmd, **kwargs: None) |
| 72 |
builder.build("wheel", "dist/") |
| 73 |
|
| 74 |
Getting package metadata without building: |
| 75 |
|
| 76 |
.. code-block:: python |
| 77 |
|
| 78 |
from build import ProjectBuilder |
| 79 |
import tempfile |
| 80 |
|
| 81 |
builder = ProjectBuilder(".") |
| 82 |
with tempfile.TemporaryDirectory() as tmpdir: |
| 83 |
metadata_dir = builder.metadata_path(tmpdir) |
| 84 |
# Read METADATA file from metadata_dir to extract package info |
| 85 |
|
| 86 |
Accessing build dependencies: |
| 87 |
|
| 88 |
.. code-block:: python |
| 89 |
|
| 90 |
from build import ProjectBuilder |
| 91 |
|
| 92 |
builder = ProjectBuilder(".") |
| 93 |
requires = builder.build_system_requires |
| 94 |
print(f"Build requires: {requires}") |
| 95 |
|
| 96 |
# Get additional dependencies for building a wheel |
| 97 |
wheel_requires = builder.get_requires_for_build("wheel") |
| 98 |
print(f"Wheel build requires: {wheel_requires}") |
| 99 |
|
| 100 |
Handling errors: |
| 101 |
|
| 102 |
.. code-block:: python |
| 103 |
|
| 104 |
from build import ProjectBuilder |
| 105 |
from build import BuildException |
| 106 |
|
| 107 |
try: |
| 108 |
builder = ProjectBuilder(".") |
| 109 |
builder.build("wheel", "dist/") |
| 110 |
except BuildException as e: |
| 111 |
print(f"Build failed: {e}") |
| 112 |
|
| 113 |
*********** |
| 114 |
Reference |
| 115 |
*********** |
| 116 |
|
| 117 |
****************** |
| 118 |
``build`` module |
| 119 |
****************** |
| 120 |
|
| 121 |
.. automodule:: build |
| 122 |
:members: |
| 123 |
:undoc-members: |
| 124 |
:show-inheritance: |
| 125 |
|
| 126 |
********************** |
| 127 |
``build.env`` module |
| 128 |
********************** |
| 129 |
|
| 130 |
.. automodule:: build.env |
| 131 |
:members: |
| 132 |
:undoc-members: |
| 133 |
:show-inheritance: |
| 134 |
|
| 135 |
*********************** |
| 136 |
``build.util`` module |
| 137 |
*********************** |
| 138 |
|
| 139 |
.. automodule:: build.util |
| 140 |
:members: |
| 141 |
:undoc-members: |
| 142 |
:show-inheritance: |