-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path04_dependencies_ci.qmd
More file actions
362 lines (245 loc) · 6.68 KB
/
Copy path04_dependencies_ci.qmd
File metadata and controls
362 lines (245 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
---
title: "Dependencies and Continuous Integration"
format:
html: default
revealjs:
output-file: 04_dependencies_ci_slides.html
slide-number: true
footer: Python package development
---
## Dependencies
⏩ Dependencies are other pieces of software your project needs to work.
🤔 They save time by reusing existing solutions, but can make your project harder to maintain if they change or are no longer available.

##
:::: {.columns}
::: {.column}
**Application**
*A program that is run by a user*
* command line tool
* script
* web application
Pin versions to ensure reproducibility, e.g. `numpy==1.11.0`
:::
::: {.column}
**Library**
*A program that is used by another program*
* *numpy*
* *scikit-learn*
Make the requirements as loose as possible, e.g. `numpy>=1.11.0`
:::
::::
::: {.notes}
Make the requirements loose, to avoid conflicts with other packages.
:::
## Dependency resolution
::: {.incremental}
- Python projects often depend on many packages, which may depend on others ("dependency tree").
- Different packages may require conflicting versions of the same dependency.
- Historically, Python tools did not always resolve these conflicts well, leading to "dependency hell." 😈
- Ensuring everyone gets the same working set of packages has been difficult, especially for larger projects. 🤪
:::
. . .
Modern tools like `uv` aim to solve these problems with faster and more reliable 😎
## uv {.smaller}
* 🚀 A single tool to replace pip, pip-tools, pipx, poetry, pyenv, twine, virtualenv, and more.
* ⚡️ 10-100x faster than pip.
* 🗂️ Provides comprehensive project management, with a universal lockfile.
* ❇️ Runs scripts, with support for inline dependency metadata.
* 🐍 Installs and manages Python versions.
* 🛠️ Runs and installs tools published as Python packages.
* 🔩 Includes a pip-compatible interface for a performance boost with a familiar CLI.
* 💾 Disk-space efficient, with a global cache for dependency deduplication.
* ⏬ Installable without Rust or Python via curl or pip.
* 🖥️ Supports macOS, Linux, and Windows.
## Dependency management
`uv` is the recommended tool for managing a Python project including dependencies.
Example of pinning versions:
```{.toml filename="pyproject.toml"}
dependencies = [
"numpy==1.11.0",
"scipy==0.17.0",
"matplotlib==1.5.1",
]
```
. . .
Or using a range of versions:
```{.toml filename="pyproject.toml"}
dependencies = [
"numpy>=1.11.0",
"scipy>=0.17.0",
"matplotlib>=1.5.1,<=2.0.0"
]
```
. . .
Install dependencies:
```{.bash}
$ uv sync
```
## Development dependencies
I.e. dependencies needed for testing, building documentation, linting, etc. not needed to run the package.
```{.toml filename="pyproject.toml"}
[dependency-groups]
dev = [
"pytest>=8.4.1",
]
```
## Dependency management using uv
* Add a dependency:
```{.bash}
$ uv add matplotlib
```
* Remove a dependency:
```{.bash}
$ uv remove seaborn
```
* Add development dependency:
```{.bash}
$ uv add --dev pytest
```
## Creating an installable package
Create a new library project:
```{.bash}
$ uv init --lib
```
Start a Python session:
```{.bash}
$ uv run python
```
```{.python}
>>> import mini
>>> mini.foo()
42
```
. . .
Run tests:
```{.bash}
$ uv run pytest
...
tests/test_foo.py . [100%]
=============== 1 passed in 0.01s ===============
```
## Virtual environments
::: {.incremental}
* Creates a clean environment for each project
* Allows different versions of a package to coexist on your machine
* Can be used to create a reproducible environment for a project
* Virtual environments are managed by `uv`
:::
## Continuous Integration (CI)
* Running tests on every commit in a well defined environment ensures that the code is working as expected.
* It solves the "it works on my machine" problem.
* Executing code on a remote server is a good way to ensure that the code is working as expected.
* Example of CI services:
- **GitHub Actions**
- Azure Pipelines
- Travis CI
## GitHub Actions {.smaller}
:::: {.columns}
::: {.column}
* Workflow are stored in the `.github/workflows` folder.
* Workflow is described in a YAML file.
* YAML is whitespace sensitive (like Python).
* YAML can contain lists, dictionaries and strings, and can be nested.
:::
::: {.column}
```bash
$ tree mikeio/.github/
mikeio/.github/
└── workflows
├── docs.yml
├── full_test.yml
├── notebooks_test.yml
├── perf_test.yml
└── python-publish.yml
```
:::
::::
---
## Workflow example {.smaller}
```yaml
name: Quick test
on: # when to run the workflow
push:
branches: [ main]
pull_request:
branches: [ main ]
jobs: # which jobs to run
build: # descriptive name 🙄
runs-on: ubuntu-latest # on what operating system
steps:
- uses: actions/checkout@v3
- name: Set up uv
uses: astral-sh/setup-uv@v6
with:
python-version: "3.13"
- name: Install dependencies
run: |
uv sync
- name: Test with pytest
run: |
uv run pytest
```
---
🙂🚀

---
☹️

## Benefits of CI
::: {.incremental}
* Run tests on every commit
* Test on different operating systems
* Test on different Python versions
* Create API documentation
* Publish package to PyPI or similar package repository
:::
## Triggers
* `push` and `pull_request` are the most common triggers
* `schedule` can be used to run the workflow on a schedule
* `workflow_dispatch` can be used to trigger the workflow manually
```{.yaml code-line-numbers="2-3|4-5|6-7|8"}
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
schedule:
- cron: '0 0 * * 0'
workflow_dispatch:
```
## Jobs
* Operating system
* Python version
* ...
```{.yaml}
...
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ["3.10","3.13"]
...
```
## GitHub Releases {.smaller}
:::: {.columns}
::: {.column}
* GitHub releases are a way to publish software releases.
* You can upload files, write release notes and tag the release.
* As a minimum, the release will contain the source code at the time of the release.
* Creating a release can trigger other workflows, e.g. publishing a package to PyPI.
:::
::: {.column}

:::
::::
<https://github.com/pydata/xarray/releases/tag/v2022.12.0>
## Summary
::: {.incremental}
* Application vs library
* Use a separate virtual environment for each project
* Use GitHub Actions to run tests on every commit
* Use GitHub Releases to publish software releases
:::