Sub topic 14
Publishing to PyPI
This chapter assumes you can build a greeter project into dist/ with python -m build. It also assumes you have a working virtual environment and have already rehearsed the release on TestPyPI. Here you publish the same files to the real index.
What publishing does
PyPI is the Python Package Index, the default source for pip install. Publishing means uploading the sdist and the wheel from dist/ along with their metadata. Twine is the standard upload tool. It sends the files over HTTPS and refuses to start if the credentials are missing.
Before you upload
PyPI never lets you replace a file once it is uploaded. Even if you delete a release, you cannot upload the same filename again. Check these points first:
- The version in
pyproject.tomlis new and follows PEP 440. dist/holds only the files for this version. Remove old builds, or twine will try to upload them too.twine check dist/*passes.- You have installed the wheel in a clean environment and run it.
Create an account and a token
Register at pypi.org and enable two-factor authentication. Then create an API token under your account settings. For a first upload of a new project, the token must have the scope "Entire account". After the project exists, replace it with a token limited to that project.
Twine reads credentials from environment variables, so nothing is stored in the repository:
export TWINE_USERNAME="__token__"
export TWINE_PASSWORD="pypi-AgEIcHlwaS5vcmc..."The username is the literal string __token__. The password is the token, which starts with pypi-. Never commit it.
Upload
python -m twine upload dist/*Twine prints each file as it uploads. When it finishes, the project page appears at https://pypi.org/project/greeter/. Replace greeter with your own project name. The name must be unique on PyPI, and the first upload claims it.
Verify the release
Install from the real index into a fresh environment, not from your local dist/ folder:
python -m venv /tmp/check
/tmp/check/bin/pip install greeter==0.1.0
/tmp/check/bin/greeter AdaThe output should be Hello, Ada!. If pip cannot find the version yet, wait a minute and retry, because the index can lag briefly.
Common errors
| Message | Cause | Fix |
|---|---|---|
| `403 Invalid or non-existent authentication information` | Wrong username or an expired token | Use `__token__` as the username and create a new token |
| `400 File already exists` | That version was uploaded before | Increase the version, rebuild, and upload again |
| `400 The name is too similar to an existing project` | The name is taken or too close to another | Choose a different `name` in `[project]` |
| `InvalidDistribution` from twine | Metadata is malformed, often the readme | Run `twine check dist/*` and fix what it reports |
Exercise
You uploaded version 0.1.0, then noticed a typo in the README. You fix the file and run twine upload dist/* again without changing anything else. What happens, and what should you do instead?
Show answer
PyPI rejects the upload with "File already exists", because a filename can never be reused, even for the same version. Change the version (for example to 0.1.1, or a post-release such as 0.1.0.post1), rebuild so dist/ holds only the new files, and upload again.