Sub topic 13
Rehearsing the release on TestPyPI
This chapter assumes you can build a wheel and an sdist with python -m build, and that twine check passes on them. It also assumes you can install a wheel into a clean virtual environment. If not, finish those chapters first.
What TestPyPI is
TestPyPI is a separate instance of the Python Package Index at test.pypi.org. It runs the same software as PyPI but has its own database, its own accounts, and its own API tokens. Uploading there does not touch the real index. The data is also cleared from time to time, so never depend on it for anything permanent.
A rehearsal catches problems that local checks miss: a malformed README that the index refuses to render, a name that is already taken, or an upload that fails because of credentials.
Why a rehearsal matters
PyPI does not let you reuse a filename. If you upload greeter-0.1.0-py3-none-any.whl and later find a mistake, you cannot replace the file. You must publish a new version. TestPyPI lets you find mistakes before you spend a version number on the real index.
Set up the account and token
- Register separately at
test.pypi.org. A PyPI login does not carry over. - Create an API token in the TestPyPI account settings. For a new project, the scope must be "Entire account", because the project does not exist yet.
- Give twine the token. The username is the literal string
__token__, and the password is the token, including itspypi-prefix.
export TWINE_USERNAME=__token__
export TWINE_PASSWORD=pypi-AgENdGVzdC5weXBpLm9yZy...your-testpypi-tokenAlternatively, leave the variables unset and twine will prompt for them.
Upload
python -m build
python -m twine check dist/*
python -m twine upload --repository testpypi dist/*The testpypi repository name is built into twine, so no extra configuration is needed. Twine prints the project URL on success.
Install from TestPyPI
Test the upload by installing it into a fresh environment. There is one catch. TestPyPI rarely hosts your dependencies, so pip must fetch those from the real PyPI.
python -m venv /tmp/rehearsal
/tmp/rehearsal/bin/pip install \
--index-url https://test.pypi.org/simple/ \
--extra-index-url https://pypi.org/simple/ \
greeter==0.1.0
/tmp/rehearsal/bin/greeter AdaWhat to check
- The project page shows your README formatted correctly.
- The version, license, and Python requirement match
pyproject.toml. - The
greetercommand runs and printsHello, Ada!. pip installworks on a clean environment, not your development one.
Fixing a failed rehearsal
If you find a problem, change the version, rebuild, and upload again. TestPyPI enforces the same no-reuse rule, so you cannot re-upload 0.1.0. A common approach is to use a pre-release version such as 0.1.0rc1, then 0.1.0rc2, and release 0.1.0 only on the real index. Remove old files from dist/ before rebuilding so twine does not try to resend them.
Exercise
You uploaded greeter 0.1.0 to TestPyPI and noticed a typo in the README. You fix it and run twine upload again with the same version. What happens, and what should you do?
Show answer
The upload is rejected because TestPyPI does not allow a filename to be reused. Bump the version (for example to 0.1.0rc2, or 0.1.1), delete the old files in dist/, rebuild, and upload again.
Exercise
Why does the install command for the rehearsal use both --index-url and --extra-index-url?
Show answer
The package itself comes from TestPyPI, but its dependencies usually exist only on the real PyPI. The extra index lets pip resolve them there.