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:
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 tokensCredentials 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:
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
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:
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/'/\x27/g'
echo "--- bump and publish"
poetry version patch
rm -r dist && poetry build -q && poetry publish -r local 2>&1 | grep -v '^