Skip to content

Commit 08a262f

Browse files
Merge pull request #339 from AaltoSciComp/course-2025/packaging-flit
packaging: Updates for 2025
2 parents 33e4982 + dc1e45c commit 08a262f

File tree

4 files changed

+112
-34
lines changed

4 files changed

+112
-34
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# calculator
2+
3+
Great calculator with amazing mathematics functions:
4+
5+
- `calculator.adding.add`: Adds numbers
6+
- `calculator.subtracting.subtract`: Subtracts numbers
7+
- `calculator.integrating.integral`: Integrate functions
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
[build-system]
2-
requires = ["setuptools>=61.0"]
3-
build-backend = "setuptools.build_meta"
2+
requires = ["flit_core >=3.11,<4"]
3+
build-backend = "flit_core.buildapi"
44

55
[project]
6-
name = "calculator-myname"
7-
description = "A small example package"
8-
version = "0.1.0"
6+
name = "calculator_myname"
7+
authors = [{name = "Firstname Lastname", email = "[email protected]"}]
98
readme = "README.md"
10-
authors = [
11-
{ name = "Firstname Lastname", email = "[email protected]" }
12-
]
9+
license = "MIT"
10+
license-files = ["LICENSE"]
11+
dynamic = ["version", "description"]
1312
dependencies = [
1413
"scipy"
1514
]
15+
16+
[project.urls]
17+
Home = "http://www.example.org"
18+
19+
[tool.flit.module]
20+
name = "calculator"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[build-system]
2+
requires = ["flit_core >=3.11,<4"]
3+
build-backend = "flit_core.buildapi"
4+
5+
[project]
6+
name = "calculator_myname"
7+
authors = [{name = "Firstname Lastname", email = "[email protected]"}]
8+
readme = "README.md"
9+
license = "MIT"
10+
license-files = ["LICENSE"]
11+
dynamic = ["version", "description"]
12+
13+
[project.urls]
14+
Home = "http://www.example.org"

content/packaging.rst

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,81 @@ These are the 3 files:
4444

4545
.. literalinclude:: packaging-example-project/calculator/adding.py
4646
:caption: adding.py
47+
:language: python
4748

4849
.. literalinclude:: packaging-example-project/calculator/subtracting.py
4950
:caption: subtracting.py
51+
:language: python
5052

5153
.. literalinclude:: packaging-example-project/calculator/integrating.py
5254
:caption: integrating.py
55+
:language: python
5356

5457
We will add a fourth file:
5558

5659
.. literalinclude:: packaging-example-project/calculator/__init__.py
5760
:caption: __init__.py
61+
:language: python
5862

5963
This ``__init__.py`` file will be the interface of our package/library.
6064
It also holds the package docstring and the version string.
6165
Note how it imports functions from the various modules using *relative imports*
6266
(with the dot).
6367

68+
After that let's create a file called ``README.md`` to the project root
69+
that will describe our project to other people who might want to use it.
70+
71+
72+
.. literalinclude:: packaging-example-project/README.md
73+
:caption: README.md
74+
:language: markdown
75+
76+
Now our folder should look something like this:
77+
78+
.. code-block:: none
79+
80+
calculator_myname
81+
├── calculator
82+
│ ├── adding.py
83+
│ ├── __init__.py
84+
│ ├── integrating.py
85+
│ └── subtracting.py
86+
└── README.md
87+
88+
After this we need to create a file called
89+
`pyproject.toml <https://packaging.python.org/en/latest/guides/writing-pyproject-toml/>`__,
90+
which describes our package.
91+
To make this easier we'll use ``flit`` (which is already installed in the
92+
course environment) in a terminal to initialize it:
93+
94+
.. code-block:: console
95+
96+
$ flit init
97+
Module name [calculator]: calculator_myname
98+
Author: Firstname Lastname
99+
Author email: [email protected]
100+
Home page: http://www.example.org
101+
Choose a license (see http://choosealicense.com/ for more info)
102+
1. MIT - simple and permissive
103+
2. Apache - explicitly grants patent rights
104+
3. GPL - ensures that code based on this is shared with the same terms
105+
4. Skip - choose a license later
106+
Enter 1-4: 1
107+
108+
Written pyproject.toml; edit that file to add optional extra info.
109+
110+
``flit`` will ask us questions about your project and it create a
111+
``pyproject.toml`` into the project folder. The name of the package
112+
(Module name) should be something that is not already in use. In best
113+
case scenario it should be the same as the Python module name. In our
114+
case, let's use a different name and let's fix this later.
115+
64116
This is how we will arrange the files in the project folder/repository:
65117

66118
.. code-block:: none
67119
:emphasize-lines: 3-6
68120
69-
project-folder
121+
calculator_myname
70122
├── calculator
71123
│ ├── adding.py
72124
│ ├── __init__.py
@@ -88,26 +140,27 @@ the next section.
88140
Testing a local pip install
89141
---------------------------
90142

91-
To make our example package pip-installable we need to add one more file:
143+
The ``pyproject.toml`` specification tells Pip what our package is and
144+
what it should install. It currently looks like this:
92145

93-
.. code-block:: none
94-
:emphasize-lines: 9
146+
.. literalinclude:: packaging-example-project/pyproject.toml-partial
147+
:caption: pyproject.toml
148+
:language: toml
95149

96-
project-folder
97-
├── calculator
98-
│ ├── adding.py
99-
│ ├── __init__.py
100-
│ ├── integrating.py
101-
│ └── subtracting.py
102-
├── LICENSE
103-
├── README.md
104-
└── pyproject.toml
105150

106-
This is how ``pyproject.toml`` looks:
151+
Let's do couple of finishing touches to it. Because we have different names
152+
for the package and our module import, we'll add a section that specifies
153+
that.
154+
155+
We also need to add the dependency to ``scipy``.
156+
157+
After the changes our ``pyproject.toml`` looks like this:
158+
107159

108160
.. literalinclude:: packaging-example-project/pyproject.toml
109161
:caption: pyproject.toml
110-
:emphasize-lines: 13-15
162+
:emphasize-lines: 12-14,19-20
163+
:language: toml
111164

112165
Note how our package requires ``scipy`` and we decided to not pin the version
113166
here (see :ref:`version_pinning`).
@@ -118,8 +171,8 @@ test before trying to upload a package to PyPI or test-PyPI
118171

119172
.. note::
120173

121-
Sometime you need to rely on unreleased, development versions as
122-
dependencies and this is also possible. For example, to use the
174+
Sometime you need to rely on unreleased, development versions as
175+
dependencies and this is also possible. For example, to use the
123176
latest ``xarray`` you could add::
124177

125178
dependencies = [
@@ -146,7 +199,7 @@ Exercise 1
146199

147200
.. hint:: To create and activate a virtual environment
148201
:class: dropdown
149-
202+
150203
.. tabs::
151204

152205
.. tab:: Unix/macOS
@@ -211,9 +264,8 @@ Let's try it out. First we create the distribution package::
211264

212265
$ python3 -m build
213266

214-
We need twine::
215-
216-
$ pip install twine
267+
We need also have ``twine`` installed, but it is included in the course
268+
environment.
217269

218270
And use twine to upload the distribution files to test-PyPI::
219271

@@ -242,7 +294,7 @@ And use twine to upload the distribution files to test-PyPI::
242294

243295
#. Under **Token name** write something memorable.
244296
It should remind you the *purpose*
245-
or the *name of the computer*, such that when you are done
297+
or the *name of the computer*, such that when you are done
246298
using it, you can safely delete it.
247299
#. Under **Scope** select ``Entire account (all projects)``.
248300
#. Click on **Create token**.
@@ -266,7 +318,7 @@ Once this is done, create yet another virtual environment and try to install fro
266318
$ python3 -m pip install \
267319
-i https://test.pypi.org/simple/ \
268320
--extra-index-url https://pypi.org/simple/ \
269-
calculator-myname
321+
calculator_myname
270322
$ deactivate
271323
272324
.. tab:: Windows
@@ -277,7 +329,7 @@ Once this is done, create yet another virtual environment and try to install fro
277329
$ python3 -m venv venv-calculator
278330
$ venv-calculator\Scripts\activate
279331
$ where python
280-
$ python3 -m pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ calculator-myname
332+
$ python3 -m pip install -i https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple/ calculator_myname
281333
$ deactivate
282334
283335
If you upload packages to PyPI or test PyPI often you can create an API token and
@@ -312,7 +364,7 @@ cross-compatible amongst each other and with ``pip``.
312364

313365
The properties of the project and your development requirements may determine which packaging
314366
tool suits you. Use the above decision tree from pyOpenSci_ to help make that choice.
315-
367+
316368

317369
.. _pyOpenSci: https://www.pyopensci.org/python-package-guide/package-structure-code/python-package-build-tools.html
318370

@@ -427,7 +479,7 @@ Tools that simplify sharing conda packages
427479

428480
- `pixi <https://pixi.sh>`__ is package management tool to cover all features of conda, along with
429481
ability to initialize and package new projects.
430-
- `rattler-build <https://rattler.build>`__ is a build tool which combines the functionalities of
482+
- `rattler-build <https://rattler.build>`__ is a build tool which combines the functionalities of
431483
``conda grayskull``, ``conda build`` and allows you to also publish packages.
432484

433485

0 commit comments

Comments
 (0)