TExcelTAD
Home › Python Packaging: pip, venv, Poetry & uv › Building & Publishing with Poetry

Building & Publishing with Poetry

Poetry covers the whole release path: poetry version bumps the version, poetry build makes an sdist and a wheel with poetry-core, and poetry publish uploads them to PyPI or any other repository. This lesson publishes to the course's local index and installs the result like a user would.

Repositories and credentials

Poetry knows PyPI as pypi out of the box. Other upload targets are named repositories, configured once per machine:

bash
poetry config repositories.local http://127.0.0.1:8080/      # upload URL (not the /simple/ one)
poetry config http-basic.local learner learner                # username + password
poetry config pypi-token.pypi pypi-AgEIcHlwaS5vcmc...           # PyPI uses API tokens

Credentials go into the system keyring when one is available, otherwise into Poetry's auth.toml. In CI, use environment variables instead: POETRY_PYPI_TOKEN_PYPI, or POETRY_HTTP_BASIC_LOCAL_USERNAME / _PASSWORD. This container has the local repository and its credentials preconfigured:

bash · try it yourself
poetry config --list | grep -E '^repositories|^virtualenvs.in-project'
repositories.local.url = "http://127.0.0.1:8080/"
virtualenvs.in-project = true

Two different URLs are involved, and mixing them up is the classic mistake. Installing reads the Simple API (http://127.0.0.1:8080/simple/, configured per project with poetry source add); uploading posts to the repository's upload endpoint (http://127.0.0.1:8080/ here, https://upload.pypi.org/legacy/ for PyPI).

Build, publish, install

bash · try it yourself
poetry new -q tadgreet && cd tadgreet
poetry source add -q --priority=primary local http://127.0.0.1:8080/simple/
cat > src/tadgreet/__init__.py <<'PY'
def hello(name: str = "world") -> str:
    return f"Hello, {name}!"
PY
echo "# tadgreet" > README.md
poetry build
poetry publish -r local
echo "=== a user somewhere else"
cd .. && python3 -m venv user && user/bin/pip install tadgreet 2>&1 | grep -E 'Downloading|Successfully'
user/bin/python -c 'import tadgreet; print(tadgreet.hello("Poetry"))'
Creating virtualenv tadgreet in /home/learner/tadgreet/.venv
Building tadgreet (0.1.0)
Building sdist
  - Building sdist
  - Built tadgreet-0.1.0.tar.gz
Building wheel
  - Building wheel
  - Built tadgreet-0.1.0-py3-none-any.whl

Publishing tadgreet (0.1.0) to local
 - Uploading tadgreet-0.1.0.tar.gz 0%
 - Uploading tadgreet-0.1.0.tar.gz 100%
 - Uploading tadgreet-0.1.0-py3-none-any.whl 0%
 - Uploading tadgreet-0.1.0-py3-none-any.whl 100%
=== a user somewhere else
  Downloading tadgreet-0.1.0-py3-none-any.whl (1.1 kB)
Successfully installed tadgreet-0.1.0
Hello, Poetry!

poetry publish --build combines the two steps. Add --dry-run to check everything except the upload itself.

Versions are immutable

An index never lets you replace a file once uploaded, even if you delete the release afterwards (PyPI enforces this forever; pypiserver here rejects overwrites too). To fix a broken release you publish a new version:

bash · try it yourself
poetry new -q tadgreet && cd tadgreet
echo "# tadgreet" > README.md
poetry build -q && poetry publish -q -r local
echo "--- uploading 0.1.0 again"
poetry publish -r local 2>&1 | grep -oE '^Publishing.*|HTTP Error [0-9]+: [A-Za-z]+|Package [^ ]+ already exists' | sed 's/&#039;/\x27/g'
echo "--- bump and publish"
poetry version patch
rm -r dist && poetry build -q && poetry publish -r local 2>&1 | grep -v '^
  


pip index versions tadgreet
--- uploading 0.1.0 again
Publishing tadgreet (0.1.0) to local
HTTP Error 409: Conflict
Package 'tadgreet-0.1.0.tar.gz' already exists
--- bump and publish
Bumping version from 0.1.0 to 0.1.1
Publishing tadgreet (0.1.1) to local
 - Uploading tadgreet-0.1.1.tar.gz 0%
 - Uploading tadgreet-0.1.1.tar.gz 100%
 - Uploading tadgreet-0.1.1-py3-none-any.whl 0%
 - Uploading tadgreet-0.1.1-py3-none-any.whl 100%
tadgreet (0.1.1)
Available versions: 0.1.1, 0.1.0

The rejection comes from the server, and the exact message depends on it: pypiserver answers 409 Conflict (its HTML error page is trimmed here), PyPI answers 400 File already exists. poetry publish --skip-existing ignores such errors, which is useful when a CI job is re-run after a partial upload.

Before your first real release

  • Check the metadata. poetry check validates pyproject.toml. Give the project a description, a readme, a license, [project.urls] and classifiers: that's the PyPI page.
  • Look inside the wheel and sdist (unzip -l, tar tzf, as in Building & Inspecting Wheels).
  • Try TestPyPI first. poetry config repositories.testpypi https://test.pypi.org/legacy/, then poetry publish -r testpypi; see TestPyPI & Trusted Publishing.
  • Prefer trusted publishing from CI over long-lived API tokens on laptops.

Test yourself

Which URL does poetry publish -r local upload to?

  1. The Simple API URL ending in /simple/
  2. The repository's upload endpoint configured as repositories.local (for PyPI: https://upload.pypi.org/legacy/)
  3. Any URL in [[tool.poetry.source]]
  4. https://pypi.org/simple/

You uploaded version 0.1.0 with a bug. How do you fix it on PyPI?

  1. Upload 0.1.0 again
  2. Delete and re-upload 0.1.0
  3. Release a new version such as 0.1.1 (and optionally yank 0.1.0)
  4. Edit the file on the website

How should a CI job pass a PyPI token to Poetry?

  1. Commit it to pyproject.toml
  2. Through the POETRY_PYPI_TOKEN_PYPI environment variable (or better, trusted publishing)
  3. Type it interactively
  4. Put it in README.md