在构建过程中使用 github 操作执行诗歌安装时,使用预编译的 numpy 包而不是构建它

Use precompiled numpy package instead of building it when performing poetry install during build with github actions

我正在使用 poetry 1.1.12 和 python 3.10。我的项目依赖于 numpy 1.21.1,每次我 运行 我的持续集成管道安装需要 5 分钟。

有没有办法让 poetry 使用某种已编译的 numpy 包而不是每次构建都重新构建它?

我已经按照 中描述的步骤缓存我的虚拟环境存储库来缓解这个问题,但我想要一个即使我更改 poetry.lock 文件或我的缓存也能正常工作的解决方案已过期。

由于公司政策规定,我只能在 github 操作中使用 ubuntu-latest 图片

我的pyproject.toml

[tool.poetry]
name = "test-poetry"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]

[tool.poetry.dependencies]
python = "^3.10"
numpy = "^1.21.1"

[tool.poetry.dev-dependencies]
pytest = "^6.2.5"

[build-system]
requires = ["poetry-core>=1.1.12"]
build-backend = "poetry.core.masonry.api"

我的 github 操作流程:

name: Continuous Integration

on: push

jobs:
  test-build:
    runs-on: ubuntu-latest

    steps: 
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Install Python 3.10
        uses: actions/setup-python@v2
        with:
          python-version: '3.10'

      - name: Install Poetry packaging manager
        run: curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/install-poetry.py | python -

      - name: Configure Poetry
        run: |
          poetry config virtualenvs.create true
          poetry config virtualenvs.in-project true

      - name: Load cached venv
        id: cached-poetry-dependencies
        uses: actions/cache@v2
        with:
          path: .venv
          key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}

      - name: Install project dependencies
        run: poetry install
        if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'

      - name: Run test
        run: poetry run pytest

Poetry 默认使用预编译包(如果存在)。为 python 版本设置上限使我的构建使用更新版本的 numpy,它已经为我的 python

版本预编译

在numpy 1.21.2之前,只设置了python的最低版本。 Numpy 1.21.1 要求 python 版本高于 3.7

但是从numpy 1.21.2开始,还有一个最大版本python。 Numpy 1.21.2(在撰写此答案时为 1.21.5)要求 python 版本大于 3.7 但严格低于 3.11。以下是 numpy/python 兼容性的摘要:

numpy version python version
1.21.0 Python >=3.7
1.21.1 Python >=3.7
1.21.2 Python >=3.7, <3.11
... ...
1.21.5 Python >=3.7, <3.11

在我的 pyproject.toml 中,我将 python 版本设置如下:

python = "^3.10"

这与 numpy 1.21.2 及更高 python 版本要求冲突。因此,诗歌决定安装与我的 python 版本兼容的最新版本,即 1.21.1。但是 numpy 版本 1.21.1 没有为 python 3.10 预编译,第一个为 python 3.10 预编译的 numpy 版本是 numpy 1.21.2.

所以每次我安装诗歌项目时,我都必须从源代码重建 numpy。

为了纠正这个问题,我更改了 pyproject.toml 依赖项部分,如下所示:

[tool.poetry.dependencies]
python = ">=3.10,<3.11"
numpy = "^1.21.5"

现在我构建的 poetry install 部分检索预编译的 python 3.10 numpy 版本 1.21.5 而不是编译 numpy 版本 1.21.1,如

现在我的 poetry install 构建步骤只需要不到 25 秒而不是 5 分钟。