You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: package-structure-code/complex-python-package-builds.md
+26-3Lines changed: 26 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,30 @@
1
1
# Complex Python package builds
2
2
3
+
This guide is focused on packages that are either pure-python or that
4
+
have a few simple extensions in another language such as C or C++.
5
+
6
+
If your package is more complex, [you may want to refer to this guide
7
+
created by Ralf Gommers on Python packaging.](https://pypackaging-native.github.io/)
8
+
9
+
## Pure Python Packages vs. packages with extensions in other languages
10
+
11
+
You can classify Python package complexity into three general categories. These
12
+
categories can in turn help you select the correct package front-end and
13
+
back end tools.
14
+
15
+
1.**Pure-python packages:** these are packages that only rely on Python to function. Building a pure Python package is simpler. As such, you can chose a tool below that
16
+
has the features that you want and be done with your decision!
17
+
2.**Python packages with non-Python extensions:** These packages have additional components called extensions written in other languages (such as `C` or `C++`). If you have a package with non-python extensions, then you need to select a build back-end tool that allows you to add additional build steps needed to compile your extension code. Further, if you wish to use a front-end tool to support your workflow, you will need to select a tool that
18
+
supports additional build setps. In this case, you could use setuptools. However, we suggest that you chose build tool that supports custom build steps such as Hatch with Hatchling or PDM. PDM is an excellent choice as it allows you to also select your build back end of choice. We will discuss this at a high level on the complex builds page.
19
+
3.**Python packages that have extensions written in different languages (e.g. fortran and C++) or that have non Python dependencies that are difficult to install (e.g. GDAL)** These packages often have complex build steps (more complex than a package with just a few C extensions for instance). As such, these packages require tools such as [scikit-build](https://scikit-build.readthedocs.io/en/latest/)
20
+
or [meson-python](https://mesonbuild.com/Python-module.html) to build. NOTE: you can use meson-python with PDM.
21
+
22
+
23
+
<!--
24
+
On this page, we will focus on using front-end tools to package pure python
25
+
packages. We will note if a package does have the flexibility to support other
26
+
back-ends and in turn more complex builds (*mentioned in #2 and #3 above*). -->
pdm and poetry both rely on setuptools for C extensions. pdm's support claims to be fully developed and documented. poetry claims nothing, and doesn't document it.
88
113
89
-
-->
90
114
91
-
```{note}
92
115
??
93
116
Poetry supports extensions written in other languages but this functionality is
94
117
currently undocumented.
@@ -101,7 +124,6 @@ package builds.
101
124
Some front-end packaging tools, such as PDM, allow you to use other
102
125
build back-ends such as **meson** and **scikit-build**.
103
126
104
-
```
105
127
106
128
me:
107
129
pdm, hatch and poetry all have "ways" of supporting c extensions via pdm-build, hatchling and poetry's build back end.
@@ -118,3 +140,4 @@ pdm and poetry both rely on setuptools for C extensions. pdm's support claims to
# Use a pyproject.toml file for your package configuration & metadata
2
+
3
+
The standard file that Python packages use to specify build requirements and
4
+
metadata is called a pyproject.toml. The pyproject.toml file has become the
5
+
standard for declaring Python package metadata (including dependencies) rather
6
+
than using a setup.py file (or a setup.py + setup.cfg file).
7
+
8
+
As such you should try to [include all project based metadata and build system specifications in a `pyproject.toml` file.](https://packaging.python.org/en/latest/specifications/declaring-project-metadata/) Using setup.py to manage both package set up and
9
+
hold metadata [can cause problems with package development.](https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html)
10
+
11
+
12
+
```{admonition} Benefits of using a pyproject.toml file
13
+
:class: tip
14
+
15
+
1. Because setup.py has a mixture of code and metadata, it will be run twice when
16
+
building your package. First it will be run to extract metadata (dependencies). Then it will be run to build your package.
17
+
1. Including your package's metadata in a separate human-readable `pyproject.toml` format also allows someone to view the project's metadata without
18
+
running any Python code.
19
+
```
20
+
21
+
A pyproject.toml is written in [TOML (Tom's Obvious, Minimal Language) format](https://toml.io/en/). TOML is an easy-to-read structure that is founded on key: value pairs.
22
+
23
+
Each section in the pyproject.toml file contains a `[table identifier]`.
24
+
Below that table identifier are key value pairs that
25
+
support configuration for that particular table.
26
+
27
+
<!-- setup.cfg for project metadata is being deprecated - set setuptools guide and
ELI: A complex build could mean running a python script that processes some data file and produces a pure python module file.
44
+
45
+
Probably not common in the scientific community specifically, but I've seen quite a few setup.py files that contain custom build stages which e.g. build gettext locale catalogs.
46
+
47
+
The main point is that it is more "complex" than simply copying files or directories as-is into the built wheel.
48
+
-->
49
+
```
50
+
51
+
## Example pyproject.toml
52
+
53
+
Below is an example build configuration for a Python project. This setup
54
+
requires:
55
+
56
+
***setuptools** to create the package structure,
57
+
***wheel** which is used by `setuptools` to create the [**.whl** (wheel) file](https://realpython.com/python-wheels/).
58
+
***setuptools build** to "build" the package
59
+
***setuptools_scm** to manage package version updates
60
+
61
+
In the example below `[build-system]` is the first table
62
+
of values. It has two keys that specify the build front end and backend for a package:
You can classify Python package complexity into three general categories. These
15
-
categories can in turn help you select the correct package front-end and
16
-
back end tools.
17
14
18
-
1. **Pure-python packages:** these are packages that only rely on Python to function. Building a pure Python package is simpler. As such, you can chose a tool below that
19
-
has the features that you want and be done with your decision!
20
-
2. **Python packages with non-Python extensions:** These packages have additional components called extensions written in other languages (such as `C` or `C++`). If you have a package with non-python extensions, then you need to select a build back-end tool that allows you to add additional build steps needed to compile your extension code. Further, if you wish to use a front-end tool to support your workflow, you will need to select a tool that
21
-
supports additional build setps. In this case, you could use setuptools. However, we suggest that you chose build tool that supports custom build steps such as Hatch with Hatchling or PDM. PDM is an excellent choice as it allows you to also select your build back end of choice. We will discuss this at a high level on the complex builds page.
22
-
3. **Python packages that have extensions written in different languages (e.g. fortran and C++) or that have non Python dependencies that are difficult to install (e.g. GDAL)** These packages often have complex build steps (more complex than a package with just a few C extensions for instance). As such, these packages require tools such as [scikit-build](https://scikit-build.readthedocs.io/en/latest/)
23
-
or [meson-python](https://mesonbuild.com/Python-module.html) to build. NOTE: you can use meson-python with PDM.
15
+
:::{figure-md} fig-target
24
16
25
-
On this page, we will focus on using front-end tools to package pure python
26
-
packages. We will note if a package does have the flexibility to support other
27
-
back-ends and in turn more complex builds (*mentioned in #2 and #3 above*).
17
+
<imgsrc="../images/python-package-tools-decision-tree.png"alt="Figure showing... will finish this once we are all happy with the figure and it's not going to change more..."width="700px">
28
18
29
-
[If you are interested in tool support for non pure python builds, check out this
30
-
page for resources.](complex-python-package-builds)
31
-
```
19
+
Diagram showing the various from end build tools that you can select from. Each tool has different features as highlighted below.
20
+
NOTE: this is still a DRAFT so i'm not going to spend time truly cleaning it up until i get lots of feedback on the general approach!!
21
+
:::
22
+
23
+
If you want to know more about Python packages that have extensions written in
24
+
other languages, [check out the page on complex package builds.](complex-python-package-builds)
32
25
33
26
## Build front-end vs. build back-end tools
34
27
@@ -99,12 +92,12 @@ Example build steps using setuptools:
99
92
A packaging front-end tool refers to a tool that makes it easier for you to
100
93
perform common packaging tasks using similar commands. These tasks include:
101
94
102
-
*[Creating a Sdist and Wheel distribution](python-package-distribution-files-sdist-wheel)
103
-
* Managing an environment or multiple environments in which you need to run tests and develop your package
104
-
* Building documentation
95
+
*[Build your packages (create the SDist and Wheel distributions](python-package-distribution-files-sdist-wheel)
105
96
* Installing your package in a development mode (so it updates when you update your code)
106
-
* Running tests
107
97
* Publishing to PyPI
98
+
* Running tests
99
+
* Building documentation
100
+
* Managing an environment or multiple environments in which you need to run tests and develop your package
108
101
109
102
There are several Python packaging tools that you can use for pure Python
110
103
builds. Each front-end tool discussed below supports a slightly different set of Python
@@ -131,13 +124,13 @@ hatch build
131
124
# Example to publish to PyPI:
132
125
hatch publish --repository testpypi
133
126
```
134
-
Example build steps using **setuptools** and **build**:
127
+
Example build steps using the **setuptools** backend and **build**:
135
128
136
129
```bash
137
130
# Build the package
138
131
python3 -m build
139
132
140
-
# Publish to test PyPI
133
+
# Publish to test PyPI using twine
141
134
twine upload -r testpypi dist/*
142
135
```
143
136
@@ -197,7 +190,7 @@ The Python developers survey results (n=>8,000 PyPI users) show setuptools and p
197
190
198
191
The tools that we review below include:
199
192
200
-
*setuptools + twine, build
193
+
*Twine, Build + setuptools
201
194
* Flit
202
195
* Hatch
203
196
* PDM
@@ -219,15 +212,6 @@ workflow to support such extensions. It also supports other backends such as sci
219
212
NOTE: You can also use Hatch but you will need to write your own plugin for this support.
<!-- ### Build tools for Python packages with complex build steps
233
217
If your package is not pure Python, or it has complex build steps (or build
@@ -430,7 +414,7 @@ is currently undocumented. Thus we don't recommend it for more complex builds.
430
414
:widths: 20,5,50
431
415
432
416
Dependency management,✅,Poetry helps you add dependencies to your `pyproject.toml` metadata. _NOTE: currently Poetry adds dependencies using an approach that is slightly out of alignment with current Python peps - however there is a plan to fix this in an upcoming release._ Allows you to organize dependencies in groups: docs; package; tests.
433
-
Dependency pinning,✖✅ ,Poetry offers dependency pinning however, it does so in a way that can be problematic for some packages. Read below for more.
417
+
Dependency pinning,✖✅ ,Poetry offers dependency pinning however it does so in a way that can be problematic for some packages. Read below for more.
434
418
Select your environment manager of choice (conda; venv; etc),✅ , Poetry allows you to either use its simple environment management tool or select the environment manager that you want to use for managing your package. [Read more about its built in environment management options](https://python-poetry.org/docs/basic-usage/#using-your-virtual-environment).
435
419
Publish to PyPI and test PyPI,✅,Poetry supports publishing to both test PyPI and PyPI
436
420
Version Control based versioning,✅ , The plugin (Poetry dynamic versioning)[https://github.com/mtkennerly/poetry-dynamic-versioning] supports versioning using git tags with Poetry.
@@ -473,7 +457,7 @@ This approach also won't support over ways of versioning tools, for instance,
473
457
some tools use [calver](https://calver.org/) which creates new versions based on the date.
474
458
```
475
459
476
-
```{admonition}where does this belong?
460
+
```{admonition}Hatch vs PDM vs Poetry
477
461
:class: note
478
462
There are some features that Hatch and PDM offer that Poetry does not.
0 commit comments