| 1 |
############# |
| 2 |
Basic Usage |
| 3 |
############# |
| 4 |
|
| 5 |
This guide covers the most common build commands and workflows. |
| 6 |
|
| 7 |
******************************************************** |
| 8 |
Building both source and wheel distributions (default) |
| 9 |
******************************************************** |
| 10 |
|
| 11 |
By default, build creates both a `source distribution |
| 12 |
<https://packaging.python.org/en/latest/specifications/source-distribution-format/>`_ (often called "sdist") and a |
| 13 |
`wheel <https://packaging.python.org/en/latest/specifications/binary-distribution-format/>`_: |
| 14 |
|
| 15 |
.. code-block:: console |
| 16 |
|
| 17 |
$ python -m build |
| 18 |
|
| 19 |
The process: |
| 20 |
|
| 21 |
1. Builds the source distribution from your source code |
| 22 |
2. Extracts the source distribution to a temporary directory |
| 23 |
3. Builds the wheel from the extracted source distribution |
| 24 |
|
| 25 |
This two-step process ensures your source distribution contains all necessary files to build your package. If files are |
| 26 |
missing, the wheel build will fail, alerting you to the problem. |
| 27 |
|
| 28 |
This is different from ``python -m build --sdist --wheel``, which builds both directly from source. The default behavior |
| 29 |
is preferable because it verifies your sdist is complete. |
| 30 |
|
| 31 |
*********************** |
| 32 |
Building only a wheel |
| 33 |
*********************** |
| 34 |
|
| 35 |
.. code-block:: console |
| 36 |
|
| 37 |
$ python -m build --wheel |
| 38 |
|
| 39 |
Or the short form: |
| 40 |
|
| 41 |
.. code-block:: console |
| 42 |
|
| 43 |
$ python -m build -w |
| 44 |
|
| 45 |
This builds the `wheel <https://packaging.python.org/en/latest/specifications/binary-distribution-format/>`_ |
| 46 |
(installable package) directly from your source directory, skipping the source distribution step. This is faster but |
| 47 |
doesn't verify your source distribution is complete. |
| 48 |
|
| 49 |
************************************* |
| 50 |
Building only a source distribution |
| 51 |
************************************* |
| 52 |
|
| 53 |
.. code-block:: console |
| 54 |
|
| 55 |
$ python -m build --sdist |
| 56 |
|
| 57 |
Or the short form: |
| 58 |
|
| 59 |
.. code-block:: console |
| 60 |
|
| 61 |
$ python -m build -s |
| 62 |
|
| 63 |
********************************* |
| 64 |
Specifying the source directory |
| 65 |
********************************* |
| 66 |
|
| 67 |
Build defaults to the current directory. To build from a different location: |
| 68 |
|
| 69 |
.. code-block:: console |
| 70 |
|
| 71 |
$ python -m build path/to/project |
| 72 |
|
| 73 |
Or explicitly: |
| 74 |
|
| 75 |
.. code-block:: console |
| 76 |
|
| 77 |
$ python -m build --srcdir path/to/project |
| 78 |
|
| 79 |
************************************* |
| 80 |
Building from a source distribution |
| 81 |
************************************* |
| 82 |
|
| 83 |
The positional argument also accepts a ``.tar.gz`` `source distribution |
| 84 |
<https://packaging.python.org/en/latest/specifications/source-distribution-format/>`_. Build checks the filename and the |
| 85 |
presence of ``PKG-INFO``, extracts the archive into a temporary directory, and runs the wheel build against the |
| 86 |
extracted source. The wheel lands next to the input archive. |
| 87 |
|
| 88 |
.. code-block:: console |
| 89 |
|
| 90 |
$ python -m build dist/mypackage-1.0.0.tar.gz |
| 91 |
|
| 92 |
No flag means the same as ``--wheel``. ``--metadata`` works the same way. ``--sdist`` errors out, since the archive |
| 93 |
already is an sdist. |
| 94 |
|
| 95 |
********************************* |
| 96 |
Specifying the output directory |
| 97 |
********************************* |
| 98 |
|
| 99 |
By default, distributions are placed in ``dist/`` within the source directory. To use a different location: |
| 100 |
|
| 101 |
.. code-block:: console |
| 102 |
|
| 103 |
$ python -m build --outdir /path/to/output |
| 104 |
|
| 105 |
Or the short form: |
| 106 |
|
| 107 |
.. code-block:: console |
| 108 |
|
| 109 |
$ python -m build -o /path/to/output |
| 110 |
|
| 111 |
******************************************* |
| 112 |
Speeding up rebuilds of compiled packages |
| 113 |
******************************************* |
| 114 |
|
| 115 |
When the default build extracts the intermediate sdist, it uses a random temporary directory. Compiler caches such as |
| 116 |
`ccache <https://ccache.dev/>`_ and `sccache <https://github.com/mozilla/sccache>`_ fold the absolute source path into |
| 117 |
their cache key, so a fresh path on every run means every C/C++ file is recompiled from scratch. |
| 118 |
|
| 119 |
Pin the extraction path with ``--sdist-extract-dir`` so the cache can recognise unchanged files across rebuilds: |
| 120 |
|
| 121 |
.. code-block:: console |
| 122 |
|
| 123 |
$ python -m build --sdist-extract-dir build/sdist |
| 124 |
|
| 125 |
Build creates the directory if missing, clears and repopulates it each run so the contents stay fresh, and keeps it |
| 126 |
after the build. The path stays stable while the project version is unchanged, as it is during iteration on the |
| 127 |
extension sources. The same flag applies when building a wheel from a ``.tar.gz``. |
| 128 |
|
| 129 |
Build still runs in a fresh isolated environment whose path is *not* pinned, so include directories from that |
| 130 |
environment (Python headers, dependency headers) keep changing. Caching those compile units needs toolchain-side path |
| 131 |
remapping on top of this flag (e.g. ``-fdebug-prefix-map``, or sccache's ``base_dir``). |
| 132 |
|
| 133 |
*********************** |
| 134 |
Controlling verbosity |
| 135 |
*********************** |
| 136 |
|
| 137 |
Increase verbosity to see more details: |
| 138 |
|
| 139 |
.. code-block:: console |
| 140 |
|
| 141 |
$ python -m build -v |
| 142 |
|
| 143 |
Or even more verbose: |
| 144 |
|
| 145 |
.. code-block:: console |
| 146 |
|
| 147 |
$ python -m build -vv |
| 148 |
|
| 149 |
Decrease verbosity for quieter output: |
| 150 |
|
| 151 |
.. code-block:: console |
| 152 |
|
| 153 |
$ python -m build -q |
| 154 |
|
| 155 |
************************** |
| 156 |
Using a faster installer |
| 157 |
************************** |
| 158 |
|
| 159 |
By default, build uses `pip <https://pip.pypa.io/>`_ to install build dependencies. For faster builds, use `uv |
| 160 |
<https://docs.astral.sh/uv/>`_ (a faster alternative to pip): |
| 161 |
|
| 162 |
.. code-block:: console |
| 163 |
|
| 164 |
$ python -m build --installer=uv |
| 165 |
|
| 166 |
This requires uv to be installed: |
| 167 |
|
| 168 |
.. code-block:: console |
| 169 |
|
| 170 |
$ pip install uv |
| 171 |
|
| 172 |
**************************** |
| 173 |
Building without isolation |
| 174 |
**************************** |
| 175 |
|
| 176 |
By default, build creates an `isolated environment |
| 177 |
<https://packaging.python.org/en/latest/glossary/#term-Isolated-Build>`_ (a clean temporary `virtual environment |
| 178 |
<https://docs.python.org/3/tutorial/venv.html>`_) to ensure reproducible builds. To skip this and use your current |
| 179 |
environment: |
| 180 |
|
| 181 |
.. code-block:: console |
| 182 |
|
| 183 |
$ python -m build --no-isolation |
| 184 |
|
| 185 |
Or the short form: |
| 186 |
|
| 187 |
.. code-block:: console |
| 188 |
|
| 189 |
$ python -m build -n |
| 190 |
|
| 191 |
.. warning:: |
| 192 |
|
| 193 |
When using ``--no-isolation``, you must manually install all build dependencies. This is mainly useful for: |
| 194 |
|
| 195 |
- Offline or air-gapped environments (no internet access) |
| 196 |
- Debugging build issues |
| 197 |
- Linux distribution packaging where dependencies are provided externally |
| 198 |
|
| 199 |
**************************** |
| 200 |
Skipping dependency checks |
| 201 |
**************************** |
| 202 |
|
| 203 |
To skip checking if build dependencies are installed (requires ``--no-isolation``): |
| 204 |
|
| 205 |
.. code-block:: console |
| 206 |
|
| 207 |
$ python -m build --no-isolation --skip-dependency-check |
| 208 |
|
| 209 |
Or: |
| 210 |
|
| 211 |
.. code-block:: console |
| 212 |
|
| 213 |
$ python -m build -nx |
| 214 |
|
| 215 |
****************** |
| 216 |
Common workflows |
| 217 |
****************** |
| 218 |
|
| 219 |
Development build |
| 220 |
================= |
| 221 |
|
| 222 |
Quick build during development: |
| 223 |
|
| 224 |
.. code-block:: console |
| 225 |
|
| 226 |
$ python -m build --wheel --installer=uv |
| 227 |
|
| 228 |
Fast CI build |
| 229 |
============= |
| 230 |
|
| 231 |
In CI where dependencies are pre-installed: |
| 232 |
|
| 233 |
.. code-block:: console |
| 234 |
|
| 235 |
$ pip install build build-backend-here |
| 236 |
$ python -m build --no-isolation |
| 237 |
|
| 238 |
Release build |
| 239 |
============= |
| 240 |
|
| 241 |
For uploading to `PyPI <https://pypi.org/>`_, build both sdist and wheel: |
| 242 |
|
| 243 |
.. tab:: uv |
| 244 |
|
| 245 |
.. code-block:: console |
| 246 |
|
| 247 |
$ python -m build |
| 248 |
$ uv publish |
| 249 |
|
| 250 |
`uv publish <https://docs.astral.sh/uv/guides/publish/>`_ is a modern option that handles uploading to PyPI with built-in support for trusted publishing. |
| 251 |
|
| 252 |
.. tab:: twine |
| 253 |
|
| 254 |
.. code-block:: console |
| 255 |
|
| 256 |
$ python -m build |
| 257 |
$ twine check dist/* |
| 258 |
$ twine upload dist/* |
| 259 |
|
| 260 |
See the `twine documentation <https://twine.readthedocs.io/>`_ for upload options. |
| 261 |
|
| 262 |
.. tip:: |
| 263 |
|
| 264 |
Use the ``hynek/build-and-inspect-python-package`` GitHub Action which handles this workflow including verification. |
| 265 |
See :doc:`ci-cd`. |
| 266 |
|
| 267 |
Testing the sdist |
| 268 |
================= |
| 269 |
|
| 270 |
Build from the sdist to ensure it's complete: |
| 271 |
|
| 272 |
.. code-block:: console |
| 273 |
|
| 274 |
$ python -m build --sdist |
| 275 |
$ python -m build dist/mypackage-1.0.0.tar.gz |
| 276 |
|
| 277 |
Or test installation: |
| 278 |
|
| 279 |
.. code-block:: console |
| 280 |
|
| 281 |
$ python -m build |
| 282 |
$ python -m pip install dist/mypackage-1.0.0.tar.gz |
| 283 |
$ python -c "import mypackage; print(mypackage.__version__)" |
| 284 |
|
| 285 |
*********************** |
| 286 |
Cleaning before build |
| 287 |
*********************** |
| 288 |
|
| 289 |
There's no built-in clean command. To ensure a fresh build, manually remove the dist directory: |
| 290 |
|
| 291 |
.. code-block:: console |
| 292 |
|
| 293 |
$ rm -rf dist/ |
| 294 |
$ python -m build |
| 295 |
|
| 296 |
Or to avoid stale artifacts, use a unique output directory: |
| 297 |
|
| 298 |
.. code-block:: console |
| 299 |
|
| 300 |
$ python -m build --outdir dist/v1.0.0 |
| 301 |
|
| 302 |
*********************************** |
| 303 |
Getting metadata without building |
| 304 |
*********************************** |
| 305 |
|
| 306 |
To extract package metadata without building the full package: |
| 307 |
|
| 308 |
.. code-block:: console |
| 309 |
|
| 310 |
$ python -m build --metadata |
| 311 |
|
| 312 |
This outputs the wheel metadata in JSON format to stdout. |
| 313 |
|
| 314 |
**************************** |
| 315 |
Checking the build version |
| 316 |
**************************** |
| 317 |
|
| 318 |
To see which version of build you're using: |
| 319 |
|
| 320 |
.. code-block:: console |
| 321 |
|
| 322 |
$ python -m build --version |
| 323 |
|
| 324 |
************************************ |
| 325 |
Installing build dependencies only |
| 326 |
************************************ |
| 327 |
|
| 328 |
For specialized workflows like static analysis or linting, you may want to install just the build dependencies without |
| 329 |
actually building: |
| 330 |
|
| 331 |
.. code-block:: python |
| 332 |
|
| 333 |
from build import ProjectBuilder |
| 334 |
|
| 335 |
builder = ProjectBuilder(".") |
| 336 |
|
| 337 |
# Get all build dependencies |
| 338 |
requires = builder.build_system_requires |
| 339 |
for dist in ["sdist", "wheel"]: |
| 340 |
requires.extend(builder.get_requires_for_build(dist)) |
| 341 |
|
| 342 |
# Install them |
| 343 |
import subprocess |
| 344 |
|
| 345 |
subprocess.run(["pip", "install", *requires]) |
| 346 |
|
| 347 |
This is useful when you need the same environment that build would create, but want to run other tools (like mypy, ruff, |
| 348 |
or custom linters) instead of building the package. |
| 349 |
|
| 350 |
See :doc:`../reference/api` for more programmatic usage examples. |
| 351 |
|
| 352 |
********** |
| 353 |
See also |
| 354 |
********** |
| 355 |
|
| 356 |
- :doc:`config-settings` for passing options to your build backend |
| 357 |
- :doc:`corporate-environments` for using build with private indexes |
| 358 |
- :doc:`troubleshooting` for common problems |
| 359 |
- :doc:`../reference/cli` for all command-line options |