Sub topic 13
Rehearsing the release on TestPyPI
This chapter assumes you can build an sdist and a wheel with python -m build, that you have inspected them with twine check, and that you have installed the wheel in a clean virtual environment. You should also be comfortable with pip, virtual environments, and the command line.
Why rehearse a release
A PyPI release is close to permanent. You can yank a release (covered in the last chapter), but you can never upload the same filename again, and you can never reuse a version number, even after deleting the release. If you upload greeter 0.1.0 with a broken README or a missing file, that version is spent. The next fix has to be 0.1.1.
TestPyPI is a separate instance of the Python Package Index at https://test.pypi.org. It runs the same software as PyPI, with its own database, its own accounts, and its own API tokens. Uploading there lets you check that:
- the upload succeeds and the metadata is accepted,
- the project page renders your README the way you expect,
- the package installs from an index, not only from a local file.
The last point matters most. twine check and a local wheel install can pass while the real index flow still fails.
Content on TestPyPI is not guaranteed to last. The index may be wiped from time to time, so never depend on it for anything real.
Setting up credentials
You need an account on https://test.pypi.org/account/register/. Enable two-factor authentication, which both indexes require for uploads. Then create an API token under your account settings.
A new project does not exist yet, so the first token you create must have the scope "Entire account". After the first upload creates the project, create a new token limited to that one project and delete the broad one.
Twine reads credentials from a few places. In order of convenience:
- Environment variables:
TWINE_USERNAME=__token__andTWINE_PASSWORD=<your token>. - A
~/.pypircfile. - An interactive prompt.
For token authentication the username is always the literal string __token__. The password is the token itself, including the pypi- prefix.
A ~/.pypirc file with a section for TestPyPI looks like this:
[distutils]
index-servers =
pypi
testpypi
[pypi]
username = __token__
[testpypi]
repository = https://test.pypi.org/legacy/
username = __token__Leave the password out of the file. Twine then asks for it, or you can supply it through TWINE_PASSWORD. Tokens stored in plain text in a dotfile are easy to leak, for example by committing the file or by a backup that is shared.
Uploading to TestPyPI
Build fresh artifacts first. Remove any old dist/ contents so stale files are not uploaded along with the new ones.
rm -rf dist/
python -m build
python -m twine check dist/*
python -m twine upload --repository testpypi dist/*The --repository testpypi option selects the [testpypi] section from .pypirc. If you have no .pypirc, pass the URL directly with --repository-url https://test.pypi.org/legacy/.
On success, twine prints the URL of the new release, such as https://test.pypi.org/project/greeter/0.1.0/. Open it and check the following:
- The long description renders as formatted Markdown, not as raw text.
- The version, the Python requirement, and the license are correct.
- The classifiers and project links appear.
- Both files, the
.tar.gzsdist and the.whlwheel, are listed under "Download files".
Name collisions on TestPyPI
Many people use TestPyPI to try packaging, so a short name like greeter may already be taken by someone else. If the upload fails with a 403 Forbidden error that says you are not allowed to upload to the project, the name belongs to another user. You have two options:
- Pick a unique name for the rehearsal only, for example
greeter-yourname. You must then changenameinpyproject.tomlbefore building, and change it back for the real release. - Choose a distinctive name for the project from the start.
Keep in mind that a name free on TestPyPI may still be taken on PyPI, and the reverse. Check https://pypi.org/project/<name>/ for the real name before you commit to it.
Installing from TestPyPI
Install into a fresh virtual environment, so nothing left over from your development setup hides a problem.
python -m venv /tmp/testpypi-check
/tmp/testpypi-check/bin/pip install \
--index-url https://test.pypi.org/simple/ \
--extra-index-url https://pypi.org/simple/ \
greeter==0.1.0
/tmp/testpypi-check/bin/greeter AdaOn Windows, the interpreter and scripts live in Scripts\ instead of bin/.
The --extra-index-url option is there because TestPyPI usually does not host your dependencies. Without it, pip cannot find requests or any other dependency of your package, and the install fails. With it, pip searches both indexes.
Because greeter here has no runtime dependencies, you could omit the extra index. Include it whenever your dependencies list is not empty, which is the common case for real libraries.
What the rehearsal can and cannot show
The following diagram shows the stages of a release and where TestPyPI sits.
TestPyPI catches problems that appear only at the index: metadata the server rejects, a README that renders badly, a wheel that installs but lacks files that the sdist would include, or a console script that fails once installed.
It does not catch everything:
- It does not exercise your real dependency resolution against current PyPI releases of other packages, apart from the fallback index.
- It does not prove that the name is free on PyPI.
- It does not test your trusted publishing configuration. That is set up separately for each index.
Fixing a failed rehearsal
TestPyPI enforces the same rule as PyPI: a filename can be uploaded only once. If you find a mistake after uploading 0.1.0, you cannot replace the file. You have these choices:
- Bump the version for the rehearsal, such as
0.1.0.dev1,0.1.0.dev2, and so on. These are valid PEP 440 development releases, and pip ignores them by default unless you request them exactly or pass--pre. - Delete the release from the TestPyPI project page, then choose a new version. Deleting does not free the filename or version, so you still cannot reuse it.
The first option works well in practice. Rehearse with 0.1.0.dev1, fix problems, and when everything is right, change the version to 0.1.0 and upload that to both indexes. The final files you upload to PyPI should be built from the exact source you reviewed.
Common errors
| Error message or symptom | Likely cause | Fix |
|---|---|---|
| `403 Invalid or non-existent authentication information` | Wrong token, or a PyPI token used on TestPyPI | Create a token on test.pypi.org and set the username to `__token__` |
| `403 The user ... isn't allowed to upload to project` | The name is owned by someone else | Choose a different name |
| `400 File already exists` | That filename was uploaded before | Bump the version and rebuild |
| `400 Invalid value for classifiers` | A typo in a trove classifier | Compare against the list on pypi.org/classifiers |
| `No matching distribution found` on install | A dependency is missing from TestPyPI | Add `--extra-index-url https://pypi.org/simple/` |
| README shown as plain text | Missing or wrong `readme` content type | Set `readme = "README.md"` so the content type is inferred |
Exercise
You upload greeter 0.1.0 to TestPyPI and notice the README table renders incorrectly. You fix the README and run twine upload --repository testpypi dist/* again after rebuilding. The upload fails with "File already exists". Why does it fail, and what is the cleanest way to continue the rehearsal?
Show answer
TestPyPI never allows the same filename to be uploaded twice, even if the file's contents changed or the earlier upload was deleted. The rebuilt files are still named greeter-0.1.0..., so the server rejects them. Change the version to a development release such as 0.1.0.dev1 in pyproject.toml, delete the old dist/ contents, rebuild, and upload again. When the project page looks right, set the version to 0.1.0 for the real release.
Exercise
Why do you pass --extra-index-url https://pypi.org/simple/ when installing a package with dependencies from TestPyPI, and why do you also pin greeter==0.1.0?
Show answer
TestPyPI normally does not contain your dependencies, so pip needs real PyPI to find them. Because pip then looks at both indexes and picks the best match, pinning the exact version of your own package prevents it from choosing a different release of the same name from the other index.