-
Notifications
You must be signed in to change notification settings - Fork 54
/
python.md
212 lines (169 loc) · 5.79 KB
/
python.md
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
# Writing a Python package
Writing a Python package is fairly straightforward, especially for "Python-only" packages.
In the second example we will build a package for `numpy` which contains compiled code.
## A Python-only package
The following recipe uses the `noarch: python` setting to build a `noarch` package that can be installed on any platform without modification.
This is very handy for packages that are pure Python and do not contain any compiled extensions.
Additionally, `noarch: python` packages work with a range of Python versions (contrary to packages with compiled extensions that are tied to a specific Python version).
```yaml title="recipe.yaml"
context:
version: "8.1.2"
package:
name: ipywidgets
version: ${{ version }}
source:
url: https://pypi.io/packages/source/i/ipywidgets/ipywidgets-${{ version }}.tar.gz
sha256: d0b9b41e49bae926a866e613a39b0f0097745d2b9f1f3dd406641b4a57ec42c9
build:
noarch: python # (1)!
script: pip install . -v
requirements:
# note that there is no build section
host:
- pip
- python >=3.7
- setuptools
- wheel
run:
- comm >=0.1.3
- ipython >=6.1.0
- jupyterlab_widgets >=3.0.10,<3.1.0
- python >=3.7
- traitlets >=4.3.1
- widgetsnbextension >=4.0.10,<4.1.0
tests:
- python:
imports:
- ipywidgets # (2)!
about:
homepage: https://github.com/ipython/ipywidgets
license: BSD-3-Clause
license_file: LICENSE
summary: Jupyter Interactive Widgets
description: |
ipywidgets are interactive HTML widgets for Jupyter notebooks and the IPython kernel.
documentation: https://ipywidgets.readthedocs.io/en/latest/
```
1. The `noarch: python` line tells `rattler-build` that this package is pure
Python and can be one-size-fits-all. `noarch` packages can be installed on any
platform without modification which is very handy.
2. The `imports` section in the tests is used to check that the package is
installed correctly and can be imported.
### Running the recipe
To build this recipe, simply run:
```bash
rattler-build build --recipe ./ipywidgets
```
## A Python package with compiled extensions
We will build a package for `numpy` – which contains compiled code.
Since compiled code is `python` version-specific, we will need to specify the `python` version explicitly.
The best way to do this is with a "variant_config.yaml" file:
```yaml title="variants.yaml"
python:
- 3.11
- 3.12
```
This will replace any `python` found in the recipe with the versions specified in the `variants.yaml` file.
```yaml title="recipe.yaml"
context:
version: 2.0.1
default_abi_level: 1.21
package:
name: numpy
version: ${{ version }}
source:
- url: https://github.com/numpy/numpy/releases/download/v${{ version }}/numpy-${{ version }}.tar.gz
sha256: 485b87235796410c3519a699cfe1faab097e509e90ebb05dcd098db2ae87e7b3
build:
python:
entry_points:
- f2py = numpy.f2py.f2py2e:main # [win]
- numpy-config = numpy._configtool:main
requirements:
build:
- ${{ compiler('c') }}
- ${{ compiler('cxx') }}
# note: some `host` dependencies that run at build time (e.g., `cython`, `meson-python`)
# should ideally be in `build` instead, this is because cross compilation of
# Python packages in conda-forge uses `crossenv` rather than regular cross compilation.
host:
# note: variant is injected here!
- python
- pip
- meson-python
- pkg-config
- python-build
- cython
- libblas
- libcblas
- liblapack
run:
- python
run_exports:
- numpy >=${{ default_abi_level }},<3.0.0a0
tests:
- python:
imports:
- numpy
- numpy.fft
- numpy.linalg
- numpy.random
- numpy.ctypeslib
- script:
- f2py -v
- numpy-config --cflags
about:
homepage: http://numpy.org/
license: BSD-3-Clause
license_file: LICENSE.txt
summary: The fundamental package for scientific computing with Python.
documentation: https://numpy.org/doc/stable/
repository: https://github.com/numpy/numpy
```
The build script for Unix:
```bash title="build.sh"
mkdir builddir
$PYTHON -m build -w -n -x \
-Cbuilddir=builddir \
-Csetup-args=-Dblas=blas \
-Csetup-args=-Dlapack=lapack
$PYTHON -m pip install dist/numpy*.whl
```
The build script for Windows:
```bat title="build.bat"
mkdir builddir
%PYTHON% -m build -w -n -x ^
-Cbuilddir=builddir ^
-Csetup-args=-Dblas=blas ^
-Csetup-args=-Dlapack=lapack
if %ERRORLEVEL% neq 0 exit 1
:: `pip install dist\numpy*.whl` does not work on windows,
:: so use a loop; there's only one wheel in dist/ anyway
for /f %%f in ('dir /b /S .\dist') do (
pip install %%f
if %ERRORLEVEL% neq 0 exit 1
)
```
### Running the recipe
Running this recipe with the variant config file will build a total of 2 `numpy` packages:
```bash
rattler-build build --recipe ./numpy
```
At the beginning of the build process, `rattler-build` will print the following message to show you the variants it found:
```txt
Found variants:
numpy-1.26.4-py311h5f8ada8_0
╭─────────────────┬───────────╮
│ Variant ┆ Version │
╞═════════════════╪═══════════╡
│ python ┆ 3.11 │
│ target_platform ┆ osx-arm64 │
╰─────────────────┴───────────╯
numpy-1.26.4-py312h440f24a_0
╭─────────────────┬───────────╮
│ Variant ┆ Version │
╞═════════════════╪═══════════╡
│ python ┆ 3.12 │
│ target_platform ┆ osx-arm64 │
╰─────────────────┴───────────╯
```