Lesson · Virtual Environments, Dependencies & Packaging
Building & Publishing Packages
65 min readBuilding & Publishing Packages
Publishing a package means building standardized artifacts, validating metadata, and uploading to an index. Wheels optimize installation while source distributions preserve a buildable source form.
Learning goals
- Tell the two artifact types apart: an sdist carries source plus build inputs, a wheel is a pre-built install target.
- Read the worked workflow (
python -m build,twine check,twine upload) and say what each step contributes. - Explain what a wheel's filename encodes and why extension modules force platform-specific wheels.
- Describe the build backend's role: a front-end tool calls it through the hooks declared in
pyproject.toml. - List the facts a distribution's metadata must carry before an index will serve it, and what
twine checkverifies.
Mental model
A build turns one source tree into standardized artifacts, and the two artifact types make different promises: the sdist says "build this here", while the wheel says "this is already built for a compatible target". The workflow separates producing artifacts from checking and publishing them.
Mental model
read it top to bottom
source tree + pyproject.toml the inputs a build needs ↓ build front end calls python -m build asks the build backend sdist (.tar.gz) buildable source for any platform ↓ or build the wheel directly one target's already-built form wheel (.whl) fast install, no build step at install time ↓ twine check, then twine upload validate metadata, then send to an index
Core idea
Building and publishing is a pipeline with a contract at each stage: the backend turns source into an sdist and a wheel, the filenames and metadata describe what a consumer can install, and an upload tool sends the checked artifacts to an index. Wheels exist because installation is faster and more predictable when the target does not have to run a build, and sdists exist because not every target can use one specific wheel. The practical skill is knowing which artifact you are shipping and which facts its metadata must carry.
Source distributions
A source distribution contains project source and metadata needed for a build. Installing it generally requires a compatible build environment on the target system.
Wheels
A wheel is a built distribution designed for fast installation without running the project's build step on the target system. Platform-specific extensions can require platform-specific wheels.
Build backends
A build backend implements standardized packaging hooks used to create distributions. Front-end tools call the backend declared in pyproject.toml.
Metadata
In a distribution, metadata is the machine-readable set of facts that travels with the artifact: the name, version, run-time dependencies, supported Python versions and the description an index displays. Build tools write it from the [project] table, and both the wheel and the sdist carry it, so installers and indexes can act without reading the source.
Console entry points
An entry point is the callable or module location where an application starts. Separating it from reusable logic makes testing and packaging easier.
Publishing workflow
In Building & Publishing Packages, Publishing workflow names a concrete behavior, API choice, or runtime rule that you should be able to recognize in code. Trace one example from input to result, record any mutation or allocation that occurs, and note the exception or fallback behavior for an invalid case. Then compare that behavior with the nearest alternative from this lesson instead of memorizing the term in isolation.
Walkthrough
python -m buildis the front end: it readspyproject.toml, prepares the declared backend, and writes both artifact types intodist/.- The commented output shows the naming pattern the two files share beneath
dist/:project-version.tar.gzfor the sdist andproject-version-*.whlfor the wheel, where the*slot holds tags such as the Python and platform identifiers. python -m twine check dist/*validates that the metadata and the long description are well formed before anything is published, and the upload step sends exactly those checked files, which is why the check is not optional.- Predict before running: which artifact types appear in
dist/, and which parts of the wheel's filename would change for a project that ships an extension module.
Experiments to try
- Build a tiny project and list
dist/; open the wheel as a zip archive and find the metadata file inside it. - Build only the sdist with
python -m build --sdist, then install from the.tar.gzwith pip and watch it build a wheel on the spot. - Point the metadata at a README file that does not exist, run
python -m twine check dist/*, and read the failure. - Build a pure-Python project and check the wheel's tag; confirm it says
py3-none-anyand explain what each part allows.
Common mistakes
- Uploading before checking:
twine checkexists because the index renders whatever description it receives, and a malformed long description becomes a broken project page that a re-upload cannot always fix. - Rebuilding between check and upload: the checked files must be the uploaded files, so any build in between invalidates the check.
- Bumping the source version but not the metadata version: the artifacts then claim a release that does not match the code inside them.
- Assuming one wheel fits every target when extension modules are involved: a wheel built for one platform and Python version is not installable elsewhere, which is why the sdist ships as the fallback.
Practice: Easy
- Build the example project, list
dist/, and match each file to sdist or wheel by reading its name. - Change the project's version in
pyproject.toml, rebuild, and confirm both artifact names changed together. - Open the wheel as a zip archive and find the metadata file that carries the name and version.
Practice: Medium
- Build only an sdist, install it into a fresh environment, and record the step pip performs that the wheel path skips.
- Run
twine checkover both artifacts, then break the description reference and run it again; explain what the second run protects. - Compare a pure-Python wheel's tag with the machine you are on and say which targets that one wheel can serve.
Practice: Hard
- Design a release checklist for a small package: version bump, build, check, upload and tag, in an order you can justify, naming the failure each step guards against.
- Build a failure lab: an upload attempt with malformed metadata, an sdist that lacks a file the tests need, and an install of a platform-specific wheel on the wrong platform; record what each consumer sees.
- Take the workflow end to end (build, check, install from both artifacts, import the package) and write down which steps a maintainer could automate and which require a decision.
Lesson checklist
- I can say what an sdist contains and when pip prefers a wheel.
- I can name the role of each step in the workflow:
build,check,upload. - I can open a wheel and point at the metadata inside it.
- I can explain why a platform tag limits which targets a given wheel can serve.
Remember
Publishing is a pipeline with artifacts at the joints: the backend builds an sdist and a wheel from one source tree, `twine check` validates what those files claim, and the upload ships exactly them. The two artifact types cover different consumers, source builders and fast installers, and the metadata inside each is what lets an index and an installer work without reading your code.
Interview angles
What is the main purpose of **Source distributions**?+
A source distribution contains project source and metadata needed for a build. Installing it generally requires a compatible build environment on the target system.
What should you pay attention to when using **Wheels**?+
A wheel is a built distribution designed for fast installation without running the project's build step on the target system. Platform-specific extensions can require platform-specific wheels.
How does **Build backends** fit into this lesson?+
A build backend implements standardized packaging hooks used to create distributions. Front-end tools call the backend declared in pyproject.toml.
Why must the artifacts uploaded to an index be the exact files that `twine check` inspected?+
The check validates the metadata and description inside those specific files; rebuilding between the check and the upload produces different bytes that were never verified, so a defect or broken description could ship unchecked.