Sub topic 11
Inspecting what you built before you upload it
Once python -m build finishes, dist/ holds an sdist and a wheel. Before you upload them, check what is inside. PyPI never lets you replace a file once it is published. A missing data file, a stray secret, or broken metadata is permanent for that version. You can only yank it and ship a new version.
This chapter assumes you can run python -m build on a project with a pyproject.toml and a src layout. You should also know that a wheel is a zip archive and an sdist is a tarball.
What to check
Inspection answers three questions:
- Does each artifact contain the files you intend to ship, and nothing else?
- Is the metadata valid and rendered as you expect on PyPI?
- Do the filenames carry the name, version, and tags you expect?
Read the filenames first
The filenames already tell you a lot. For the greeter project you should see:
dist/
├── greeter-0.1.0.tar.gz
└── greeter-0.1.0-py3-none-any.whlA wheel name has the form {name}-{version}-{python tag}-{abi tag}-{platform tag}.whl. The tags py3-none-any mean pure Python, no compiled extension, any platform. If you see a tag like cp312-cp312-linux_x86_64 for a pure Python library, something compiled by accident. If the version is not the one you expected, such as 0.1.0.dev3, you built from the wrong commit or configuration.
Stale files are a common source of confusion. dist/ is not cleaned between builds, and twine upload dist/* uploads everything in it. Delete the directory before each build:
rm -rf dist/ build/
python -m buildList the wheel contents
A wheel is a zip file, so the standard library can list it. This works on any platform and needs no extra tools:
python -m zipfile -l dist/greeter-0.1.0-py3-none-any.whlFor the example project the listing should look like this:
File Name Modified Size
greeter/__init__.py 2026-11-26 10:52:48 155
greeter/cli.py 2026-11-26 10:52:48 187
greeter/py.typed 2026-11-26 10:52:48 0
greeter-0.1.0.dist-info/licenses/LICENSE 2026-11-26 10:52:48 1067
greeter-0.1.0.dist-info/METADATA 2026-11-26 10:52:48 812
greeter-0.1.0.dist-info/WHEEL 2026-11-26 10:52:48 91
greeter-0.1.0.dist-info/entry_points.txt 2026-11-26 10:52:48 42
greeter-0.1.0.dist-info/top_level.txt 2026-11-26 10:52:48 8
greeter-0.1.0.dist-info/RECORD 2026-11-26 10:52:48 711Sizes and dates will differ on your machine. The structure is what matters. Read it with these points in mind:
- The package sits at the top level. There is no
src/prefix. The src layout exists only in your source tree. If you seesrc/greeter/...inside the wheel, the backend was misconfigured. py.typedis present. If it is missing, type checkers will ignore your inline annotations for users of the library.- No tests, no
.git, no.env. Tests are normally not part of a wheel. Anything that looks like a credential or a local config file is a reason to stop. - The
.dist-infodirectory holds the metadata.entry_points.txtis what creates thegreetercommand at install time.licenses/LICENSEis there because oflicense-files.
The exact name of top_level.txt and the placement of license files depend on the backend and its version. Do not treat them as a fixed contract.
List the sdist contents
An sdist is a gzip-compressed tar archive. List it with tar:
tar -tzf dist/greeter-0.1.0.tar.gzAn sdist is different from a wheel. It contains the source tree as a build needs it, so you should expect to see more:
greeter-0.1.0/PKG-INFO
greeter-0.1.0/pyproject.toml
greeter-0.1.0/README.md
greeter-0.1.0/LICENSE
greeter-0.1.0/src/greeter/__init__.py
greeter-0.1.0/src/greeter/cli.py
greeter-0.1.0/src/greeter/py.typed
greeter-0.1.0/src/greeter.egg-info/...
greeter-0.1.0/tests/test_greet.pyHere the src/ prefix is correct, because an sdist keeps the source layout. The sdist must contain pyproject.toml, the readme named in readme, and the license files. If README.md is missing from the sdist, then building the wheel from the sdist will fail, because the readme field points at a file that no longer exists.
Whether tests/ is included depends on the backend. Setuptools includes test files that match its default patterns. This is useful for downstream packagers who want to run your tests, so it is usually fine.
Compare the sdist and the wheel
Files can be in the sdist and missing from the wheel, or the other way around. The usual causes are:
| Symptom | Likely cause |
|---|---|
| Data file is in the sdist but not in the wheel | The backend was not told to treat it as package data |
| Data file is in the wheel but not in the sdist | A build-time generated file, so building from the sdist may produce different results |
| Whole package missing from the wheel | Package discovery found nothing, often a wrong `src` path |
| Files from an old build appear | A stale `build/` directory was reused |
A good habit is to build the wheel from the sdist, which is what installers do when no wheel is available. python -m build builds the sdist first and then builds the wheel from it by default, so a successful run already exercises this path. If you pass --wheel alone, the wheel is built straight from the source tree and skips that check.
Check the metadata with twine
Listing files does not validate metadata. twine check does:
python -m pip install twine
python -m twine check dist/*A clean result looks like this:
Checking dist/greeter-0.1.0-py3-none-any.whl: PASSED
Checking dist/greeter-0.1.0.tar.gz: PASSEDThe command verifies that the long description (your readme) will render on PyPI and that required metadata fields are present and well formed. Common failures are:
- Invalid readme markup. If the readme is reStructuredText with a syntax error, or the content type does not match the file, the check fails. With
readme = "README.md"the content type is set to Markdown automatically. - Missing or malformed fields. An empty
nameor an invalid version string is reported here, before PyPI rejects it. - Warnings. Treat a warning as a failure when you release. Pass
--strictso warnings produce a non-zero exit code:
python -m twine check --strict dist/*twine check does not confirm that the name is free on PyPI, that your classifiers are valid trove classifiers (PyPI rejects unknown ones at upload time), or that the version is higher than the last release. Those errors only appear when you upload.
Read the metadata yourself
The METADATA file inside the wheel is what pip and PyPI display. Print it and compare it against your pyproject.toml:
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/METADATAYou should see lines such as these, followed by your readme text:
Metadata-Version: 2.4
Name: greeter
Version: 0.1.0
Summary: A tiny library that builds greeting messages.
License-Expression: MIT
License-File: LICENSE
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.9
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Description-Content-Type: text/markdownCheck each of these against what you intended:
Requires-Pythonmatches the lowest Python version you test on. A value that is too loose lets users on unsupported versions install a broken release.- Each
Requires-Distline without anextra ==marker is a hard runtime dependency. Anything there that you meant as optional is in the wrong place. Provides-Extralists every extra. A typo in an extra name here means users get a warning and no packages when they runpip install "greeter[tset]".- The
Metadata-Versiondepends on the backend version. License fields in the form shown above need a recent setuptools, which is why the examplepyproject.tomlrequiressetuptools>=77.
A script that fails when something is wrong
Manual reading is fine once. For repeated releases, automate the structural checks. This script asserts that the wheel has what you expect and nothing you do not expect. It uses only the standard library:
import sys
import zipfile
from pathlib import Path
REQUIRED = {
"greeter/__init__.py",
"greeter/cli.py",
"greeter/py.typed",
}
FORBIDDEN_PREFIXES = ("tests/", "src/", ".git", ".env")
def check_wheel(path: Path) -> list[str]:
problems = []
with zipfile.ZipFile(path) as wheel:
names = set(wheel.namelist())
for name in sorted(REQUIRED - names):
problems.append(f"missing: {name}")
for name in sorted(names):
if name.startswith(FORBIDDEN_PREFIXES):
problems.append(f"unexpected: {name}")
if not any(n.endswith(".dist-info/entry_points.txt") for n in names):
problems.append("missing: entry_points.txt")
return problems
def main() -> int:
wheels = sorted(Path("dist").glob("*.whl"))
if len(wheels) != 1:
print(f"expected exactly one wheel in dist/, found {len(wheels)}")
return 1
problems = check_wheel(wheels[0])
for problem in problems:
print(problem)
return 1 if problems else 0
if __name__ == "__main__":
sys.exit(main())Save it as check_dist.py and run python check_dist.py after each build. The check on the number of wheels catches the stale-file problem described earlier. The list syntax list[str] needs Python 3.9 or later, which matches the requires-python in the example project.
Edge cases worth knowing
- Files ignored by Git can still be shipped. Some backends include everything that matches their patterns, and some use Git to decide what goes in the sdist. Setuptools relies on
MANIFEST.inand its defaults unless a plugin such assetuptools-scmis used. Always verify with the listing. - Secrets in the sdist. A
.envfile or a key left in the project root can end up in an sdist if a pattern matches it. Search the listing before uploading. Published files cannot be fully retracted, and yanking only hides a release from resolvers, so a leaked credential must be rotated. - Reproducibility. Two builds of the same source can produce archives with different bytes, since timestamps differ. Compare file lists, not hashes.
- Size. An unexpectedly large wheel usually means a data directory, a notebook, or a build artifact was included. PyPI has a per-file size limit, but you should notice long before you reach it.
Exercise
You run python -m zipfile -l on your wheel and see entries starting with src/greeter/. twine check passes. Should you upload? What do you do?
Show answer
No. twine check only validates metadata, not file placement. Entries under src/ mean the package is nested wrongly inside the wheel, so import greeter would fail after installation (the module would be at src.greeter, not importable as greeter). Fix the package discovery configuration so the backend looks in src/, delete dist/ and build/, rebuild, and list the wheel again.
Exercise
Your release directory contains greeter-0.1.0-py3-none-any.whl, greeter-0.1.0.tar.gz, and an old greeter-0.0.9.tar.gz. Why is running twine upload dist/* a problem, and what prevents it?
Show answer
The glob uploads all three files, including the old 0.0.9 sdist. That version either already exists on PyPI, which makes the upload fail partway, or it is an unintended extra file. Remove dist/ and build/ before every build (rm -rf dist/ build/) so only the files from the current build are present, and confirm with the file listing.
Exercise
A teammate says that since the sdist is built first and the wheel is built from it, there is no need to look inside the wheel. What is wrong with this reasoning?
Show answer
Building the wheel from the sdist only proves the sdist contains enough to build. It does not prove the wheel contains what you intend to ship. A data file, py.typed, or the license file can be left out of the wheel while the build still succeeds. Only listing the wheel and reading its METADATA shows what users actually receive.