|
1 |
| -# The Python Package Source and Wheel Distributions |
| 1 | +# Learn about Building a Python Package |
| 2 | + |
| 3 | +:::{figure-md} build-workflow |
| 4 | +<img src="../images/python-package-development-process.png" alt="Alt tag to be added when image is final" width="700px"> |
| 5 | + |
| 6 | +You need to build your Python package in order to publish it to PyPI (or Conda). The build process organizes your code and metadata into a distribution format that can be uploaded to PyPI and subsequently downloaded and installed by users. |
| 7 | +::: |
| 8 | + |
| 9 | +## What is building a Python package? |
| 10 | + |
| 11 | +To [publish your Python package](build-workflow) and make it easy for anyone to install, you first need to build it. |
| 12 | + |
| 13 | +But, what does it mean to build a Python package? |
| 14 | + |
| 15 | +[As shown in the figure above](build-workflow), when you build your Python package, you convert the source files into something called a distribution package. A distribution package contains your source code and metadata about the package, in the format required by the Python Package Index, so that it can be installed by tools like pip. |
| 16 | + |
| 17 | +:::{note} |
| 18 | +The term package used to mean many different things in Python and other languages. On this page, we adapt the convention of the [Python Packaging Authority](https://www.pypa.io/en/latest/) and refer to the product of the |
| 19 | +build step as a **distribution package**. |
| 20 | +::: |
| 21 | + |
| 22 | +This process of organizing and formatting your |
| 23 | +code, documentation, tests and metadata into a format that both pip |
| 24 | +and PyPI can use, is called a build step. |
| 25 | + |
| 26 | +### Project metadata and PyPI |
| 27 | + |
| 28 | +The metadata that both build tools and PyPI uses to describe and understand your package is generally stored in a [pyproject.toml file](pyproject-toml-python-package-metadata). This metadata is used for several purposes: |
| 29 | + |
| 30 | +1. It helps whatever tool you use to build your package (pip, [pypa's Build](https://pypi.org/project/build/) or an end-to-end tool such as poetry, PDM or Hatch) understand how to build your package. Information it provides to your build tool includes: |
| 31 | + |
| 32 | +- The [build-system] table in your pyproject.toml file tells pip what [build backend tool](python-package-build-tools.html#build-back-ends) you wish to use for creating your sdist and wheel distributions. |
| 33 | + |
| 34 | +```toml |
| 35 | +[build-system] |
| 36 | +requires = ["hatchling"] |
| 37 | +build-backend = "hatchling.build" |
| 38 | +``` |
| 39 | + |
| 40 | +- And the dependencies section of your project table tells the build tool and PyPI what dependencies your project requires. |
| 41 | + |
| 42 | +``` |
| 43 | +dependencies = [ |
| 44 | + "numpy", |
| 45 | + "geopandas", |
| 46 | +] |
| 47 | +``` |
| 48 | + |
| 49 | +2. When the build tool creates your package distribution file (the file that you publish on PyPI), it also creates a METADATA file which PyPI can read and use to help users find your package. For example: |
| 50 | + |
| 51 | +- The `classifiers = ` section of your `[project]` table in the pyproject.toml file provides information that users on PyPI can use to filter for packages that contain specific licenses or that support specific versions of python. |
| 52 | + |
| 53 | +```toml |
| 54 | +classifiers = [ |
| 55 | + # How mature is this project? Common values are |
| 56 | + "Development Status :: 4 - Beta", |
| 57 | + |
| 58 | + # Indicate who your project is intended for |
| 59 | + "Intended Audience :: Developers", |
| 60 | + "Topic :: Software Development :: Build Tools", |
| 61 | + "License :: OSI Approved :: MIT License", |
| 62 | + "Programming Language :: Python :: 3 :: Only", |
| 63 | + "Programming Language :: Python :: 3.10", |
| 64 | + "Programming Language :: Python :: 3.11", |
| 65 | +] |
| 66 | +``` |
| 67 | + |
| 68 | +```{admonition} |
| 69 | +project metadata used to be stored in either a setup.py file or a setup.cfg file. The current recommended practice for storing package metadata is to use a pyproject.toml file. [Learn more about the pyproject.toml file here.](pyproject-toml-python-package-metadata) |
| 70 | +``` |
| 71 | + |
| 72 | +### An example - xclim |
| 73 | + |
| 74 | +When you publish to PyPI, you will notice that each package has metadata listed. Let’s have a look at [xclim](https://pypi.org/project/xclim/), one of our [pyOpenSci packages](https://www.pyopensci.org/python-packages.html). Notice that on the PyPI landing page you see some metadata about the package including python, maintainer information and more. PyPI is able to populate this metadata because it was defined using correct syntax and classifiers by Xclim's maintainers, [pyproject.toml file](https://github.com/Ouranosinc/xclim/blob/master/pyproject.toml). This metadata when the xclim package is built, is translated into a distribution file that allows PyPI to read the metadata and print it out on their website. |
| 75 | + |
| 76 | +```{figure} ../images/python-build-package/pypi-metadata-classifiers.png |
| 77 | +:scale: 50 % |
| 78 | +:align: center |
| 79 | +:alt: Image showing the left side bar of PyPI for the package xclim. The section at the top says Classifier. Below there is a list of items including Development status, intended audience, License, natural language, operating system, programming language and topic. Below each of those sections are various classifier options." width="300px"> |
| 80 | +
|
| 81 | +When you add the classifier section to your pyproject.toml |
| 82 | +and your package is built, the build tool organizes the metadata into a format that PyPI can understand and |
| 83 | +represent on your pypi landing page. These classifiers also allow users to sort through packages by version of python they support, categories and more. |
| 84 | +``` |
| 85 | + |
| 86 | +:::{figure-md} fig-target |
| 87 | +<img src="../images/python-build-package/pypi-metadata-keywords-license.png" alt="t." width="700px"> |
| 88 | + |
| 89 | +::: |
| 90 | + |
| 91 | +:::{figure-md} fig-target |
| 92 | +<img src="../images/python-build-package/pypi-metadata-maintainers.png" alt="t." width="700px"> |
| 93 | + |
| 94 | +::: |
| 95 | + |
| 96 | +## How to create the distribution format that PyPI and Pip expects? |
| 97 | + |
| 98 | +You could in theory create your own scripts to organize your code the way PyPI wants it to be. However, just like there are packages that handle known structures such as Pandas for data frames and Numpy for arrays, there are packages and tools that help you create package build distribution files. |
| 99 | + |
| 100 | +```{note} |
| 101 | +
|
| 102 | +There are a suite of packaging tools that can either help you with |
| 103 | +the entire packaging process or just one step of the process. For instance |
| 104 | +setuptools is a commonly used build back end that can be used to create your |
| 105 | +sdist and wheel. Whereas tools like Hatch, PDM, Poetry and flit help with other |
| 106 | +parts of the packaging process. |
| 107 | +
|
| 108 | +While this can cause some confusion and |
| 109 | +complexity in the packaging ecosystem - for the most part, each tool provides |
| 110 | +the same distribution output (with minor differences that most users may not |
| 111 | +care about). Learn more about those tools on this page. |
| 112 | +``` |
| 113 | + |
| 114 | +Below, you will learn about the two distribution files that PyPI expects you to publish: sdist and wheel. You will learn about |
| 115 | +their structure and what files belong in each. |
2 | 116 |
|
3 | 117 | There are two core distribution files
|
4 | 118 | that you need to create to publish your Python package to
|
5 | 119 | PyPI source distribution (often called an sdist) and wheel. The sdist contains the raw source
|
6 |
| -code for your package. The Wheel (.whl) contains the built / compiled files |
| 120 | +code for your package. The wheel (.whl) contains the built / compiled files |
7 | 121 | that can be directly installed onto anyones' computer.
|
8 | 122 |
|
9 | 123 | Learn more about both distributions below.
|
10 | 124 |
|
11 | 125 | ```{note}
|
12 | 126 | If your package is a pure python package with no additional
|
13 |
| -build / compilation steps then the sdist and Wheel distributions will have |
| 127 | +build / compilation steps then the sdist and wheel distributions will have |
14 | 128 | similar content. However if your package has extensions in other languages
|
15 | 129 | or is more complex in its build, the two distributions will be very different.
|
16 | 130 |
|
@@ -82,13 +196,13 @@ stravalib-1.1.0.post2-SDist.tar.gz file contents
|
82 | 196 |
|
83 | 197 | ```
|
84 | 198 |
|
85 |
| -```{admonition} GitHub archive vs SDist |
| 199 | +```{admonition} GitHub archive vs sdist |
86 | 200 | :class: tip
|
87 | 201 | When you make a release on GitHub, it creates a `git archive` that contains all
|
88 | 202 | of the files in your GitHub repository. While these files are similar to an
|
89 |
| -SDist, these two archives are not the same. The SDist contains a few other |
| 203 | +sdist, these two archives are not the same. The sdist contains a few other |
90 | 204 | items including a metadata directory and if you use `setuptools_scm` or `hatch_vcs`
|
91 |
| -the SDist may also contain a file that stores the version. |
| 205 | +the sdist may also contain a file that stores the version. |
92 | 206 | ```
|
93 | 207 |
|
94 | 208 | ## Wheel (.whl files):
|
|
0 commit comments