Debian Codegraph @

docs/how-to/choosing-tools.rst

raw
1 ######################
2 build vs Other Tools
3 ######################
4
5 The Python packaging ecosystem has several tools that work together. This guide helps you understand when to use build
6 versus other tools.
7
8 *******************
9 When to use build
10 *******************
11
12 Use build when you want to:
13
14 - Create distribution packages (`source distributions
15 <https://packaging.python.org/en/latest/specifications/source-distribution-format/>`_ and/or `wheels
16 <https://packaging.python.org/en/latest/specifications/binary-distribution-format/>`_) for your project
17 - Build for your **current Python version and platform** only
18 - Create packages to upload to `PyPI <https://pypi.org/>`_
19 - Test that your package builds correctly
20 - Build as part of a development workflow
21
22 Build is a **build frontend** - it knows how to invoke :std:term:`build backends <Build Backend>` (`setuptools
23 <https://setuptools.pypa.io/>`_, `hatchling <https://hatch.pypa.io/latest/>`_, `flit <https://flit.pypa.io/>`_, etc.)
24 but doesn't handle testing, multi-version builds, or task automation.
25
26 .. code-block:: console
27
28 $ python -m build
29
30 This builds your package for the currently active Python interpreter.
31
32 **************************
33 When to use cibuildwheel
34 **************************
35
36 Use `cibuildwheel <https://cibuildwheel.readthedocs.io/>`_ when you need to build `wheels
37 <https://packaging.python.org/en/latest/specifications/binary-distribution-format/>`_ for **multiple Python versions and
38 platforms**:
39
40 - Building wheels for distribution on `PyPI <https://pypi.org/>`_
41 - Supporting Python 3.8, 3.9, 3.10, 3.11, 3.12, 3.13, 3.14
42 - Supporting multiple operating systems (Linux, macOS, Windows)
43 - Building `manylinux <https://peps.python.org/pep-0513/>`_/`musllinux <https://peps.python.org/pep-0656/>`_ wheels
44 (standardized Linux wheel formats)
45 - Building wheels with compiled extensions (C, C++, Rust, etc.)
46
47 cibuildwheel uses build under the hood but handles all the complexity of:
48
49 - Running builds in `manylinux containers <https://github.com/pypa/manylinux>`_
50 - Testing wheels after building
51 - Handling platform-specific quirks
52 - Proper `ABI tagging <https://packaging.python.org/en/latest/specifications/platform-compatibility-tags/>`_
53
54 Example ``.github/workflows/build.yml``:
55
56 .. code-block:: yaml
57
58 name: Build
59
60 on: [push, pull_request]
61
62 jobs:
63 build_wheels:
64 name: Build wheels on ${{ matrix.os }}
65 runs-on: ${{ matrix.os }}
66 strategy:
67 matrix:
68 os: [ubuntu-latest, windows-latest, macos-latest]
69
70 steps:
71 - uses: actions/checkout@v4
72 - uses: pypa/[email protected]
73 - uses: actions/upload-artifact@v4
74 with:
75 name: wheels
76 path: ./wheelhouse/*.whl
77
78 See `cibuildwheel documentation <https://cibuildwheel.readthedocs.io/>`_ for complete details.
79
80 *****************
81 When to use tox
82 *****************
83
84 Use `tox <https://tox.wiki/>`_ when you need to:
85
86 - Test your package across multiple Python versions (e.g., 3.10, 3.11, 3.12, 3.13, 3.14)
87 - Run tests in `isolated environments <https://packaging.python.org/en/latest/glossary/#term-Virtual-Environment>`_
88 - Automate testing workflows
89 - Run linters (code checkers), formatters, type checkers
90 - Build documentation
91 - Orchestrate multiple development tasks
92
93 Example ``tox.toml``:
94
95 .. code-block:: toml
96
97 [env_run_base]
98 description = "run test suite with {basepython}"
99 deps = [
100 "pytest",
101 "pytest-cov",
102 ]
103 commands = [
104 ["pytest", "tests"],
105 ]
106
107 [env.build]
108 description = "build the package"
109 deps = ["build"]
110 commands = [["python", "-m", "build"]]
111
112 [env.lint]
113 description = "run linters and type checkers"
114 deps = [
115 "ruff",
116 "mypy",
117 ]
118 commands = [
119 ["ruff", "check", "."],
120 ["mypy", "src"],
121 ]
122
123 Tox can call build to create distributions, but its main purpose is test automation.
124
125 See `tox documentation <https://tox.wiki/>`_ for complete details.
126
127 *****************
128 When to use nox
129 *****************
130
131 Use `nox <https://nox.thea.codes/>`_ when you want tox-like functionality but prefer Python over INI:
132
133 Example ``noxfile.py``:
134
135 .. code-block:: python
136
137 import nox
138
139
140 @nox.session(python=["3.10", "3.11", "3.12", "3.13", "3.14"])
141 def tests(session):
142 session.install("pytest", "pytest-cov")
143 session.run("pytest", "tests")
144
145
146 @nox.session
147 def build(session):
148 session.install("build")
149 session.run("python", "-m", "build")
150
151 See `nox documentation <https://nox.thea.codes/>`_ for complete details.
152
153 ****************
154 When to use uv
155 ****************
156
157 Use `uv <https://docs.astral.sh/uv/>`_ when you want:
158
159 - Fast package installation (faster than pip)
160 - Workspace management (monorepos with multiple packages)
161 - Combined dependency resolution and installation
162 - Modern Python packaging workflows
163
164 uv can also build packages:
165
166 .. code-block:: console
167
168 $ uv build
169
170 This is equivalent to ``python -m build --installer=uv``.
171
172 See `uv documentation <https://docs.astral.sh/uv/>`_ for complete details.
173
174 *****************
175 When to use pip
176 *****************
177
178 Use pip when you need to:
179
180 - **Install** packages (not build them)
181 - Install from PyPI or other indexes
182 - Install in editable mode for development
183 - Manage dependencies in your environment
184
185 pip can build packages as a side effect of installation, but for explicit building, use build.
186
187 .. code-block:: console
188
189 # Installing (use pip)
190 $ pip install mypackage
191
192 # Installing in editable mode (use pip)
193 $ pip install -e .
194
195 # Building distributions (use build)
196 $ python -m build
197
198 ************************
199 How they work together
200 ************************
201
202 A typical complete workflow might use all these tools:
203
204 Development workflow
205 ====================
206
207 .. code-block:: console
208
209 # Install your package in editable mode
210 $ pip install -e .[dev]
211
212 # Run tests across Python versions
213 $ tox
214
215 # Build distributions
216 $ python -m build
217
218 # Upload to PyPI
219 $ twine upload dist/*
220
221 CI/CD workflow
222 ==============
223
224 .. code-block:: yaml
225
226 name: CI
227
228 on: [push, pull_request]
229
230 jobs:
231 test:
232 runs-on: ubuntu-latest
233 strategy:
234 matrix:
235 python: ["3.10", "3.11", "3.12", "3.13", "3.14"]
236 steps:
237 - uses: actions/checkout@v4
238 - uses: actions/setup-python@v5
239 with:
240 python-version: ${{ matrix.python }}
241 - run: pip install tox
242 - run: tox -e py
243
244 build:
245 runs-on: ubuntu-latest
246 steps:
247 - uses: actions/checkout@v4
248 - uses: hynek/build-and-inspect-python-package@v2
249
250 build_wheels:
251 runs-on: ${{ matrix.os }}
252 strategy:
253 matrix:
254 os: [ubuntu-latest, windows-latest, macos-latest]
255 steps:
256 - uses: actions/checkout@v4
257 - uses: pypa/[email protected]
258
259 This workflow:
260
261 1. Uses **tox** to test across Python versions
262 2. Uses **build** (via hynek's action) to create sdist
263 3. Uses **cibuildwheel** to create wheels for all platforms
264
265 ***************
266 Decision tree
267 ***************
268
269 .. mermaid::
270
271 %%{init: {'theme':'base', 'themeVariables': { 'primaryColor':'#4051b5','primaryTextColor':'#fff','primaryBorderColor':'#2c3e8f','lineColor':'#5468c4','secondaryColor':'#7c8fd6','tertiaryColor':'#e8eaf6'}}}%%
272 flowchart TD
273 A[What do you need to do?] --> B{Build a package?}
274 B -->|Yes| C{For how many platforms?}
275 B -->|No, just testing| D{Multiple Python versions?}
276 B -->|No, just installing| E[Use pip or uv]
277
278 C -->|Current platform only| F[Use python -m build]
279 C -->|Multiple platforms/versions| G[Use cibuildwheel]
280
281 D -->|Yes| H[Use tox or nox]
282 D -->|No| I[Use pytest or your test runner]
283
284 style F fill:#4051b5,stroke:#2c3e8f,color:#fff
285 style G fill:#4051b5,stroke:#2c3e8f,color:#fff
286 style H fill:#7c8fd6,stroke:#5468c4,color:#fff
287 style I fill:#7c8fd6,stroke:#5468c4,color:#fff
288 style E fill:#f57c00,stroke:#e65100,color:#fff
289
290 *****************
291 Quick reference
292 *****************
293
294 ============ ================================ ==============================
295 Tool Purpose Use for
296 ============ ================================ ==============================
297 build Build sdist/wheel for one Python Creating distributions
298 cibuildwheel Build wheels for all platforms Distributing on PyPI
299 tox Test across Python versions CI/testing automation
300 nox Like tox, but Python-based CI/testing automation
301 uv Fast package manager Installing + optional building
302 pip Package installer Installing packages
303 twine Upload to PyPI Publishing
304 ============ ================================ ==============================
305
306 **********
307 See also
308 **********
309
310 - :doc:`basic-usage` for build command examples
311 - :doc:`ci-cd` for CI/CD integration
312 - :doc:`../explanation/build-backends` to understand build vs backend