【问题标题】:Github action using wrong version of Python使用错误版本的 Python 的 Github 操作
【发布时间】:2022-10-02 05:40:41
【问题描述】:

我有以下 Github 操作,其中我指定了 Python 3.10:

name: Unit Tests
runs-on: ubuntu-latest
defaults:
  run:
    shell: bash
    working-directory: app
steps:
  - uses: actions/checkout@v3
  - name: Install poetry
    run: pipx install poetry
  - uses: actions/setup-python@v3
    with:
      python-version: \"3.10\"
      cache: \"poetry\"
  - run: poetry install
  - name: Run tests
    run: |
      make mypy
      make test

pyproject.toml 也指定了 Python 3.10:

[tool.poetry.dependencies]
python = \">=3.10,<3.11\"

当动作运行时,我得到以下信息:

The currently activated Python version 3.8.10 is not supported by the project 
(>=3.10,<3.11).
Trying to find and use a compatible version. 
Using python3 (3.10.5)

看起来它使用的是 3.10,但 py.test 使用的是 3.8.10:

platform linux -- Python 3.8.10, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- 
/home/runner/.cache/pypoetry/virtualenvs/vital-background-pull-yluVa_Vi-py3.10/bin/python

对于上下文,此 Github 操作之前在 3.8 上运行。我已经更新了test.yamlpyproject.toml 中的python 版本,但它仍在使用3.8。我还应该更改什么以使其使用 3.10?

谢谢

  • 疯狂的猜测:将 actions/checkout@v3 移动为第一步,看看是否有帮助?
  • 我发现问题可能出在pipx install poetry:installed package poetry 1.1.14, installed using Python 3.8.10
  • 您是否在使用 setup 操作设置 python 版本后尝试安装诗歌?

标签: python github devops github-actions


【解决方案1】:

根本原因是节

- uses: actions/setup-python@v3
  with:
    python-version: "3.10"
    cache: "poetry"

使用行缓存poetry。由于 poetry 之前安装了与 Python 3.8 关联的 pip,因此将从与该 Python 版本关联的缓存中检索包。它需要使用新的 Python 版本重新安装。

您可以从单个 GH 操作执行中删除 cache: poetry,或 remove the cache manually。这将解决您的问题。

【讨论】:

  • 我不认为这是它。我用一个新的工作流设置了同样的东西,没有缓存,我明确地使用cache-dependency-path,你必须在 setup-python 或 setup-python 失败之前安装诗歌,因为它调用poetry env use
【解决方案2】:

Pipx 可能会使用意外版本的 python 安装诗歌。您可以指定要使用的 python 版本:

pipx install poetry --python $(which python)
# or
pipx install poetry --python python3.10

这是我在setup-python@v3 步骤之后所做的。

您还可以指定预期 python 版本的路径,those are available in the github docs。这将允许您按照上面的顺序执行cache: poetry 步骤。

在阅读how does pipx know which python to use 后,我遇到了类似的问题并解决了它。我没有使用 pyenv,因为我在 setup-python@v3 中指定了版本。

您还可以在 python 设置步骤之后安装诗歌以确保您的版本可用,假设您执行了python-version: "3.10.12" 或其他操作。然后剩下的就是缓存,可能是using the cache actionsetup-python 步骤分开。

【讨论】:

    【解决方案3】:

    就我而言,这是因为我的 pyproject.toml 位于存储库的子目录中。

    我的 actions/setup-python@v4 操作的日志如下所示:

    /opt/pipx_bin/poetry env use /opt/hostedtoolcache/Python/3.9.14/x64/bin/python
    
    Poetry could not find a pyproject.toml file in /home/runner/work/PLAT/PLAT or its parents
    Warning: 
    Poetry could not find a pyproject.toml file in /home/runner/work/PLAT/PLAT or its parents
    

    但操作成功完成。后来,诗不知道用什么python,因为它无法写到它的全局envs.toml。最终我发现actions/setup-python 中有an open issue for this

    使固定

    你可以做两件事之一。最简单的就是作弊:

    runs-on: ubuntu-22.04
    

    ubuntu-22.04 图像 has Python 3.10 baked in,所以你可以忘记切换 python 并且暂时没问题。

    更好的解决方法是在setup-python 之后但在poetry install 之前添加一个步骤:

       - run: poetry env use ${pythonLocation}/bin/python
         working-directory: wherever/your/pyproject.toml/is
    

    【讨论】:

      猜你喜欢
      • 2020-07-07
      • 1970-01-01
      • 2020-04-09
      • 2018-05-07
      • 1970-01-01
      • 2020-09-19
      • 2021-04-19
      • 1970-01-01
      • 2020-07-22
      相关资源
      最近更新 更多