TExcelTAD
Home › Python Packaging: pip, venv, Poetry & uv › Mini Project: Publish a CLI with Poetry & uv

Mini Project: Publish a CLI with Poetry & uv

Time to put the whole course together. You'll build tadwords, a small library with a command-line tool, and ship it twice: once with Poetry and once with uv. Each time you'll test it, build an sdist and a wheel, inspect them, publish them to the local index, and finally install the package into a fresh environment like a stranger would, and run the command.

The specification

  • Package tadwords, src layout, requires-python = ">=3.13".
  • Library API: tadwords.stats(text) -> dict returning words, unique and top (the most common word, ties broken alphabetically).
  • A console script tadwords built with click (from the index) that reads text from its arguments and prints the stats.
  • Tests with pytest in a dev group, never shipped to users.
  • The version is read at run time from the installed metadata (importlib.metadata), so there's one source of truth: pyproject.toml.
  • Published to http://127.0.0.1:8080/ as user learner.

The two sessions below are independent (each starts in an empty container, with an empty index), so each one does the full journey.

Part 1: with Poetry

bash · try it yourself
poetry new -q tadwords && cd tadwords
poetry source add -q --priority=primary local http://127.0.0.1:8080/simple/
poetry add -q "click>=8.1"
poetry add -q --group dev pytest
cat > src/tadwords/__init__.py <<'PY'
"""Tiny text statistics."""
from collections import Counter
from importlib.metadata import version

__version__ = version("tadwords")

def stats(text: str) -> dict:
    words = [w.strip(".,!?;:").lower() for w in text.split()]
    words = [w for w in words if w]
    counts = Counter(words)
    top = min(counts, key=lambda w: (-counts[w], w)) if counts else None
    return {"words": len(words), "unique": len(counts), "top": top}
PY
cat > src/tadwords/cli.py <<'PY'
import click
from tadwords import __version__, stats

@click.command()
@click.version_option(__version__, prog_name="tadwords")
@click.argument("text", nargs=-1, required=True)
def main(text: tuple[str, ...]) -> None:
    """Print word statistics for TEXT."""
    s = stats(" ".join(text))
    click.echo(f"words={s['words']} unique={s['unique']} top={s['top']}")
PY
cat > tests/test_stats.py <<'PY'
from tadwords import stats

def test_counts():
    assert stats("the cat and the hat") == {"words": 5, "unique": 4, "top": "the"}

def test_ties_and_punctuation():
    assert stats("B, a! b a") == {"words": 4, "unique": 2, "top": "a"}

def test_empty():
    assert stats("") == {"words": 0, "unique": 0, "top": None}
PY
cat >> pyproject.toml <<'TOML'

[project.scripts]
tadwords = "tadwords.cli:main"
TOML
poetry install -q
echo "=== test"
poetry run pytest -q
echo "=== build and inspect"
poetry build -q
ls dist
unzip -Z1 dist/tadwords-0.1.0-py3-none-any.whl
unzip -p dist/tadwords-0.1.0-py3-none-any.whl tadwords-0.1.0.dist-info/METADATA | grep -E '^(Name|Version|Requires)'
echo "=== publish"
poetry publish -r local 2>&1 | grep -E 'Publishing|100%'
echo "=== a stranger installs it into a fresh venv"
cd /tmp && python3 -m venv fresh && fresh/bin/pip install tadwords 2>&1 | grep Successfully
fresh/bin/tadwords --version
fresh/bin/tadwords "The cat sat on the mat, the end."
fresh/bin/pip list --format=freeze | grep -iE 'pytest|tadwords|click' 
=== test
...                                                                      [100%]
3 passed in 0.02s
=== build and inspect
tadwords-0.1.0-py3-none-any.whl
tadwords-0.1.0.tar.gz
tadwords/__init__.py
tadwords/cli.py
tadwords-0.1.0.dist-info/METADATA
tadwords-0.1.0.dist-info/WHEEL
tadwords-0.1.0.dist-info/entry_points.txt
tadwords-0.1.0.dist-info/RECORD
Name: tadwords
Version: 0.1.0
Requires-Python: >=3.13
Requires-Dist: click (>=8.1)
=== publish
Publishing tadwords (0.1.0) to local
 - Uploading tadwords-0.1.0.tar.gz 100%
 - Uploading tadwords-0.1.0-py3-none-any.whl 100%
=== a stranger installs it into a fresh venv
Successfully installed click-8.5.0 tadwords-0.1.0
tadwords, version 0.1.0
words=8 unique=6 top=the
click==8.5.0
tadwords==0.1.0

Check the results against the specification:

  • three tests passed;
  • the wheel contains only the package and its .dist-info: no tests, no .venv;
  • METADATA requires click but not pytest (the dev group isn't published);
  • the fresh environment got tadwords and click, no pytest, and the tadwords launcher works, including --version read from metadata.

Part 2: with uv

The same project, managed by uv. uv init --package gives the src layout, a console script and the uv_build backend; we point the script at our cli module.

bash · try it yourself
uv init -q --package tadwords && cd tadwords
uv add -q "click>=8.1"
uv add -q --dev pytest
sed -i 's/^tadwords = "tadwords:main"/tadwords = "tadwords.cli:main"/' pyproject.toml
uv version -q 0.2.0
mkdir -p tests
cat > src/tadwords/__init__.py <<'PY'
"""Tiny text statistics."""
from collections import Counter
from importlib.metadata import version

__version__ = version("tadwords")

def stats(text: str) -> dict:
    words = [w.strip(".,!?;:").lower() for w in text.split()]
    words = [w for w in words if w]
    counts = Counter(words)
    top = min(counts, key=lambda w: (-counts[w], w)) if counts else None
    return {"words": len(words), "unique": len(counts), "top": top}
PY
cat > src/tadwords/cli.py <<'PY'
import click
from tadwords import __version__, stats

@click.command()
@click.version_option(__version__, prog_name="tadwords")
@click.argument("text", nargs=-1, required=True)
def main(text: tuple[str, ...]) -> None:
    """Print word statistics for TEXT."""
    s = stats(" ".join(text))
    click.echo(f"words={s['words']} unique={s['unique']} top={s['top']}")
PY
cat > tests/test_stats.py <<'PY'
from tadwords import stats

def test_counts():
    assert stats("the cat and the hat") == {"words": 5, "unique": 4, "top": "the"}

def test_ties_and_punctuation():
    assert stats("B, a! b a") == {"words": 4, "unique": 2, "top": "a"}
PY
echo "=== test (uv run syncs first)"
uv run -q pytest -q
uv lock --check
echo "=== build and inspect"
uv build -q
ls dist
unzip -Z1 dist/tadwords-0.2.0-py3-none-any.whl
echo "=== publish"
uv publish -q --publish-url http://127.0.0.1:8080/ -u learner -p learner && echo "published 0.2.0"
pip index versions tadwords
echo "=== a stranger installs it, three ways"
cd /tmp
python3 -m venv fresh && fresh/bin/pip install -q tadwords && fresh/bin/tadwords --version
uv tool install -q tadwords 2>/dev/null && ~/.local/bin/tadwords "to be or not to be"
uvx tadwords "uv runs it without installing" 2>/dev/null
=== test (uv run syncs first)
..                                                                       [100%]
2 passed in 0.02s
Resolved 8 packages in 1ms
=== build and inspect
tadwords-0.2.0-py3-none-any.whl
tadwords-0.2.0.tar.gz
tadwords/
tadwords/__init__.py
tadwords/cli.py
tadwords-0.2.0.dist-info/
tadwords-0.2.0.dist-info/WHEEL
tadwords-0.2.0.dist-info/entry_points.txt
tadwords-0.2.0.dist-info/METADATA
tadwords-0.2.0.dist-info/RECORD
=== publish
published 0.2.0
tadwords (0.2.0)
Available versions: 0.2.0
=== a stranger installs it, three ways
tadwords, version 0.2.0
words=6 unique=4 top=be
words=5 unique=5 top=installing

Part 3: consume it from another project

A library is only really published when another project can depend on it. This session publishes tadwords again (fresh container), then creates an application that depends on it through the index and locks it:

bash · try it yourself
uv init -q --lib tadwords && cd tadwords
cat > src/tadwords/__init__.py <<'PY'
def stats(text: str) -> dict:
    words = text.split()
    return {"words": len(words), "unique": len(set(words))}
PY
uv build -q && uv publish -q --publish-url http://127.0.0.1:8080/ -u learner -p learner
cd .. && uv init -q --no-package report && cd report
uv add "tadwords>=0.1" 2>&1 | grep -E 'Resolved|tadwords'
grep -A6 '^name = "tadwords"' uv.lock | sed 's/sha256:[0-9a-f]*/sha256:…/'
cat > main.py <<'PY'
from tadwords import stats
print(stats("ship it ship it now"))
PY
uv run main.py
Resolved 2 packages in 154ms
 + tadwords==0.1.0
name = "tadwords"
version = "0.1.0"
source = { registry = "http://127.0.0.1:8080/simple/" }
sdist = { url = "http://127.0.0.1:8080/packages/tadwords-0.1.0.tar.gz", hash = "sha256:…" }
wheels = [
    { url = "http://127.0.0.1:8080/packages/tadwords-0.1.0-py3-none-any.whl", hash = "sha256:…" },
]
{'words': 5, 'unique': 3}

The application's uv.lock now pins tadwords 0.1.0 from the local index, with the hash of the exact wheel you uploaded.

Extend it

  1. Add a --json flag to the CLI and a test for it; bump the version with poetry version minor / uv version --bump minor and publish again. What happens if you forget to bump?
  2. Add an extra fancy = ["rich>=13"] that prints a table when rich is installed; check Provides-Extra in METADATA and install tadwords[fancy] into a fresh venv.
  3. Write the GitHub Actions workflow from TestPyPI & Trusted Publishing for this project, with a test job on Python 3.13 that runs uv sync --locked and uv run pytest.
  4. Test the lower bound: run the uv project's tests with uv run --resolution lowest-direct pytest -q. Which click version does it use?

Test yourself

Why didn't pytest end up in the stranger's fresh environment?

  1. pip skipped it by accident
  2. It was in a dependency group, which isn't part of the published metadata
  3. pytest isn't on the index
  4. Poetry removed it during build

Where does tadwords --version get its version number?

  1. A hard-coded string in cli.py
  2. The installed distribution's metadata via importlib.metadata.version, which came from pyproject.toml
  3. From git tags
  4. From PyPI

What does the consuming application's uv.lock record about tadwords?

  1. Only the name
  2. The exact version, the index it came from and the hash of the wheel
  3. The source code
  4. The publisher's password