Skip to content

Commit ea0d1bf

Browse files
authored
Add: answer the question - what is building
Add build clarification to package guide
2 parents d656dac + e31f6f0 commit ea0d1bf

7 files changed

+153
-17
lines changed
120 KB
Loading
60.5 KB
Loading
102 KB
Loading
217 KB
Loading

package-structure-code/intro.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,61 @@ best fitted for your workflow.
1414
:gutter: 3
1515

1616
:::{grid-item-card}
17-
:link: python-package-build-tools
17+
:link: python-package-structure
1818
:link-type: doc
1919

20-
Finding the right packaging tool(s)
20+
1. Package file structure
2121
^^^
22-
23-
Learn more about the suite of packaging tools out there.
24-
And learn which tool might be best for you.
22+
src layout, flat layout and where should tests folders live? No matter what your level of packaging knowledge is, this page will help you decide upon a package structure that follows modern python best practices.
2523
:::
2624

2725
:::{grid-item-card}
2826
:link: python-package-structure
2927
:link-type: doc
3028

31-
Package file structure
29+
2. Learn about building your package
3230
^^^
33-
src layout, flat layout and where should tests folders live? No matter what your level of packaging knowledge is, this page will help you decide upon a package structure that follows modern python best practices.
31+
Publishing a Python package is a great way to share your code with scientists to support open science workflow. In order to publish a package, you will need to first build it. The act of "building" refers to the process of placing your package code and
32+
metadata into a format that can be published on PyPI. Once published on PyPI, your users can easily install it locally using pip. Learn more about building a Python package here.
3433
:::
3534

3635
:::{grid-item-card}
3736
:link: python-package-build-tools
3837
:link-type: doc
3938

40-
✨ Publish to PyPI and Conda ✨
39+
✨ 3. What Python package tool should you use? ✨
40+
^^^
41+
42+
Learn more about the suite of packaging tools out there.
43+
And learn which tool might be best for you.
44+
:::
45+
46+
:::{grid-item-card}
47+
:link: python-package-build-tools
48+
:link-type: doc
49+
50+
✨ 4. Publish your package to PyPI and Conda ✨
4151
^^^
4252
If you have a pure python package, it's a straight forward
4353
process to publish to both PyPI and then a Conda channel such as
4454
conda-forge. Learn more here.
4555
:::
4656

57+
:::{grid-item-card}
58+
:link: python-package-versions
59+
:link-type: doc
60+
61+
✨ 5. Setup package versioning ✨
62+
^^^
63+
Semver (numeric versioning) and Calver (versioning using the date) are 2
64+
common ways to version a package. Which one should you pick? Learn more here.
65+
:::
66+
4767
:::{grid-item-card}
4868
:link: code-style-linting-format
4969
:link-type: doc
5070

51-
✨ Code style & linters ✨
71+
6. Code style & linters ✨
5272
^^^
5373
Black, blue, flake8, Ruff - which tools can help you ensure your
5474
package follows best practices for code format? Learn more about the options and why this is important here.
@@ -135,7 +155,7 @@ Intro <self>
135155
136156
Python package structure <python-package-structure>
137157
pyproject.toml Package Metadata <pyproject-toml-python-package-metadata>
138-
What are SDist & Wheel Files? <python-package-distribution-files-sdist-wheel>
158+
Build Your Package <python-package-distribution-files-sdist-wheel>
139159
Package Build Tools <python-package-build-tools>
140160
Complex Builds <complex-python-package-builds>
141161
```

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# Python Package Build Tools
1+
# Python Packaging Tools
22

33
<!-- TODO: add a small discussion on what pinning is?-->
44

5+
## Tools for building your package
6+
57
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,
68
benefits and limitations of the most commonly used Python packaging tools.
79
We focus on pure-python packages in this guide. However, we also

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

Lines changed: 120 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,130 @@
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.
2116

3117
There are two core distribution files
4118
that you need to create to publish your Python package to
5119
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
7121
that can be directly installed onto anyones' computer.
8122

9123
Learn more about both distributions below.
10124

11125
```{note}
12126
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
14128
similar content. However if your package has extensions in other languages
15129
or is more complex in its build, the two distributions will be very different.
16130
@@ -82,13 +196,13 @@ stravalib-1.1.0.post2-SDist.tar.gz file contents
82196
83197
```
84198

85-
```{admonition} GitHub archive vs SDist
199+
```{admonition} GitHub archive vs sdist
86200
:class: tip
87201
When you make a release on GitHub, it creates a `git archive` that contains all
88202
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
90204
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.
92206
```
93207

94208
## Wheel (.whl files):

0 commit comments

Comments
 (0)