Sub topic 11
Inspecting what you built before you upload it
This chapter assumes you can run python -m build and have a greeter project with a pyproject.toml, a src/ layout, and a dist/ directory holding one sdist and one wheel. PyPI does not let you replace a file once uploaded, so check the artifacts first.
Why inspect
A successful build only means the backend finished. It does not mean the right files are inside. The common failures are:
- A missing
LICENSEorpy.typedfile. - Stray files such as
.env, notebooks, or local caches in the sdist. - Broken metadata, such as a
readmethat PyPI cannot render. - Tests or other unwanted top-level packages in the wheel.
Step 1: validate the metadata
twine check reads the metadata of each file in dist/ and reports problems that would make PyPI reject the upload or render the description badly.
python -m pip install build twine
python -m build
python -m twine check dist/*A clean run prints PASSED for each file. Treat a warning as a failure, because it usually means the long description will not display on the project page.
Step 2: list the wheel contents
A wheel is a zip archive. You can list it with the standard library, with no extra tools.
python -m zipfile -l dist/greeter-0.1.0-py3-none-any.whlFor greeter you should see these entries:
greeter/__init__.pygreeter/cli.pygreeter/py.typedgreeter-0.1.0.dist-info/METADATAgreeter-0.1.0.dist-info/entry_points.txtgreeter-0.1.0.dist-info/licenses/LICENSE
The package sits at the top of the archive, not under src/. That is correct, because src/ is only a layout choice for your repository. Nothing from tests/ should appear.
Step 3: list the sdist contents
An sdist is a gzipped tar file. It must hold everything needed to rebuild the wheel.
python -m tarfile -l dist/greeter-0.1.0.tar.gzLook for pyproject.toml, README.md, LICENSE, and the src/greeter/ files. Tests are often included in an sdist, and that is fine. Secrets and build junk are not.
Step 4: read the metadata
The METADATA file inside the wheel is what pip and PyPI read. Print it to confirm the name, version, Requires-Python, extras, and license.
python -m zipfile -e dist/greeter-0.1.0-py3-none-any.whl /tmp/inspect
cat /tmp/inspect/greeter-0.1.0.dist-info/METADATACheck these fields:
NameandVersionmatch what you intend to release.Requires-Pythonshows>=3.9.Provides-Extra: testappears.- The README text appears after the headers.
Exercise
You run python -m zipfile -l on your wheel and see src/greeter/__init__.py as the path inside the archive. What does this suggest, and where would you look to fix it?
Show answer
The wheel was built with the package placed under a src/ directory instead of at the top level, so an install would put the wrong path into site-packages. Check the package discovery settings in pyproject.toml (for setuptools, make sure the src directory is the package root, which is the default for a src layout), then rebuild and list the wheel again.