0

我在 python 3.10 中使用诗歌 1.1.12。我的项目依赖于 numpy 1.21.1,每次运行持续集成管道时都需要 5 分钟的时间来安装。

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

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

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

我的 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
4

1 回答 1

1

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

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

但是从 numpy 1.21.2 开始,python 也有了最高版本。Numpy 1.21.2(在撰写此答案时为 1.21.5)需要大于 3.7 但严格低于 3.11 的 python 版本。以下是 numpy/python 兼容性的总结:

麻木版本 蟒蛇版本
1.21.0 蟒蛇> = 3.7
1.21.1 蟒蛇> = 3.7
1.21.2 蟒蛇 >=3.7,<3.11
... ...
1.21.5 蟒蛇 >=3.7,<3.11

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

python = "^3.10"

这与 numpy 1.21.2 和更高的 python 版本要求冲突。因此,poetry 决定安装与我的 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 版,如this answer中所述

现在,我poetry install在构建过程中的步骤不到 25 秒,而不是 5 分钟。

于 2021-12-20T13:12:02.573 回答