Part 16 / Topic 5 of 6
Versioning and publishing to PyPI
Versioning
Python packages use semantic versioning, MAJOR.MINOR.PATCH (e.g. 1.4.2):
- PATCH (
1.4.2→1.4.3): a bug fix, nothing about how you use the package changes. - MINOR (
1.4.2→1.5.0): a new feature, old code still works. - MAJOR (
1.4.2→2.0.0): something that could break existing users' code.
Bump it in pyproject.toml's version = "..." (by hand, or with uv version --bump patch/minor/major) before every release; PyPI never lets you re-upload the same version number for the same package, even if you delete it.
Building
A build turns your source into two standard files PyPI understands: a wheel (.whl, a ready-to-install zip) and an sdist (.tar.gz, the raw source, as a fallback for anything a wheel cannot cover). Build with the standard build tool, or uv build:
$ python -m build
Successfully built rupee_split-0.1.0.tar.gz and rupee_split-0.1.0-py3-none-any.whl
$ ls dist/
rupee_split-0.1.0-py3-none-any.whl
rupee_split-0.1.0.tar.gz
(python -m build needs the build package installed — pip install build — once; uv build needs nothing extra.)
Checking before you upload
twine check catches common packaging mistakes (bad metadata, a README that won't render on PyPI) before you ever touch the network:
$ python -m twine check dist/*
Checking dist/rupee_split-0.1.0-py3-none-any.whl: PASSED
Checking dist/rupee_split-0.1.0.tar.gz: PASSED
TestPyPI first
PyPI (pypi.org) is the real, public index that pip install <anything> downloads from by default. TestPyPI (test.pypi.org) is a separate, throwaway copy of it for practice — always upload there first, so a typo or a broken pyproject.toml does not permanently claim a name on the real index.
You need a free account (and a project-scoped API token, not your password — PyPI no longer accepts plain passwords for uploads) on whichever index you publish to.
# 1. upload to TestPyPI first
$ python -m twine upload --repository testpypi dist/*
# or:
$ uv publish --publish-url https://test.pypi.org/legacy/ --token <your-testpypi-token>
# 2. once it looks right on test.pypi.org, the real thing:
$ python -m twine upload dist/*
# or:
$ uv publish --token <your-pypi-token>
This part is written as a walkthrough, not a demo — it needs a real account and a real token, so nothing above was actually uploaded while writing this lesson (everything up to dist/* being built and twine check passing was run for real, as shown). twine upload --help and uv publish --help confirm these flags exist and are spelled this way on the installed versions (twine and uv's own --help output), so trust the flags; just bring your own token.
Once it is up, anyone can pip install rupee-split (or uv add rupee-split) exactly like any other package on PyPI — the same command you have used all course for requests, pytest, rich.
Read more: the official packaging tutorial.
Exercise 5: Put a shuffled uv workflow back in order
Scenario: A new teammate wrote the steps to start and publish a uv project on sticky notes, then dropped them. Put them back in order, and spot what is missing from a shorter list.
What to do:
- Write
sort_steps(steps): given a shuffled list containing some or all ofCANONICAL_ORDER's items, return them sorted into that canonical order. Build a{step: index}lookup fromCANONICAL_ORDERand sort by it. - Write
missing_steps(steps): return the items ofCANONICAL_ORDERthat are not present insteps, in canonical order. - Print
SHUFFLED, the result of sorting it, and whether that result equalsCANONICAL_ORDER. - Print
INCOMPLETEand its missing steps.
Starting data:
CANONICAL_ORDER = ["uv init", "uv add", "uv lock", "uv run", "uv build", "uv publish"]
SHUFFLED = ["uv build", "uv init", "uv run", "uv publish", "uv add", "uv lock"]
INCOMPLETE = ["uv init", "uv run", "uv build"]
Sample output:
Shuffled: ['uv build', 'uv init', 'uv run', 'uv publish', 'uv add', 'uv lock']
Back in order: ['uv init', 'uv add', 'uv lock', 'uv run', 'uv build', 'uv publish']
Matches canonical order: True
Incomplete workflow: ['uv init', 'uv run', 'uv build']
Missing steps: ['uv add', 'uv lock', 'uv publish']
💡 Hint: sorted(steps, key=lambda step: rank[step]) is the whole trick for step 1; missing_steps is a one-line list comprehension that walks CANONICAL_ORDER, not steps.
Show the solution to Exercise 5
"""Exercise 5: Put a shuffled uv workflow back in canonical order.
Run: python ex05.py (macOS/Linux: python3 ex05.py)
"""
CANONICAL_ORDER = [
"uv init",
"uv add",
"uv lock",
"uv run",
"uv build",
"uv publish",
]
SHUFFLED = [
"uv build",
"uv init",
"uv run",
"uv publish",
"uv add",
"uv lock",
]
INCOMPLETE = [
"uv init",
"uv run",
"uv build",
]
def sort_steps(steps: list[str]) -> list[str]:
rank = {step: i for i, step in enumerate(CANONICAL_ORDER)}
return sorted(steps, key=lambda step: rank[step])
def missing_steps(steps: list[str]) -> list[str]:
return [step for step in CANONICAL_ORDER if step not in steps]
def main() -> None:
ordered = sort_steps(SHUFFLED)
print("Shuffled:", SHUFFLED)
print("Back in order:", ordered)
print("Matches canonical order:", ordered == CANONICAL_ORDER)
print()
print("Incomplete workflow:", INCOMPLETE)
print("Missing steps:", missing_steps(INCOMPLETE))
if __name__ == "__main__":
main()