Sub topic 10
Building the sdist and the wheel
This chapter assumes you have the greeter project from the earlier chapters: a src layout and a pyproject.toml with a [build-system] table and a [project] table. It also assumes you can use a virtual environment and pip.
Two artifacts, two jobs
A build produces two files in dist/:
- The sdist (source distribution) is a
.tar.gzarchive of your source files plus metadata. It is the fallback pip uses when no compatible wheel exists, and it lets others rebuild your package. - The wheel is a
.whlfile, a zip archive in the final installed layout. pip unpacks it intosite-packageswithout running any build code, so installs are fast and predictable.
Running the build
Install the build frontend and run it from the directory that contains pyproject.toml:
python -m pip install build
python -m build
ls dist/You should see:
greeter-0.1.0-py3-none-any.whl
greeter-0.1.0.tar.gzBy default build first creates the sdist. It then builds the wheel from that sdist, not from your working tree. If the sdist is missing a file, the wheel build fails or the wheel is incomplete, and you find out before you publish.
Reading the wheel's filename
The wheel name has the form name-version-pythontag-abitag-platformtag.whl. In py3-none-any:
py3means any Python 3 interpreter.nonemeans no dependence on a specific C ABI.anymeans any platform.
That tag is correct for pure-Python code. A wheel containing compiled extensions gets platform-specific tags instead.
Useful options
| Command | Effect |
|---|---|
| `python -m build --sdist` | Build only the sdist |
| `python -m build --wheel` | Build only the wheel, directly from the source tree |
| `python -m build --outdir out/` | Write artifacts somewhere other than `dist/` |
| `python -m build --no-isolation` | Use the current environment instead of a fresh one |
Isolation is the default for a reason. The frontend creates a temporary virtual environment, installs the packages listed in build-system.requires, and builds there. That means a build that works on your machine does not depend on packages you happened to install globally.
Common errors
- Stale files in
dist/.builddoes not clean the directory. Old versions stay there, andtwine upload dist/*would try to upload them too. Deletedist/before each build. - Wrong working directory. Running from a parent folder fails with an error about a missing
pyproject.tomlorsetup.py. - Missing files in the sdist. Setuptools includes your packages,
pyproject.toml, and the README and license files it finds. Other files, such as test data outside the package, may be left out. List the contents of the archive to check. - Version already used. PyPI never accepts the same filename twice. Bump the version before you build a new release.
Exercise
You ran python -m build for 0.1.0, changed the code, bumped the version to 0.1.1, and built again. What does dist/ contain, and what problem could that cause when you run twine upload dist/*?
Show answer
It contains four files: the 0.1.0 and 0.1.1 sdists and wheels. The upload would try to send the 0.1.0 files again, and PyPI would reject them because that version already exists. Delete dist/ before building, or upload only the 0.1.1 files.