|
3 | 3 | Writing a Python package is fairly straightforward, especially for "Python-only" packages.
|
4 | 4 | In the second example we will build a package for `numpy` which contains compiled code.
|
5 | 5 |
|
| 6 | +## Generating a starter recipe |
| 7 | + |
| 8 | +Rattler-build provides a command to generate a recipe for a package from PyPI. |
| 9 | +The generated recipe can be used as a starting point for your recipe. |
| 10 | +The recipe generator will fetch the metadata from PyPI and generate a recipe that will build the package from the `sdist` source distriution. |
| 11 | + |
| 12 | +```bash |
| 13 | +rattler-build generate-recipe pypi ipywidgets |
| 14 | +# select an older version of the package |
| 15 | +rattler-build generate-recipe pypi ipywidgets --version 8.0.0 |
| 16 | +``` |
| 17 | + |
6 | 18 | ## A Python-only package
|
7 | 19 |
|
8 | 20 | The following recipe uses the `noarch: python` setting to build a `noarch` package that can be installed on any platform without modification.
|
@@ -210,3 +222,118 @@ numpy-1.26.4-py312h440f24a_0
|
210 | 222 | │ target_platform ┆ osx-arm64 │
|
211 | 223 | ╰─────────────────┴───────────╯
|
212 | 224 | ```
|
| 225 | + |
| 226 | +## An ABI3-compatible package |
| 227 | + |
| 228 | +Certain packages contain compiled code that is compatible with multiple Python versions. |
| 229 | +This is the case e.g. for a lot of Rust / PyO3 based Python extensions. |
| 230 | + |
| 231 | +In this case, you can use the special `abi3` settings to build a package that is specific to a certain operating system and architecture, but compatible with multiple Python versions. |
| 232 | + |
| 233 | +Note: this feature relies on the `python-abi3` package which exists in the `conda-forge` channel. |
| 234 | +The full recipe can be found on [`conda-forge/py-rattler-feedstock`](https://github.com/conda-forge/py-rattler-feedstock) |
| 235 | + |
| 236 | +```yaml title="recipe.yaml" |
| 237 | +context: |
| 238 | + name: py-rattler |
| 239 | + python_name: py_rattler |
| 240 | + version: "0.11.0" |
| 241 | + python_min: "3.8" |
| 242 | + |
| 243 | +package: |
| 244 | + name: py-rattler |
| 245 | + version: ${{ version }} |
| 246 | + |
| 247 | +source: |
| 248 | + url: https://pypi.org/packages/source/${{ name[0] }}/${{ name }}/${{ python_name }}-${{ version }}.tar.gz |
| 249 | + sha256: b00f91e19863741ce137a504eff3082c0b0effd84777444919bd83357530867f |
| 250 | + |
| 251 | +build: |
| 252 | + number: 0 |
| 253 | + script: build.sh |
| 254 | + python: |
| 255 | + version_independent: true |
| 256 | + |
| 257 | +requirements: |
| 258 | + build: |
| 259 | + - ${{ compiler('c') }} |
| 260 | + - ${{ compiler('rust') }} |
| 261 | + - cargo-bundle-licenses |
| 262 | + host: |
| 263 | + - python ${{ python_min }}.* |
| 264 | + - python-abi3 ${{ python_min }}.* # (1)! |
| 265 | + - maturin >=1.2.2,<2 |
| 266 | + - pip |
| 267 | + - if: unix |
| 268 | + then: |
| 269 | + - openssl |
| 270 | + run: |
| 271 | + - python >=${{ python_min }} |
| 272 | + |
| 273 | +tests: |
| 274 | + - python: |
| 275 | + imports: |
| 276 | + - rattler |
| 277 | + python_version: ["${{ python_min ~ '.*' }}"] # (2)! |
| 278 | + # You could run `abi3audit` here, but it is not necessary |
| 279 | + # - script: |
| 280 | + # - abi3audit ${{ SP_DIR }}/spam.abi3.so -s -v --assume-minimum-abi3 ${{ python_min }} |
| 281 | + # requirements: |
| 282 | + # run: |
| 283 | + # - abi3audit |
| 284 | + |
| 285 | +about: |
| 286 | + homepage: https://github.com/conda/rattler |
| 287 | + license: BSD-3-Clause |
| 288 | + license_file: |
| 289 | + - LICENSE |
| 290 | + - py-rattler/THIRDPARTY.yml |
| 291 | + summary: A blazing fast library to work with the conda ecosystem |
| 292 | + description: | |
| 293 | + Rattler is a library that provides common functionality used within the conda |
| 294 | + ecosystem. The goal of the library is to enable programs and other libraries to |
| 295 | + easily interact with the conda ecosystem without being dependent on Python. Its |
| 296 | + primary use case is as a library that you can use to provide conda related |
| 297 | + workflows in your own tools. |
| 298 | + repository: https://github.com/conda/rattler |
| 299 | +``` |
| 300 | +
|
| 301 | +1. The `python-abi3` package is a special package that ensures that the run dependencies |
| 302 | + are compatible with the ABI3 standard. |
| 303 | +2. The `python_version` setting is used to test against the oldest compatible Python version. |
| 304 | + |
| 305 | +## Testing Python packages |
| 306 | + |
| 307 | +Testing Python packages is done using the `tests` section of the recipe. |
| 308 | +We can either use a special "python" test or a regular script test to test the package. |
| 309 | + |
| 310 | +All tests will have the current package and all it's run dependencies installed in an isolated environment. |
| 311 | + |
| 312 | +```yaml title="recipe.yaml" |
| 313 | +# contents of the recipe.yaml file |
| 314 | +tests: |
| 315 | + - python: |
| 316 | + # The Python test type will simply import packages as a sanity check. |
| 317 | + imports: |
| 318 | + - rattler |
| 319 | + - rattler.version.Version |
| 320 | + # You can select different Python versions to test against. |
| 321 | + python_version: ["${{ python_min ~ '.*' }}", "3.12.*"] # (1)! |
| 322 | +
|
| 323 | + # You can run a script test to run arbitrary code. |
| 324 | + - script: |
| 325 | + - pytest ./tests |
| 326 | + requirements: # (2)! |
| 327 | + run: |
| 328 | + - pytest |
| 329 | + files: # (3)! |
| 330 | + source: |
| 331 | + - tests/ |
| 332 | + # You can also directly execute a Python script and run some tests from it. |
| 333 | + # The script is searched in the `recipe` directory. |
| 334 | + - script: mytest.py |
| 335 | +``` |
| 336 | +
|
| 337 | +1. The `python_version` setting is used to test against different Python versions. It is useful to test against the minimum version of Python that the package supports. |
| 338 | +2. We can add additional requirements for the test run. such as pytest, pytest-cov, ... – you can also specify a `python` version here by adding e.g. `python 3.12.*` to the run requirements. |
| 339 | +3. This will copy over the tests from the source directory into the package. Note that this makes the package larger, so you might want to use a different approach for larger packages. |
0 commit comments