Publishing with twine & uv publish
Publishing means uploading your sdist and wheel to an index with the upload API that PyPI defined (a multipart HTTP POST to https://upload.pypi.org/legacy/). Any tool can do it: twine is PyPA's dedicated uploader, uv publish and poetry publish are built in to their project managers. Private indexes (pypiserver, devpi, Artifactory, Nexus, GitLab, AWS CodeArtifact, Google Artifact Registry) speak the same API, so the commands are the same with a different URL.
Check before you upload
twine check validates the metadata, in particular that the long description (your README) will render on PyPI:
uv init -q --lib tadcalc && cd tadcalc
printf '# tadcalc\n\nTiny calculator helpers.\n' > README.md
uv build -q
ls dist
twine check dist/* 2>&1 | sed 's/ *$//'tadcalc-0.1.0-py3-none-any.whl
tadcalc-0.1.0.tar.gz
Checking dist/tadcalc-0.1.0-py3-none-any.whl: PASSED
Checking dist/tadcalc-0.1.0.tar.gz: PASSED
twine upload
twine reads repository settings from ~/.pypirc (or --repository-url, or the TWINE_REPOSITORY_URL/TWINE_USERNAME/TWINE_PASSWORD variables). This container's ~/.pypirc defines a repository named local:
cat ~/.pypirc
uv init -q --lib tadcalc && cd tadcalc
uv build -q
twine upload -r local --disable-progress-bar dist/*
echo "=== a user installs it"
cd .. && python3 -m venv user && user/bin/pip install tadcalc 2>&1 | grep -E 'Downloading|Successfully'
user/bin/python -c 'import tadcalc; print(tadcalc.hello())'[distutils]
index-servers =
local
[local]
repository = http://127.0.0.1:8080/
username = learner
password = learner
Uploading distributions to http://127.0.0.1:8080/
Uploading tadcalc-0.1.0-py3-none-any.whl
Uploading tadcalc-0.1.0.tar.gz
=== a user installs it
Downloading tadcalc-0.1.0-py3-none-any.whl (1.4 kB)
Successfully installed tadcalc-0.1.0
Hello from tadcalc!
For PyPI the file looks like this; with an API token the username is literally __token__:
[distutils]
index-servers =
pypi
testpypi
[pypi]
username = __token__
password = pypi-AgEIcHlwaS5vcmc...
[testpypi]
repository = https://test.pypi.org/legacy/
username = __token__
password = pypi-AgENdGVzdC5weXBp...A plaintext token in your home directory is convenient and risky; prefer a keyring (keyring set https://upload.pypi.org/legacy/ __token__), environment variables in CI, or better, trusted publishing.
uv publish
uv publish uploads everything in dist/ by default. Credentials come from flags, UV_PUBLISH_USERNAME/UV_PUBLISH_PASSWORD/UV_PUBLISH_TOKEN, or trusted publishing:
uv init -q --lib tadcalc && cd tadcalc
uv version --bump minor 2>&1 | tail -1
uv build -q
uv publish --publish-url http://127.0.0.1:8080/ -u learner -p learner 2>&1
pip index versions tadcalctadcalc 0.1.0 => 0.2.0
Publishing 2 files to http://127.0.0.1:8080/
Hashing tadcalc-0.2.0-py3-none-any.whl (1.4KiB)
Uploading tadcalc-0.2.0-py3-none-any.whl (1.4KiB)
Hashing tadcalc-0.2.0.tar.gz (904B)
Uploading tadcalc-0.2.0.tar.gz (904B)
tadcalc (0.2.0)
Available versions: 0.2.0
uv hashes each file and uploads it. Against PyPI, uv publish with no URL targets PyPI; a named index in pyproject.toml ([[tool.uv.index]] with publish-url) lets you write uv publish --index internal.
Uploading the same version twice
Every file on an index is immutable. A second upload of tadcalc-0.1.0 is rejected: pypiserver answers a bare 400 Bad Request here, PyPI answers 400 File already exists:
uv init -q --lib tadcalc && cd tadcalc
uv build -q
twine upload -r local --disable-progress-bar dist/* > /dev/null
twine upload -r local --disable-progress-bar dist/* 2>&1 | sed 's/ *$//'
echo "--- uv publish checks first"
uv publish --publish-url http://127.0.0.1:8080/ -u learner -p learner --check-url http://127.0.0.1:8080/simple/ 2>&1Uploading distributions to http://127.0.0.1:8080/
Uploading tadcalc-0.1.0-py3-none-any.whl
WARNING Error during upload. Retry with the --verbose option for more details.
ERROR HTTPError: 400 Bad Request from http://127.0.0.1:8080/
Bad Request
--- uv publish checks first
Publishing 2 files to http://127.0.0.1:8080/
File tadcalc-0.1.0-py3-none-any.whl already exists, skipping
File tadcalc-0.1.0.tar.gz already exists, skipping
With --check-url (or a configured index), uv first looks at the index, sees identical files are already there and skips them instead of failing. That makes a re-run of a half-finished release job safe. twine's --skip-existing does the same against PyPI, but only for servers it recognises; pypiserver isn't one of them.
Yanking instead of deleting
If a release is broken, don't delete it (people may have pinned it, and the version can never be reused anyway). Yank it on PyPI's web UI (PEP 592): resolvers then skip it unless a requirement pins that exact version (==1.4.0), and pip prints a warning with your yank reason when it installs it. Then publish a fixed version.
Test yourself
What username do you use when uploading to PyPI with an API token?
- Your PyPI username
- __token__
- pypi
- token
Why is re-running a release job with uv publish --check-url … safe?
- It deletes the old files first
- uv checks the index and skips files that are already uploaded with the same content
- It bumps the version
- It uploads to TestPyPI
A release is broken. What's the right move on PyPI?
- Delete it and re-upload the same version
- Yank it (resolvers skip it unless pinned exactly) and publish a new version
- Upload a file with the same name
- Ask users to use --force