Learn / Advanced / Lesson 7

Packaging and publishing

12 min Tested on Python 3.15Updated By
In short

A Python package is described by a pyproject.toml file: name, version, dependencies and how to build it. uv build turns your project into a wheel (.whl) that anyone can install with pip install, and uv publish uploads it to PyPI. Entry points let users run your tool as a normal command.

Project layout

layout
slugkit/
├── pyproject.toml
├── README.md
├── src/
│   └── slugkit/
│       ├── __init__.py
│       └── core.py
└── tests/
    └── test_core.py
src/slugkit/core.py
import re


def slugify(text: str) -> str:
    """Turn any title into a URL-friendly slug."""
    text = re.sub(r"[^a-z0-9]+", "-", text.lower())
    return text.strip("-")


def main() -> None:
    import sys
    print(slugify(" ".join(sys.argv[1:])))
src/slugkit/__init__.py
from slugkit.core import slugify

__all__ = ["slugify"]
__version__ = "0.1.0"

pyproject.toml

pyproject.toml
[project]
name = "slugkit"
version = "0.1.0"
description = "Turn titles into URL slugs"
readme = "README.md"
requires-python = ">=3.10"
authors = [{ name = "Your Name", email = "you@example.com" }]
license = "MIT"
dependencies = []

[project.scripts]
slugkit = "slugkit.core:main"

[dependency-groups]
dev = ["pytest>=8"]

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
  • [project] holds the metadata PyPI shows: name, version, description, Python versions.
  • dependencies lists what users need installed; keep version ranges loose for libraries.
  • [project.scripts] creates a slugkit command that runs main().
  • [dependency-groups] holds tools only you need while developing, like pytest.
  • [build-system] says which tool builds the package; Hatchling is a common default.

Try it locally

terminal
$ uv sync                      # create .venv and install the project
$ uv run slugkit "Hello, Python World!"
hello-python-world
$ uv run pytest -q
.                                                    [100%]
1 passed

Build and publish

terminal
$ uv build
Building source distribution...
Building wheel from source distribution...
Successfully built dist/slugkit-0.1.0.tar.gz
Successfully built dist/slugkit-0.1.0-py3-none-any.whl

$ uv publish --publish-url https://test.pypi.org/legacy/   # practise on TestPyPI first
$ uv publish                                               # the real PyPI

Versions

Most packages use semantic versioning, MAJOR.MINOR.PATCH: bump PATCH for bug fixes, MINOR for new features that don't break anything, and MAJOR for breaking changes. A version can never be re-uploaded to PyPI, so every release needs a new number.

Practice

Package tools compare versions as numbers, not text. Write parse_version(v: str) -> tuple[int, ...] that turns "3.15.0" into (3, 15, 0). Then print "3.9.1" < "3.15.0" as plain strings, the same comparison with your function, and the newest of ["0.9.0", "0.10.2", "0.10.10"] using max(..., key=parse_version).

Expected output:
False
True
0.10.10
Your code

Quick check

1. Where do you list a package's runtime dependencies?

2. What does [project.scripts] create?

3. You fixed a bug without changing features. Version 1.4.2 becomes...

Use AI at work

Python is the base of AI. On AIMates you learn to use AI tools well in your daily job.

Visit AIMates →