File: getting_started.md

package info (click to toggle)
scikit-build-core 0.11.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,588 kB
  • sloc: python: 14,643; ansic: 254; cpp: 134; sh: 27; fortran: 18; makefile: 7
file content (434 lines) | stat: -rw-r--r-- 11,727 bytes parent folder | download | duplicates (3)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# Getting started

If you've never made a Python package before, [packaging.python.org's
tutorial][] is a great place to start. It walks you through creating a simple
package in pure Python using modern tooling and configuration. Another great
resource is the [Scientific Python Developer Guide][]. And a tutorial can be
found at [INTERSECT Training: Packaging][].

## Quick start

There are several mechanisms to quickly get started with a package:

- [uv][] has built-in support for scikit-build-core. Just make a directory for
  your package and run: `uv init --lib --build-backend=scikit`.
- [scientific-python/cookie][] has a cookiecutter/copier template for making a
  package with all the suggestions in the [Scientific Python Developer Guide][].
- For pybind11, there's a example template at [pybind11/scikit_build_example][].
  For nanobind, [nanobind example][] includes the Stable ABI on Python 3.12+!
- There are several examples including scikit-build-core examples (including
  free-threading) at [scikit-build-sample-projects][].

## Writing an extension

We will be writing these files:

````{tab} pybind11

```
example-project
├── example.cpp
├── pyproject.toml
└── CMakeLists.txt
```

````

````{tab} nanobind

```
example-project
├── example.cpp
├── pyproject.toml
└── CMakeLists.txt
```

````

````{tab} SWIG

```
example-project
├── example.c
├── example.i
├── pyproject.toml
└── CMakeLists.txt
```

````

````{tab} Cython

```
example-project
├── example.pyx
├── pyproject.toml
└── CMakeLists.txt
```

````

````{tab} C

```
example-project
├── example.c
├── pyproject.toml
└── CMakeLists.txt
```

````

````{tab} ABI3

```
example-project
├── example.c
├── pyproject.toml
└── CMakeLists.txt
```

````

````{tab} Fortran

```
example-project
├── example.f
├── pyproject.toml
└── CMakeLists.txt
```

````

### Source code

For this tutorial, you can either write a C extension yourself, or you can use
pybind11 and C++. Select your preferred version using the tabs - compare them!

````{tab} pybind11

```{literalinclude} ../examples/getting_started/pybind11/example.cpp
:language: cpp
```

````

````{tab} nanobind

```{literalinclude} ../examples/getting_started/nanobind/example.cpp
:language: cpp
```

````

````{tab} SWIG

```{literalinclude} ../examples/getting_started/swig/example.c
:language: c
```

```{literalinclude} ../examples/getting_started/swig/example.i
:language: swig
```

````

````{tab} Cython

```{literalinclude} ../examples/getting_started/cython/example.pyx
:language: cython
```

````

````{tab} C

```{literalinclude} ../examples/getting_started/c/example.c
:language: c
```

````

````{tab} ABI3

```{literalinclude} ../examples/getting_started/abi3/example.c
:language: c
```

````

````{tab} Fortran

```{literalinclude} ../examples/getting_started/fortran/example.f
:language: fortran
```

````

### Python package configuration

To create your first compiled package, start with a pyproject.toml like this:

````{tab} pybind11

```{literalinclude} ../examples/getting_started/pybind11/pyproject.toml
:language: toml
```

````

````{tab} nanobind

```{literalinclude} ../examples/getting_started/nanobind/pyproject.toml
:language: toml
```

````

````{tab} SWIG

```{literalinclude} ../examples/getting_started/swig/pyproject.toml
:language: toml
```

````

````{tab} Cython

```{literalinclude} ../examples/getting_started/cython/pyproject.toml
:language: toml
```

````

````{tab} C

```{literalinclude} ../examples/getting_started/c/pyproject.toml
:language: toml
```

````

````{tab} ABI3

```{literalinclude} ../examples/getting_started/abi3/pyproject.toml
:language: toml
```

````

````{tab} Fortran

```{literalinclude} ../examples/getting_started/fortran/pyproject.toml
:language: toml
```

```{warning}
The module you build will require an equal or newer version to the version of
NumPy it built with. You should use `oldest-supported-numpy` or manually set
the NumPy version, though you will then be stuck with older versions of f2py.
Also it's hard to compile Fortran on Windows as it's not supported by MSVC and
macOS as it's not supported by Clang.
```


````

Notice that you _do not_ include `cmake`, `ninja`, `setuptools`, or `wheel` in
the requires list. Scikit-build-core will intelligently decide whether it needs
`cmake` and/or `ninja` based on what versions are present in the environment -
some environments can't install the Python versions of CMake and Ninja, like
Android, FreeBSD, WebAssembly, and ClearLinux, but they may already have these
tools installed. Setuptools is not used by scikit-build-core's native builder,
and wheel should never be in this list.

There are other keys you should include under `[project]` if you plan to publish
a package, but this is enough to start for now. The
[project metadata specification](https://packaging.python.org/en/latest/specifications/pyproject-toml)
page covers what keys are available. Another example is available at
[the Scientific Python Library Development Guide](https://learn.scientific-python.org/development/guides).

### CMake file

Now, you'll need a file called `CMakeLists.txt`. This one will do:

````{tab} pybind11

```{literalinclude} ../examples/getting_started/pybind11/CMakeLists.txt
:language: cmake
```

Scikit-build requires CMake 3.15, so there's no need to set it lower than 3.15.

The project line can optionally use `SKBUILD_PROJECT_NAME` and
`SKBUILD_PROJECT_VERSION` variables to avoid repeating this information from
your `pyproject.toml`. You should specify exactly what language you use to keep
CMake from searching for both `C` and `CXX` compilers (the default).

If you place find Python first, pybind11 will respect it instead of the classic
FindPythonInterp/FindPythonLibs mechanisms, which work, but are not as modern.
Here we set `PYBIND11_FINDPYTHON` to `ON` instead of doing the find Python
ourselves. Pybind11 places its config file such that CMake can find it from
site-packages.

You can either use `pybind11_add_module` or `python_add_library` and then link
to `pybind11::module`, your choice.

````

````{tab} nanobind

```{literalinclude} ../examples/getting_started/nanobind/CMakeLists.txt
:language: cmake
```

Scikit-build and nanobind require CMake 3.15, so there's no need to set it
lower than 3.15.

The project line can optionally use `SKBUILD_PROJECT_NAME` and
`SKBUILD_PROJECT_VERSION` variables to avoid repeating this information from
your `pyproject.toml`. You should specify exactly what language you use to keep
CMake from searching for both `C` and `CXX` compilers (the default).

Nanobind places its config file such that CMake can find it from site-packages.
````

````{tab} SWIG

```{literalinclude} ../examples/getting_started/swig/CMakeLists.txt
:language: cmake
```

Scikit-build requires CMake 3.15, so there's no need to set it lower than 3.15.

The project line can optionally use `SKBUILD_PROJECT_NAME` and
`SKBUILD_PROJECT_VERSION` variables to avoid repeating this information from
your `pyproject.toml`. You should specify exactly what language you use to keep
CMake from searching for both `C` and `CXX` compilers (the default).

You'll need to handle the generation of files by SWIG directly.

````

````{tab} Cython

```{literalinclude} ../examples/getting_started/cython/CMakeLists.txt
:language: cmake
```

Scikit-build requires CMake 3.15, so there's no need to set it lower than 3.15.

The project line can optionally use `SKBUILD_PROJECT_NAME` and
`SKBUILD_PROJECT_VERSION` variables to avoid repeating this information from
your `pyproject.toml`. You should specify exactly what language you use to keep
CMake from searching for both `C` and `CXX` compilers (the default).

You'll need to handle the generation of files by Cython directly at the moment.
A helper (similar to scikit-build classic) might be added in the future.

````

````{tab} C

```{literalinclude} ../examples/getting_started/c/CMakeLists.txt
:language: cmake
```

Scikit-build requires CMake 3.15, so there's no need to set it lower than 3.15.

The project line can optionally use `SKBUILD_PROJECT_NAME` and
`SKBUILD_PROJECT_VERSION` variables to avoid repeating this information from
your `pyproject.toml`. You should specify exactly what language you use to keep
CMake from searching for both `C` and `CXX` compilers (the default).

`find_package(Python ...)` should always include the `Development.Module`
component instead of `Development`; the latter breaks if the embedding
components are missing, such as when you are building redistributable wheels on
Linux.

You'll want `WITH_SOABI` when you make the module to ensure the full extension
is included on Unix systems (PyPy won't even be able to open the extension
without it).

````

````{tab} ABI3

```{literalinclude} ../examples/getting_started/abi3/CMakeLists.txt
:language: cmake
```

Scikit-build requires CMake 3.15, so there's no need to set it lower than 3.15.

The project line can optionally use `SKBUILD_PROJECT_NAME` and
`SKBUILD_PROJECT_VERSION` variables to avoid repeating this information from
your `pyproject.toml`. You should specify exactly what language you use to keep
CMake from searching for both `C` and `CXX` compilers (the default).

`find_package(Python ...)` needs `Development.SABIModule` for ABI3 extensions.

You'll want `WITH_SOABI` when you make the module. You'll also need to set the `USE_SABI`
argument to the minimum version to build with. This will also add a proper
PRIVATE define of `Py_LIMITED_API` for you.

```{note}
This will not support pypy, so you'll want to provide an alternative if you
support PyPy).
```

````

````{tab} Fortran

```{literalinclude} ../examples/getting_started/fortran/CMakeLists.txt
:language: cmake
```

Scikit-build requires CMake 3.15, so there's no need to set it lower than 3.15.

The project line can optionally use `SKBUILD_PROJECT_NAME` and
`SKBUILD_PROJECT_VERSION` variables to avoid repeating this information from
your `pyproject.toml`. You should specify exactly what language you use to keep
CMake from searching for both `C` and `CXX` compilers (the default).

You'll need to handle the generation of files by NumPy directly at the moment.
A helper (similar to scikti-build classic) might be added in the future. You'll
need gfortran on macOS.

````

Finally, you install your module. The default install path will go directly to
`site-packages`, so if you are creating anything other than a single
c-extension, you will want to install to the package directory (possibly
`${SKBUILD_PROJECT_NAME}`) instead.

### Building and installing

That's it! You can try building it:

```console
$ pipx run build
```

[pipx](https://pipx.pypa.io) allows you to install and run Python applications
in isolated environments.

Or installing it (in a virtualenv, ideally):

```console
$ pip install .
```

That's it for a basic package!

<!-- prettier-ignore-start -->
[scientific python developer guide]: https://github.com/scikit-build/scikit-build-sample-projects
[scikit-build-sample-projects]:      https://github.com/scikit-build/scikit-build-sample-projects
[uv]:                                https://docs.astral.sh/uv/
[scientific-python/cookie]:          https://github.com/scientific-python/cookie
[pybind11/scikit_build_example]:     https://github.com/pybind/scikit_build_example
[INTERSECT Training: Packaging]:     https://intersect-training.org/packaging
[packaging.python.org's tutorial]:   https://packaging.python.org/en/latest/tutorials/packaging-projects
[nanobind example]:                  https://github.com/wjakob/nanobind_example
<!-- prettier-ignore-end -->