Sub topic 16
Publishing to a package index
This chapter assumes you can run uv build and that your pyproject.toml has a [build-system] table, as covered earlier. You should also know what a wheel and an sdist are.
What publishing does
A package index is a server that stores distributions and serves them to installers. PyPI is the public one. Publishing uploads the files in dist/ (an sdist and a wheel) to an index, so others can run uv add your-package or pip install your-package.
The flow has four steps: set metadata, build, test on a test index, then upload.
Prepare the metadata
PyPI rejects uploads whose name and version already exist, so choose both carefully. The [project] table supplies them, along with the description and readme shown on the project page.
[project]
name = "weather-cli"
version = "0.1.1"
description = "Print the weather from the command line"
readme = "README.md"
requires-python = ">=3.12"
license = "MIT"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"Change weather-cli to a name that is free on the index. The license value is an SPDX expression, which requires a recent build backend.
Build and upload
Build the distributions, then publish them. Uploading needs an API token, which you create in your PyPI account settings. Pass it through an environment variable rather than typing it into shell history or committing it.
uv build
export UV_PUBLISH_TOKEN="pypi-your-token-here"
uv publishuv publish uploads everything in dist/ by default. To upload to another index, give it a URL with --publish-url.
Try TestPyPI first
TestPyPI is a separate index meant for practice. Accounts and tokens are separate from PyPI's.
uv publish --publish-url https://test.pypi.org/legacy/
uv run --with weather-cli --no-project \
--index https://test.pypi.org/simple/ \
--index-strategy unsafe-best-match \
-- weatherThe second command installs the package into a temporary environment and runs the weather entry point. TestPyPI often lacks your dependencies, so --index-strategy unsafe-best-match lets uv also look at PyPI. Without a fallback, dependency resolution can fail.
Common errors
- File already exists. An index never accepts the same filename twice, even after you delete a release. Bump the version and rebuild.
- Stale files in
dist/.uv publishuploads every file there. Remove old builds, or pass explicit paths such asuv publish dist/weather_cli-0.1.1*. - Invalid or missing credentials. A 403 response usually means a wrong token, or a token scoped to a different project.
- Missing metadata. An unreadable
readmepath or a missingnamemakes the build or the upload fail.
Exercise
You ran uv publish and got "file already exists" for version 0.1.0. You fixed a typo in the README. What should you do before uploading again?
Show answer
Change version in pyproject.toml (for example to 0.1.1), delete the old files in dist/ or rebuild into a clean directory, run uv build, then run uv publish. The index never accepts a filename it has seen before.
Exercise
Why would you publish to TestPyPI before PyPI?
Show answer
A release on PyPI cannot be replaced or reused. TestPyPI lets you check that the metadata, README rendering, and installed entry point work, without spending a real version number.