Sub topic 10
Building the sdist and the wheel
This chapter assumes you have a working pyproject.toml with a [build-system] table and a [project] table, as in the earlier chapters. It also assumes you can create and activate a virtual environment. Here you will build the two distribution files PyPI accepts, learn what each one is for, and see what happens during a build.
The two artifacts
A release of a pure-Python library normally consists of two files:
- A source distribution (sdist) is a
.tar.gzarchive of your source tree plus metadata. It is the "here is everything needed to build this project" file. - A wheel is a
.whlfile, which is a ZIP archive in a fixed layout. Installing it means copying its contents intosite-packages. No build step runs on the user's machine.
You publish both. Wheels make installs fast and predictable. The sdist is the fallback for platforms with no matching wheel. Linux distributions and other downstream packagers also build from it.
| Property | sdist | wheel |
|---|---|---|
| File name for this project | `greeter-0.1.0.tar.gz` | `greeter-0.1.0-py3-none-any.whl` |
| Contains | Source, `pyproject.toml`, `PKG-INFO`, extra files | Installable package files and metadata |
| Install runs the build backend | Yes | No |
| Needs build tools on the user's machine | Yes | No |
| Includes tests and other non-runtime files | Often | Rarely |
The wheel name encodes compatibility as {name}-{version}-{python tag}-{abi tag}-{platform tag}. For greeter, the tags are py3-none-any. The code is pure Python, needs no particular ABI, and runs on any platform. A project with compiled extensions gets tags such as cp312-cp312-manylinux_2_17_x86_64, and you must build one wheel per target platform.
What `python -m build` does
build is a build frontend. It reads [build-system], creates a temporary isolated environment, installs the requires packages there, and calls the backend through the standard hooks. With setuptools as the backend, those hooks are build_sdist and build_wheel.
By default build does this in order:
- Build the sdist from the source tree.
- Unpack the sdist into a temporary directory.
- Build the wheel from that unpacked sdist.
Building the wheel from the sdist is deliberate. If a file is missing from the sdist, the wheel build fails or produces a wheel without that file. You find the problem on your machine before a downstream packager does. If you build only the wheel with --wheel, build skips that cross-check and builds straight from the source tree.
Running the build
From the project root, in a virtual environment:
python -m pip install build
rm -rf dist build src/*.egg-info
python -m build
ls distThe expected listing is:
greeter-0.1.0-py3-none-any.whl
greeter-0.1.0.tar.gzThe rm -rf line matters. build does not clean dist/, so old versions pile up. A later twine check dist/* or twine upload dist/* then picks up every file in the directory, including releases you did not mean to send. Stale build/ directories can also leak files that no longer exist in your source tree into a new wheel. Clean first, or use --outdir to write to a fresh directory.
Useful flags:
| Flag | Effect |
|---|---|
| `--sdist` / `-s` | Build only the sdist |
| `--wheel` / `-w` | Build only the wheel, directly from the source tree |
| `--outdir DIR` / `-o DIR` | Write artifacts to `DIR` instead of `dist/` |
| `--no-isolation` / `-n` | Use the current environment instead of an isolated one |
What ends up in each file
The build includes what your configuration says it should, not everything in the directory. Look inside before you trust it. The sdist is a tar archive and the wheel is a ZIP, so the standard library can list both:
tar -tzf dist/greeter-0.1.0.tar.gz
python -m zipfile -l dist/greeter-0.1.0-py3-none-any.whlA healthy wheel for this project contains the package files and a .dist-info directory:
greeter/__init__.py
greeter/cli.py
greeter/py.typed
greeter-0.1.0.dist-info/METADATA
greeter-0.1.0.dist-info/WHEEL
greeter-0.1.0.dist-info/entry_points.txt
greeter-0.1.0.dist-info/licenses/LICENSE
greeter-0.1.0.dist-info/top_level.txt
greeter-0.1.0.dist-info/RECORDCheck for these things:
- Package files are under
greeter/. Thesrc/prefix must not appear in the wheel. If it does, the layout or backend configuration is wrong. py.typedis present. If it is missing, add it explicitly. With setuptools this is[tool.setuptools.package-data]withgreeter = ["py.typed"]. Then rebuild.entry_points.txtlists thegreeterconsole script. This file is howpipcreates the command.- The license file is under
dist-info/licenses/. This comes fromlicense-filesin[project]. - No stray files. Look for
.envfiles, notebooks, or large data you did not intend to publish.
The sdist should contain the src/greeter/ tree, pyproject.toml, README.md, LICENSE, and a generated PKG-INFO. Setuptools also adds a default set of files, which can include tests/test*.py, depending on version and configuration. If you need other files in the sdist, such as docs or a changelog, list them in MANIFEST.in. Files that appear only in the sdist do not affect what users get from the wheel.
Generated metadata
The backend writes your [project] table into a METADATA file in the wheel and a PKG-INFO file in the sdist. These files use core metadata fields such as Name, Version, Requires-Python, and Provides-Extra. PyPI shows these fields on your project page, and pip reads them to resolve dependencies. You can print the wheel's copy to confirm it:
python -m zipfile -e dist/greeter-0.1.0-py3-none-any.whl /tmp/greeter-wheel
cat /tmp/greeter-wheel/greeter-0.1.0.dist-info/METADATAConfirm that Version, Requires-Python: >=3.9, Provides-Extra: test, and the README text (the long description) look right. Many publishing errors come from a field that is wrong or missing here.
Common build failures
| Symptom | Likely cause |
|---|---|
| `No module named 'setuptools'` with `--no-isolation` | The build tools are not installed in the current environment |
| Wheel contains `src/greeter/...` | The package discovery configuration does not match the src layout |
| Wheel installs but `import greeter` fails | The package directory has no `__init__.py`, or discovery skipped it |
| `LICENSE` missing from the artifacts | `license-files` points at a path that does not exist |
| Metadata error on `license` | The backend is too old for the SPDX string form, so raise the `setuptools` lower bound |
| Old files appear in the new wheel | Stale `build/` directory from an earlier run |
If a build behaves oddly, read the log from the top. build prints each backend step, and the first warning is usually the real cause.
Reproducibility
Two builds of the same commit can produce byte-different archives, because timestamps and file ordering differ. Setting the SOURCE_DATE_EPOCH environment variable to a fixed Unix timestamp makes most backends use that time for archive entries, which helps when you want to compare artifacts. It is optional for a small library, but it is useful when you rebuild artifacts to verify a release.
Exercise
You run python -m build --wheel and the build succeeds. A colleague then runs python -m build on the same commit and it fails while building the wheel. What is the most likely explanation, and what does it tell you?
Show answer
The plain python -m build builds the wheel from the unpacked sdist, while --wheel builds straight from the source tree. The failure most likely means a file the build needs is missing from the sdist, for example a data file or license file that is not included by default. Add it to MANIFEST.in or the backend's include configuration, then rebuild. The sdist-first build caught the problem before a downstream packager did.
Exercise
After building, python -m zipfile -l shows src/greeter/__init__.py inside the wheel instead of greeter/__init__.py. Is the wheel usable, and why or why not?
Show answer
It is not usable as intended. Installing it would place a top-level src package in site-packages, so import greeter would fail and import src.greeter would work, polluting the namespace. The backend's package discovery did not map src/ as the package root. Fix the discovery configuration, rebuild, and list the wheel again.