Debian Codegraph @

docs/reference/cli.rst

raw
1 ########################
2 Command-Line Interface
3 ########################
4
5 A simple, correct Python build frontend.
6
7 By default, a `source distribution (sdist)
8 <https://packaging.python.org/en/latest/specifications/source-distribution-format/>`_ is built from the project root and
9 a `binary distribution (wheel) <https://packaging.python.org/en/latest/specifications/binary-distribution-format/>`_ is
10 built from the sdist. If this is undesirable, you can pass ``--sdist`` and/or ``--wheel`` to build distributions
11 independently of each other.
12
13 The positional ``srcdir`` argument also accepts a ``.tar.gz`` source distribution. Build checks the filename and the
14 presence of ``PKG-INFO``, extracts the archive into a temporary directory, and runs the wheel build against the
15 extracted source. The default ``--outdir`` is the directory containing the archive. ``--sdist`` errors against an
16 archive, since the archive already is an sdist.
17
18 .. sphinx_argparse_cli::
19 :module: build.__main__
20 :func: main_parser
21 :prog: python -m build
22 :title: python -m build
23 :usage_width: 97
24
25 ********************
26 Isolation Behavior
27 ********************
28
29 By default build will build the package in an isolated environment, but this behavior can be disabled with
30 ``--no-isolation``. When using isolation, build creates a temporary virtual environment, installs the build dependencies
31 specified in your ``pyproject.toml``, runs the build, and then cleans up the environment. This ensures reproducible
32 builds regardless of what packages are installed in your development environment.
33
34 Pass ``--env-dir PATH`` to put the environment at a fixed location instead of a temporary directory. The location must
35 be empty. build removes it after a successful build and keeps it after a failure so you can inspect it. A fixed path
36 helps compilation caches like ccache and sccache, which treat a changed build-environment path as a new file and miss
37 the cache. You cannot combine ``--env-dir`` with ``--no-isolation``.
38
39 When a build fails, ``--env-dir`` and ``--sdist-extract-dir`` keep the environment and the extracted sources for
40 inspection; see :ref:`debug-a-failed-build`.
41
42 ******************
43 Dependency Check
44 ******************
45
46 With ``--no-isolation``, build does not install anything; it checks that the build dependencies are already present in
47 the interpreter running build. When a requirement is unmet it exits with an ``Unmet dependencies`` error that names the
48 interpreter checked and, for each requirement, the version specifier that was ``wanted`` and the version that was
49 ``found`` (``not installed`` when absent):
50
51 .. code-block:: text
52
53 ERROR Unmet dependencies (checked against /usr/local/bin/python3.9):
54 anndata>=0.7.4
55 wanted: >=0.7.4
56 found: not installed
57 matplotlib>=3.4 -> kiwisolver>=1.0.1
58 wanted: >=1.0.1
59 found: 1.0.0
60
61 Transitive requirements are shown as a ``parent -> child`` chain; the ``wanted``/``found`` lines describe the unmet
62 leaf. Pass ``--skip-dependency-check`` to skip this check (see :doc:`../how-to/basic-usage`).
63
64 ****************
65 Verbose Output
66 ****************
67
68 Repeating ``-v`` raises the verbosity level. Each level adds to the previous one:
69
70 - ``-v`` streams the output of the environment-creation and dependency-installation subprocesses.
71 - ``-vv`` additionally passes ``-v`` through to the installer.
72
73 Regardless of verbosity, after installing the build dependencies of an isolated build, build prints a summary of the
74 resolved versions, one ``name==version`` per line. build reads these from the isolated environment's installed metadata,
75 so they reflect what was installed rather than the specifiers in ``pyproject.toml``:
76
77 .. code-block:: console
78
79 $ python -m build --wheel
80 * Creating isolated environment: venv+pip...
81 * Installing packages in isolated environment:
82 - setuptools >= 42.0.0
83 * Getting build dependencies for wheel...
84 * Installed build dependency versions:
85 - setuptools==82.0.1
86 * Building wheel...
87 Successfully built mypackage-1.0.0-py3-none-any.whl
88
89 build reports this for isolated builds only; with ``--no-isolation`` build installs nothing, so inspect the active
90 interpreter with your installer instead (for example ``pip list``).
91
92 **************
93 Build Report
94 **************
95
96 ``--report PATH`` writes a JSON description of the built artifacts to ``PATH`` after a successful build. The artifact
97 names are dynamic (they encode version, Python, ABI and platform tags), so this gives scripts and CI a stable way to
98 refer to the produced files instead of globbing ``dist/``. Build writes the report to a file it controls, not to
99 standard output, because the build backend may write to ``stdout``/``stderr`` too.
100
101 .. code-block:: console
102
103 $ python -m build --report build-report.json
104 $ twine upload $(jq -r '.artifacts[].path' build-report.json)
105
106 .. note::
107
108 Keep the report out of the output directory: tools commonly upload ``dist/*`` wholesale (``twine upload dist/*``),
109 and a stray non-distribution file there can break that. The write is atomic, so a reader never sees a partial file;
110 builds running in parallel still need a distinct ``PATH`` each to avoid overwriting one another's report.
111
112 The schema is:
113
114 .. code-block:: json
115
116 {
117 "version": "1.0",
118 "artifacts": [
119 {
120 "name": "mypackage-1.0.0.tar.gz",
121 "path": "dist/mypackage-1.0.0.tar.gz",
122 "kind": "sdist",
123 "size": 852,
124 "hashes": {
125 "sha256": "dbd5486e6e893663263caf84a4b87d67cc28f6963b6650f69eaa54b78e42ece4"
126 }
127 },
128 {
129 "name": "mypackage-1.0.0-py3-none-any.whl",
130 "path": "dist/mypackage-1.0.0-py3-none-any.whl",
131 "kind": "wheel",
132 "size": 1121,
133 "hashes": {
134 "sha256": "ef460897fdce998634efc3385aa4261b4280a2fe2ab0e08ddcacc8496658465d"
135 }
136 }
137 ]
138 }
139
140 ``version`` is the schema version. Each artifact lists its ``name`` (basename), ``path`` (relative to the current
141 directory, mirroring ``--outdir``), ``kind`` (``sdist`` or ``wheel``), ``size`` in bytes, and ``hashes`` keyed by
142 algorithm. ``--report`` cannot be combined with ``--metadata``.
143
144 To inspect a wheel listed in the report, pass its path straight to ``--metadata``: when the source argument is a
145 ``.whl`` file, build reads ``METADATA`` from the archive and prints it as JSON instead of building anything.
146
147 .. code-block:: console
148
149 $ python -m build --report build-report.json
150 $ python -m build --metadata "$(jq -r '.artifacts[] | select(.kind=="wheel") | .path' build-report.json)"
151
152 ************************
153 Alternative CLI Script
154 ************************
155
156 A ``pyproject-build`` CLI script is also available, which is functionally identical to ``python -m build``. This is
157 useful for tools like pipx_ that prefer direct script entry points.
158
159 .. code-block:: console
160
161 $ pyproject-build
162 $ pyproject-build --help
163
164 Both commands accept the same options and behave identically.
165
166 *****************
167 Common Patterns
168 *****************
169
170 For practical usage examples and workflows, see :doc:`../how-to/basic-usage`.
171
172 **********
173 See Also
174 **********
175
176 - :doc:`../how-to/basic-usage` for common workflows
177 - :doc:`../how-to/config-settings` for passing options to backends
178 - :doc:`environment-variables` for environment variables that affect build
179
180 .. _pipx: https://github.com/pipxproject/pipx