Sub topic 16
Publishing to a package index
This chapter covers building a distribution and uploading it with uv publish. It assumes you have read the chapters on pyproject.toml, entry points, and build backends. You should know what [project] and [build-system] contain and how uv build produces files in dist/.
What a package index is
A package index is a server that stores distribution files and answers the question "which versions of this name exist, and where are the files?" PyPI is the default public index. TestPyPI is a separate instance with its own accounts and its own copy of the namespace, intended for practice uploads. Organizations often run a private index, such as Artifactory, CodeArtifact, or devpi.
Publishing means uploading the files uv build created. The index reads the metadata inside them (name, version, dependencies, requires-python) and serves it to installers.
Pre-flight checks
Uploads are hard to undo. PyPI never accepts the same filename twice, even after you delete a release. Check these before uploading:
- The name is free. Search the index. If someone owns
weather-cli, pick another name. - The version is new. Bump
versionin[project]for every upload. - The metadata is complete. Include a
description, areadme, a license, andrequires-python. Add[project.urls]so users can find the source. - The build works from a clean state. Run the build with sources disabled, so it fails if the package depends on a
[tool.uv.sources]entry that other people cannot resolve.
[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"
dependencies = ["httpx>=0.27", "typer>=0.12"]
[project.urls]
Homepage = "https://example.com/weather-cli"
Source = "https://example.com/weather-cli/repo"
[project.scripts]
weather = "weather_cli:main"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"rm -rf dist
uv build --no-sources
ls distThe rm -rf dist step matters because uv publish uploads everything in dist/ by default. Old wheels left there from earlier versions would be sent again and rejected.
The build produces two files, for example weather_cli-0.1.1.tar.gz (the sdist) and weather_cli-0.1.1-py3-none-any.whl (the wheel). Publish both. Installers prefer the wheel, and the sdist lets other tools rebuild the package.
The publish flow
Authenticating
uv publish accepts credentials in three ways.
- An API token. Create it in your index account settings. Pass it with
--tokenor theUV_PUBLISH_TOKENenvironment variable. PyPI tokens work as a username of__token__plus the token as the password, and uv handles that for you. - A username and password. Use
--usernameand--password, orUV_PUBLISH_USERNAMEandUV_PUBLISH_PASSWORD. This is mainly for private indexes. - Trusted publishing. Your CI provider proves its identity to PyPI with a short-lived OpenID Connect token. No long-lived secret is stored anywhere.
Rehearsing on TestPyPI
Declare TestPyPI as a named index in pyproject.toml. The url is where installers look, and publish-url is where uploads go. The two differ.
[[tool.uv.index]]
name = "testpypi"
url = "https://test.pypi.org/simple/"
publish-url = "https://test.pypi.org/legacy/"
explicit = trueexplicit = true means uv uses this index only for packages that are pinned to it with tool.uv.sources. It never searches TestPyPI for your ordinary dependencies. This protects you from picking up a stray package with a familiar name.
Now upload:
export UV_PUBLISH_TOKEN="pypi-...your TestPyPI token..."
uv publish --index testpypiWithout an index entry you can pass the URL directly: uv publish --publish-url https://test.pypi.org/legacy/.
Verifying the upload
Test the release in a fresh environment that has nothing from your working directory. uvx is convenient for this:
uvx --index https://test.pypi.org/simple/ --from weather-cli==0.1.1 weatherIf the command prints "Sunny", the wheel installed, the dependencies resolved, and the entry point works. A failure here usually means a missing file in the wheel, a wrong [project.scripts] target, or a dependency you declared only as a development dependency.
TestPyPI may lack some of your dependencies or hold old, broken copies of them. When that happens, check the package contents instead. You can also install the wheel from dist/ into a scratch directory with uv run --no-project --with dist/weather_cli-0.1.1-py3-none-any.whl weather.
Publishing to PyPI
Once the rehearsal passes, upload to the real index. PyPI is the default target, so no URL is needed:
uv publish --token "$PYPI_TOKEN"Add --check-url https://pypi.org/simple/ if your CI might retry after a partial failure. uv then skips files that are already on the index instead of failing on them.
Trusted publishing from GitHub Actions
Trusted publishing replaces the stored token. On PyPI, add a "trusted publisher" for your project. You give it your GitHub repository, the workflow filename, and optionally an environment name. The workflow then needs permission to request an identity token:
name: publish
on:
push:
tags: ["v*"]
jobs:
publish:
runs-on: ubuntu-latest
environment: pypi
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v5
- run: uv build --no-sources
- run: uv publishOn GitHub Actions, uv detects trusted publishing automatically when no credentials are given. You can force it with --trusted-publishing always, or turn it off with --trusted-publishing never.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `File already exists` | That version was uploaded before | Bump `version`, rebuild, and clear `dist/` |
| `403 Forbidden` | Wrong token, or a token scoped to a different project | Create a token for this project on the correct index |
| `Invalid distribution metadata` | Missing or malformed fields, such as a bad `readme` path | Fix `pyproject.toml` and rebuild |
| Package installs but the command is missing | `[project.scripts]` points to a function that does not exist | Check `module:function` and rebuild |
| Works locally, fails for users | Build relied on a `[tool.uv.sources]` path or git entry | Build with `--no-sources` and declare real, published dependencies |
Releases are permanent
On PyPI you can yank a release, which makes installers skip it unless the version is pinned exactly. You cannot replace it. A bad release means a new version number. Treat each upload as final and use TestPyPI to catch problems first.
Exercise
You run uv publish, and the upload fails with "File already exists" for weather_cli-0.1.0-py3-none-any.whl. You fixed a bug, rebuilt, and published again without touching pyproject.toml. What went wrong, and what are the correct steps?
Show answer
Version 0.1.0 was already uploaded, and PyPI never accepts the same filename twice, so the rebuilt file is rejected. Bump version in [project] (for example to 0.1.1), delete the old files in dist/, run uv build --no-sources, and run uv publish again. Rehearsing on TestPyPI first catches this kind of mistake without consuming a real version number.
Exercise
Why is explicit = true set on the TestPyPI index entry, and what does it prevent?
Show answer
It limits the index to packages explicitly pinned to it through tool.uv.sources. Without it, uv could search TestPyPI when resolving ordinary dependencies and pick up an unrelated or untrusted package with the same name as a real one.