Skip to content

Commit 3433f99

Browse files
committed
Add build clarification to package guide
1 parent 3441de2 commit 3433f99

File tree

6 files changed

+90
-0
lines changed

6 files changed

+90
-0
lines changed
120 KB
Loading
60.5 KB
Loading
102 KB
Loading
200 KB
Loading

package-structure-code/python-package-build-tools.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
# Python Package Build Tools
22

3+
## What is building a Python package and why do we do it?
4+
5+
In Python, if you want to publish your code in a way that can be
6+
installed by both yourself and others, your code, tests and associated
7+
metadata need to be organized in a specific way. This specific
8+
organization and structure is important because it's the structure
9+
that both `PyPI` and any installer that you use like `pip` can
10+
understand and parse. This process of organizing and formatting your
11+
code, documentation, tests and metadata into a format that both pip
12+
and PyPI can use, is called a build step.
13+
14+
For instance, 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.
15+
16+
## How to create the distribution format that PyPI and Pip expects?
17+
18+
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.
19+
20+
```{note}
21+
22+
BREAKOUT: there are a suite of packaging tools that can either help you with the entire packaging process or just one step of the process. For instance setuptools is a commonly used build back end that can be used to create your SDist and wheel. Whereas tools like Hatch, PDM, Poetry and flit help with other parts of the packaging process. While this can cause some confusion and complexity in the packaging ecosystem - for the most part, each tool provides the same distribution output (with minor differences that most users may not care about). Learn more about those tools on this page.
23+
```
24+
25+
Below, you will learn about the two distribution files that PyPI expects you to publish: SDist and Wheel. You will learn about
26+
their structure and what files belong in each.
27+
328
<!-- TODO: add a small discussion on what pinning is?-->
429

30+
## Tools for building your package
31+
532
There are a several different build tools that you can use to [create your Python package's _sdist_ and _wheel_ distributions](python-package-distribution-files-sdist-wheel). Below, we discuss the features,
633
benefits and limitations of the most commonly used Python packaging tools.
734
We focus on pure-python packages in this guide. However, we also

package-structure-code/python-package-distribution-files-sdist-wheel.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,68 @@
11
# The Python Package Source and Wheel Distributions
22

3+
```{figure} ../images/python-package-development-process.png
4+
:align: center
5+
:alt: Image showing the left side bar of PiPy 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">
6+
7+
Notice the metadata printed on the PyPI page for xclim. When you add the classifier section to your pyproject.toml
8+
and your package is built, the build tool organizes the metadata into a format that PyPI can understand and
9+
represent on your pypi landing page. These classifiers also allow users to sort through packages by version of python they support, categories and more.
10+
```
11+
12+
## What is building a Python package?
13+
14+
In Python, if you want to publish your code in a way that can be
15+
installed by both yourself and others, your code, tests and associated
16+
metadata need to be organized in a specific way. This specific
17+
organization and structure is important because it's the structure
18+
that both `PyPI` and any installer that you use like `pip` can
19+
understand and parse. This process of organizing and formatting your
20+
code, documentation, tests and metadata into a format that both pip
21+
and PyPI can use, is called a build step.
22+
23+
For instance, 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.
24+
25+
```{figure} ../images/python-build-package/pypi-metadata-classifiers.png
26+
:scale: 50 %
27+
:align: center
28+
:alt: Image showing the left side bar of PiPy 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">
29+
30+
When you add the classifier section to your pyproject.toml
31+
and your package is built, the build tool organizes the metadata into a format that PyPI can understand and
32+
represent on your pypi landing page. These classifiers also allow users to sort through packages by version of python they support, categories and more.
33+
```
34+
35+
:::{figure-md} fig-target
36+
<img src="../images/python-build-package/pypi-metadata-keywords-license.png" alt="t." width="700px">
37+
38+
:::
39+
40+
:::{figure-md} fig-target
41+
<img src="../images/python-build-package/pypi-metadata-maintainers.png" alt="t." width="700px">
42+
43+
:::
44+
45+
## How to create the distribution format that PyPI and Pip expects?
46+
47+
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.
48+
49+
```{note}
50+
51+
There are a suite of packaging tools that can either help you with
52+
the entire packaging process or just one step of the process. For instance
53+
setuptools is a commonly used build back end that can be used to create your
54+
SDist and wheel. Whereas tools like Hatch, PDM, Poetry and flit help with other
55+
parts of the packaging process.
56+
57+
While this can cause some confusion and
58+
complexity in the packaging ecosystem - for the most part, each tool provides
59+
the same distribution output (with minor differences that most users may not
60+
care about). Learn more about those tools on this page.
61+
```
62+
63+
Below, you will learn about the two distribution files that PyPI expects you to publish: SDist and Wheel. You will learn about
64+
their structure and what files belong in each.
65+
366
There are two core distribution files
467
that you need to create to publish your Python package to
568
PyPI source distribution (often called an sdist) and wheel. The sdist contains the raw source

0 commit comments

Comments
 (0)