Debian Codegraph @

docs/tutorial/getting-started.rst

raw
1 #################
2 Getting Started
3 #################
4
5 This tutorial will guide you through installing build and creating your first Python package ready for distribution.
6
7 .. note::
8
9 New to Python packaging? Start with the `Python Packaging User Guide tutorial
10 <https://packaging.python.org/en/latest/tutorials/packaging-projects/>`_ to learn how to structure a Python project.
11 This tutorial assumes you already have a project with a ``pyproject.toml`` file.
12
13 ***************
14 Prerequisites
15 ***************
16
17 You need Python 3.10 or later installed on your system. Check your Python version:
18
19 .. code-block:: console
20
21 $ python --version
22 Python 3.11.0
23
24 **************
25 Installation
26 **************
27
28 The recommended way to install build is using pip:
29
30 .. code-block:: console
31
32 $ pip install build
33
34 Or, if you prefer to install it in an isolated environment using pipx:
35
36 .. code-block:: console
37
38 $ pipx install build
39
40 For corporate environments or systems with restricted internet access, see :doc:`../how-to/corporate-environments`.
41
42 ***************************
43 Creating a simple package
44 ***************************
45
46 Let's create a minimal Python package to demonstrate how build works.
47
48 1. Create a project directory:
49
50 .. code-block:: console
51
52 $ mkdir mypackage
53 $ cd mypackage
54
55 2. Create a ``pyproject.toml`` file:
56
57 .. code-block:: toml
58
59 [build-system]
60 requires = ["hatchling"]
61 build-backend = "hatchling.build"
62
63 [project]
64 name = "mypackage"
65 version = "0.1.0"
66 description = "A simple example package"
67 readme = "README.md"
68 requires-python = ">=3.8"
69
70 3. Create a ``README.md`` file:
71
72 .. code-block:: markdown
73
74 # My Package
75
76 This is a simple example package.
77
78 4. Create your Python package:
79
80 .. code-block:: console
81
82 $ mkdir src/mypackage
83 $ touch src/mypackage/__init__.py
84
85 5. Add some code to ``src/mypackage/__init__.py``:
86
87 .. code-block:: python
88
89 def hello():
90 return "Hello from mypackage!"
91
92 ***********************
93 Building your package
94 ***********************
95
96 Now you're ready to build your package:
97
98 .. code-block:: console
99
100 $ python -m build
101
102 You should see output like:
103
104 .. code-block:: console
105
106 * Creating isolated environment: venv+pip...
107 * Installing packages in isolated environment:
108 - hatchling
109 * Getting build dependencies for sdist...
110 * Building sdist...
111 * Building wheel from sdist
112 * Creating isolated environment: venv+pip...
113 * Installing packages in isolated environment:
114 - hatchling
115 * Getting build dependencies for wheel...
116 * Building wheel...
117 Successfully built mypackage-0.1.0.tar.gz and mypackage-0.1.0-py3-none-any.whl
118
119 The built packages are now in the ``dist/`` directory:
120
121 .. code-block:: console
122
123 $ ls dist/
124 mypackage-0.1.0-py3-none-any.whl
125 mypackage-0.1.0.tar.gz
126
127 *****************************
128 Understanding what happened
129 *****************************
130
131 Build created two `distribution files <https://packaging.python.org/en/latest/glossary/#term-Distribution-Package>`_
132 (packages ready for distribution):
133
134 1. **Source distribution**: ``mypackage-0.1.0.tar.gz``
135
136 This `tarball <https://en.wikipedia.org/wiki/Tar_(computing)>`_ (compressed archive) contains your source code.
137 Anyone can download it and build your package on their system. Learn more in the `source distribution specification
138 <https://packaging.python.org/en/latest/specifications/source-distribution-format/>`_.
139
140 2. **Wheel**: ``mypackage-0.1.0-py3-none-any.whl``
141
142 This `wheel <https://packaging.python.org/en/latest/specifications/binary-distribution-format/>`_ is a pre-built
143 package that `pip <https://pip.pypa.io/>`_ can install directly without needing to build anything. This makes
144 installation much faster.
145
146 Build used an **isolated environment** to ensure your package builds consistently regardless of what you have installed
147 on your computer. It:
148
149 1. Created a temporary `virtual environment <https://docs.python.org/3/tutorial/venv.html>`_
150 2. Installed only your build dependencies (`hatchling <https://hatch.pypa.io/latest/>`_)
151 3. Invoked the **build backend** to create the distribution files
152 4. Cleaned up the temporary environment when done
153
154 *****************************************
155 Seeing which backend versions were used
156 *****************************************
157
158 build deletes the isolated environment after each build, so it does not leave the installed versions behind. Instead it
159 prints them as it goes:
160
161 .. code-block:: console
162
163 $ python -m build --wheel
164 ...
165 * Getting build dependencies for wheel...
166 * Installed build dependency versions:
167 - hatchling==1.30.1
168 * Building wheel...
169
170 build prints each installed build dependency as ``name==version``. Keep this handy when reporting a problem to a
171 backend's issue tracker, which usually asks for the exact version you built with. See :doc:`../how-to/troubleshooting`.
172
173 **********************************
174 Building a wheel from your sdist
175 **********************************
176
177 You can also point build at the source distribution you just produced. Build checks the archive and runs a wheel build
178 against its contents, which catches missing files in the sdist:
179
180 .. code-block:: console
181
182 $ python -m build dist/mypackage-0.1.0.tar.gz
183 Successfully built mypackage-0.1.0-py3-none-any.whl
184
185 The wheel lands next to the sdist. Pass ``--outdir`` to write somewhere else. If your package compiles C/C++ and you
186 rebuild often, ``--sdist-extract-dir`` keeps the extracted sources at a fixed path so ``ccache``/``sccache`` can reuse
187 earlier compilations; see :doc:`../how-to/basic-usage`.
188
189 ****************************
190 Building without isolation
191 ****************************
192
193 If you install the build dependencies yourself and build with ``--no-isolation``, build first checks that every declared
194 dependency is present in the interpreter you are running. When something is missing it stops with an ``Unmet
195 dependencies`` error that names the interpreter it checked and, for each requirement, the version it ``wanted`` against
196 the version it ``found`` - so you can tell "not installed" apart from "installed, but the wrong version". See
197 :doc:`../how-to/troubleshooting` for how to read it.
198
199 *******************************
200 Recording what build produced
201 *******************************
202
203 If a script or CI job needs the exact filenames build produced, add ``--report PATH`` to write them to a JSON file
204 instead of parsing the output:
205
206 .. code-block:: console
207
208 $ python -m build --report build-report.json
209
210 ************
211 Next steps
212 ************
213
214 - Learn about :doc:`../how-to/basic-usage` for common build workflows
215 - Understand :doc:`../explanation/how-it-works` for details on the build process
216 - See :doc:`../how-to/config-settings` to customize your build
217 - When a build fails, follow :ref:`debug-a-failed-build` to capture backend logs and keep the working directories