cibuildwheel

cibuildwheel vereinfacht die Erstellung von Python Wheels für die verschiedenen Plattformen und Python-Versionen durch Continuous Integration (CI) Workflows. Genauer gesagt baut es Manylinux-, macOS 10.9+- und Windows-Wheels für CPython und PyPy mit GitHub Actions, Azure Pipelines, Travis CI, AppVeyor, CircleCI, oder GitLab CI/CD.

Darüber hinaus bündelt es gemeinsam genutzte Bibliotheksabhängigkeiten unter Linux und macOS durch auditwheel und delocate.

Schließlich können die Tests auch gegen die Wheels laufen.

Siehe auch

Um Linux-, macOS- und Windows-Wheels erstellen zu können, erstellt eine .github/workflows/build_wheels.yml-Datei in eurem GitHub Repo:

.github/workflows/build_wheels.yml
name: Build

on:
  push:
    branches: [main]
  release:
    types:
      - published
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

permissions: {}
release

wird bei der Übertragung einer getaggten Version ausgeführt.

Siehe auch

workflow_dispatch

ermöglicht euch, in der grafischen Benutzeroberfläche auf eine Schaltfläche zu klicken, um einen Build auszulösen. Das ist perfekt zum manuellen Testen von Wheels vor einer Veröffentlichung eignet, da ihr sie einfach von artifacts herunterladen könnt.

Siehe auch

Nun können die Wheels gebaut werden mit:

jobs:
  build_wheels:
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        # macos-13 is an intel runner, macos-14 is apple silicon
        os: [ubuntu-latest, windows-latest, macos-13, macos-14]

    steps:
      - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
        with:
          persist-credentials: false

      - name: Build wheels
        uses: pypa/cibuildwheel@8d2b08b68458a16aeb24b64e68a09ab1c8e82084 # v3.4.1

Dadurch wird der CI-Workflow mit den folgenden Standardeinstellungen ausgeführt:

  • package-dir: .

  • output-dir: wheelhouse

  • config-file: "{package}/pyproject.toml"

Schließlich können die Wheels zum Python Package Index hochgeladen werden mit:

      - uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
        with:
          name: cibw-wheels-${{ matrix.os }}-${{ strategy.job-index }}
          path: ./wheelhouse/*.whl

Um Linux-, macOS- und Windows-Wheels mit GitLab CI/CD zu bauen, erstellt eine .gitlab-ci.yml-Datei in eurem Git-Repository:

.gitlab-ci.yml
linux:
  image: python:3.13
  services:
    - name: docker:dind
      entrypoint: ["env", "-u", "DOCKER_HOST"]
      command: ["dockerd-entrypoint.sh"]
  variables:
    DOCKER_HOST: tcp://docker:2375/
    DOCKER_DRIVER: overlay2
    # See https://github.com/docker-library/docker/pull/166
    DOCKER_TLS_CERTDIR: ""

    # skip all but the basic tests
    # (comment the below line in a PR to debug a Gitlab-specific issue)
    PYTEST_ADDOPTS: -k "unit_test or test_0_basic" --suppress-no-test-exit-code
  rules:
    - if: '$CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH =~ /^gitlab/'
      variables:
        CIBW_ENABLE: "all"
  script:
    - curl -sSL https://get.docker.com/ | sh
    - docker run --rm --privileged docker.io/tonistiigi/binfmt:latest --install all
    - python -m pip install -U pip
    - python -m pip install -e. pytest-custom-exit-code --group test
    - python ./bin/run_tests.py

windows:
  variables:
    PYTEST_ADDOPTS: -k "unit_test or test_0_basic" --suppress-no-test-exit-code
  rules:
    - if: '$CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH =~ /^gitlab/'
      variables:
        # Everything except graalpy. GraalPy is JVM-based and very slow to
        # start; on the small (2-core) GitLab SaaS Windows runners,
        # virtualenv's interpreter query for graalpy.exe times out, failing
        # the build. (Other platforms run the full "all" group.)
        CIBW_ENABLE: "cpython-prerelease pypy pypy-eol pyodide-eol pyodide-prerelease"
  script:
    - python -m pip install -U pip
    - python -m pip install -e. pytest-custom-exit-code --group test
    - python bin\run_tests.py
  tags:
    - saas-windows-medium-amd64

macos:
  image: macos-15-xcode-16
  variables:
    PYTEST_ADDOPTS: -k "unit_test or test_0_basic" --suppress-no-test-exit-code
  rules:
    - if: '$CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH =~ /^gitlab/'
      variables:
        CIBW_ENABLE: "all"
  script:
    - python3 -m pip install -U pip
    - python3 -m pip install -e. pytest-custom-exit-code --group test
    - python3 ./bin/run_tests.py
  tags:
    - saas-macos-medium-m1

Optionen

cibuildwheel kann entweder über Umgebungsvariablen oder über eine Konfigurationsdatei wie pyproject.toml konfiguriert werden, z.B.:

[tool.cibuildwheel]
build-verbosity = 1
test-command = "pytest {project}/tests"
test-requires = "pytest"
# support Universal2 for Apple Silicon:
macos.archs = [ "auto", "universal2" ]
macos.test-skip = [ "*universal2:arm64" ]

Beispiele