Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding a "packaging flow" overview document #1100

Merged
merged 33 commits into from Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8e7ba62
add initial translation of the flow document from the MarkDown original
cameron-simpson Jun 27, 2022
93e0b22
source/index.rst: add the flow to the toctree
cameron-simpson Jun 28, 2022
4d7282c
source/flow.rst: minor formatting and typo fixes
cameron-simpson Jun 28, 2022
d169cf7
flow.rst: some suggestions from @ abravalheri
cameron-simpson Jun 28, 2022
ae3c2a3
flow.rst: small clarification that this is about distribution package…
cameron-simpson Jun 28, 2022
1a14289
undo previous commit - this isn't just about distribution packages
cameron-simpson Jun 28, 2022
2322aa6
flow.rst: I was wrong, restoring @sinoroc's suggestion
cameron-simpson Jun 29, 2022
7ea231f
flow.rst: The Configuration File: be less prescriptive - suggestion b…
cameron-simpson Jun 30, 2022
34a91e9
flow.rst: The Build System: small elaboration suggested by @pfmoore
cameron-simpson Jun 30, 2022
1f18e29
flow.rst: use :pep: roles for PEP references
cameron-simpson Jun 30, 2022
d96323f
flow.rst: convert in tree refs to :doc:
cameron-simpson Jun 30, 2022
a9161aa
flow.rst: small fixups
cameron-simpson Jun 30, 2022
990302e
Merge branch 'main' into cs-packaging-flow
cameron-simpson Jul 8, 2022
28d9092
Lots of copy edits and TODOs
bhrutledge Jul 21, 2022
bcdf5df
Set 78 character line length
bhrutledge Jul 21, 2022
df3eb16
Condense build-system description
bhrutledge Jul 21, 2022
962f464
Tweak project links
bhrutledge Jul 21, 2022
9234fd2
Merge branch 'main' into cs-packaging-flow
bhrutledge Jul 21, 2022
9b8c413
flow.rst: restore mention of "pip" in the end user steps
cameron-simpson Jul 26, 2022
f14c2cd
flow.rst: no, the PEP links are indeed not useful; I've mentioned a P…
cameron-simpson Jul 26, 2022
17cf3e5
flow.rst: replace tool links with references to the key projects file
cameron-simpson Jul 26, 2022
9ff8f37
flow.rst: mention build frontends and backends
cameron-simpson Jul 26, 2022
d9e1c27
flow.rst: mention all 3 of setuptools, flit and hatchling
cameron-simpson Jul 26, 2022
fe0b053
flow.rst: add a paragraph about end user systems how wheel tags can d…
cameron-simpson Jul 26, 2022
ac8894f
Reformat and add TODOs
bhrutledge Jul 27, 2022
f72c3d7
Link more key projects
bhrutledge Jul 28, 2022
72a5a4f
Rework wheel section
bhrutledge Jul 28, 2022
cb23f4e
Rework frontend and backend
bhrutledge Jul 28, 2022
d8b49af
Simplify example build-system
bhrutledge Jul 28, 2022
bb737e8
Always use ref for tools
bhrutledge Jul 28, 2022
68eb42e
Link other install tools
bhrutledge Jul 28, 2022
0e08f5f
Tweak pip reference
bhrutledge Jul 28, 2022
ae1a164
Merge branch 'main' into cs-packaging-flow
bhrutledge Jul 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
177 changes: 177 additions & 0 deletions source/flow.rst
@@ -0,0 +1,177 @@
==================
The Packaging Flow
==================

The document aims to outline the flow involved in publishing/distributing a
:term:`distribution package <Distribution Package>`, usually to the `Python
Package Index (PyPI)`_. It is written for package publishers, who are assumed
to be the package author.

.. _Python Package Index (PyPI): https://pypi.org/

While the :doc:`tutorial <tutorials/packaging-projects>` walks through the
process of preparing a simple package for release, it does not fully enumerate
what steps and files are required, and for what purpose.

Publishing a package requires a flow from the author's source code to an end
user's Python environment. The steps to achieve this are:

- Have a source tree containing the package. This is typically a checkout from
a version control system (VCS).

- Prepare a configuration file describing the package metadata (name, version
and so forth) and how to create the build artifacts. For most packages, this
will be a :file:`pyproject.toml` file, maintained maunally in the source
tree.

- Create build artifacts to be sent to the package distribution service
(usually PyPI); these will normally be a
:term:`source distribution ("sdist") <Source Distribution (or "sdist")>`
and one or more :term:`built distributions ("wheels") <Built Distribution>`.
These are made by a build tool using the configuration file from the
previous step. Often there is just one generic wheel for a pure Python
package.

- Upload the build artifacts to the package distribution service.

At that point, the package is present on the package distribution service.
To use the package, end users must:

- Download one of the package's build artifacts from the package distribution
service.

- Install it in their Python environment, usually in its ``site-packages``
directory. This step may involve a build/compile step which, if needed, must
be described by the package metadata.

These last 2 steps are typically performed by :ref:`pip` when an end user runs
``pip install``.
bhrutledge marked this conversation as resolved.
Show resolved Hide resolved

The steps above are described in more detail below.

The source tree
===============

The source tree contains the package source code, usually a checkout from a
VCS. The particular version of the code used to create the build artifacts
will typically be a checkout based on a tag associated with the version.

The configuration file
======================

The configuration file depends on the tool used to create the build artifacts.
The standard practice is to use a :file:`pyproject.toml` file in the `TOML
format`_.

.. _TOML format: https://github.com/toml-lang/toml

At a minimum, the :file:`pyproject.toml` file needs a ``[build-system]`` table
specifying your build tool. There are many build tools available, including
but not limited to :ref:`flit`, :ref:`hatch`, :ref:`pdm`, :ref:`poetry`,
:ref:`setuptools`, `trampolim`_, and `whey`_. Each tool's documentation will
show what to put in the ``[build-system]`` table.

.. _trampolim: https://pypi.org/project/trampolim/
.. _whey: https://pypi.org/project/whey/

For example, here is a table for using :ref:`hatch`:

.. code-block:: toml

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

With such a table in the :file:`pyproject.toml` file, a "frontend" tool like
:ref:`build` can run your chosen build tool's "backend" to create the build
artifacts. Your build tool may also provide its own frontend. An install tool
like :ref:`pip` also acts as a frontend when it runs your build tool's backend
to install from a source distribution.

The particular build tool you choose dictates what additional information is
required in the :file:`pyproject.toml` file. For example, you might specify:

* a ``[project]`` table containing project
:doc:`Core Metadata </specifications/core-metadata/>`
(name, version, author and so forth); see
:doc:`Declaring project metadata </specifications/declaring-project-metadata/>`
for more detail

* a ``[tool]`` table containing tool-specific configuration options

Build artifacts
===============

The source distribution (sdist)
-------------------------------

A source distribution contains enough to install the package from source in an
end user's Python environment. As such, it needs the package source, and may
also include tests and documentation. These are useful for end users wanting
to develop your sources, and for end user systems where some local compilation
step is required (such as a C extension).

The :ref:`build` package knows how to invoke your build tool to create one of
these:

.. code-block:: bash

python3 -m build --sdist source-tree-directory

Or, your build tool may provide its own interface for creating an sdist.


The built distributions (wheels)
--------------------------------

A built distribution contains only the files needed for an end user's Python
environment. No compilation steps are required during the install, and the
wheel file can simply be unpacked into the ``site-packages`` directory. This
makes the install faster and more convenient for end users.

A pure Python package typically needs only one "generic" wheel. A package with
compiled binary extensions needs a wheel for each supported combination of
Python interprerter, operating system, and CPU architecture that it supports.
If a suitable wheel file is not available, tools like :ref:`pip` will fall
back to installing the source distribtion.

The :ref:`build` package knows how to invoke your build tool to create one of
these:

.. code-block:: bash

python3 -m build --wheel source-tree-directory

Or, your build tool may provide its own interface for creating a wheel.

.. note::

The default behaviour of :ref:`build` is to make both an sdist and a wheel
from the source in the current directory; the above examples are
deliberately specific.

Upload to the package distribution service
==========================================

The :ref:`twine` tool can upload build artifacts to PyPI for distribution,
using a command like:

.. code-block:: bash

twine upload dist/package-name-version.tar.gz dist/package-name-version-py3-none-any.whl

Or, your build tool may provide its own interface for uploading.

Download and install
====================

Now that the package is published, end users can download and install the
package into their Python environment. Typically this is done with :ref:`pip`,
using a command like:

.. code-block:: bash

python3 -m pip install package-name

End users may also use other tools like :ref:`pipenv`, :ref:`poetry`, or
:ref:`pdm`.
3 changes: 3 additions & 0 deletions source/index.rst
Expand Up @@ -11,6 +11,7 @@ Python Packaging User Guide
:hidden:

overview
flow
tutorials/index
guides/index
discussions/index
Expand All @@ -37,6 +38,8 @@ Get started
Essential tools and concepts for working within the Python
development ecosystem are covered in our :doc:`tutorials/index` section:

* To get an overview of the flow used to publish your code, see the
:doc:`packaging flow <flow>`
* To learn how to install packages, see the
:doc:`tutorial on installing packages <tutorials/installing-packages>`
* To learn how to manage dependencies in a version controlled project, see the
Expand Down