Python Tutorial 0/52 lessons ~6 min read Lesson 50

    Packaging & Distribution

    Ship Python code as a wheel on PyPI using pyproject.toml + build + twine (or modern uv publish, hatch publish).

    Course progress0%
    Focus
    8 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Ship Python code as a wheel on PyPI using pyproject.toml + build + twine (or modern uv publish, hatch publish).

    Understanding the topic

    Core concepts to understand:

    • pyproject.toml drives everything (PEP 621).
    • python -m build creates wheel + sdist.
    • twine upload dist/* publishes to PyPI.
    • Semver: 1.2.3 = major.minor.patch.

    Syntax reference

    Visual flow / code:

    toml
    [project]
    name = "my-lib"
    version = "0.1.0"
    description = "Cool stuff"
    requires-python = ">=3.10"
    dependencies = ["requests>=2.31"]
    [project.urls]
    Homepage = "https://github.com/me/my-lib"
    [build-system]
    requires = ["hatchling"]
    build-backend = "hatchling.build"
    # Build & publish
    $ python -m build
    $ twine upload dist/*

    Execution workflow

    1Packaging & Distribution Workflow
    1 / 4

    pyproject.toml drives everything (PEP 621)

    pyproject.toml drives everything (PEP 621).

    Real-world use

    Once published to PyPI, anyone can pip install my-lib. Major orgs run private indexes (Artifactory, AWS CodeArtifact) for internal libs.

    Best practices

    • Tag releases in git matching the version.
    • Use CI (GitHub Actions) to publish on tag.
    • Test on TestPyPI first.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1IntermediateQuestionwheel vs sdist?+

    Answer

    An sdist (source distribution, a .tar.gz) contains the source and metadata, and may need to be built on install, which can require compilers for native extensions. A wheel (.whl) is a prebuilt, zip-based format that pip just unpacks, so installs are faster and run no build code. Projects normally publish both, with platform-specific wheels for compiled packages.

    Follow-up

    What do the tags in a wheel filename mean?
    2BeginnerQuestionSemver — what changes when?+

    Answer

    Semantic Versioning uses MAJOR.MINOR.PATCH: increment MAJOR for backward-incompatible API changes, MINOR for backward-compatible new features, and PATCH for backward-compatible bug fixes. Under 1.0.0 anything may change. Python packaging itself uses PEP 440 versions, which allow pre-releases like 1.2.0rc1 and are mostly compatible with semver, though not every project follows it strictly.

    Follow-up

    How do you pin a dependency to compatible releases?
    3BeginnerQuestionWhy pyproject.toml?+

    Answer

    It is the single standard configuration file for Python projects. The [build-system] table (PEP 518) tells tools which backend builds the package, [project] (PEP 621) holds metadata and dependencies, and many tools such as pytest, ruff, and mypy keep their config under [tool.*]. It replaces executable setup.py with declarative data and makes builds reproducible across backends.

    Follow-up

    What is a build backend?

    Summary

    In summary: pyproject.toml is the source of truth. build + twine (or uv) to publish.

    Ready to mark this lesson complete?Track your journey across the entire course.