You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitLab CI中Poetry命令找不到问题及正确配置咨询

GitLab CI中解决poetry: command not found问题

问题根源:GitLab CI的每个Job都是独立运行在全新的Docker容器中,上一个Job(install阶段)安装的软件和环境不会自动传递到下一个Job,因此lint阶段的容器中没有poetry。

以下是两种可行的解决配置方案:

方案一:全局前置脚本+缓存依赖

image: python:latest

stages:
  - install
  - lint
  - test

before_script:
  - python --version ; pip --version  # 调试版本信息
  - pip install poetry
  - export PATH="$HOME/.local/bin:$PATH"  # 将poetry所在路径加入系统环境变量

# 缓存虚拟环境和依赖包,减少重复下载安装
cache:
  paths:
    - .venv/
    - ~/.cache/pip/
    - ~/.local/bin/

install_dependencies:
  stage: install
  script:
    - poetry install  # 创建虚拟环境并安装所有依赖

pre-commit:
  stage: lint
  script:
    - poetry run pre-commit run --all-files

pytest:
  stage: test
  script:
    - poetry run pytest

说明

  • 全局before_script会在每个Job执行前运行,确保每个容器都安装了poetry并配置好环境变量
  • 缓存配置会保留虚拟环境和pip缓存,后续Job无需重复安装依赖,提升运行速度

方案二:使用Poetry官方安装脚本(更可靠)

image: python:latest

stages:
  - install
  - lint
  - test

before_script:
  - python --version ; pip --version
  # 通过官方脚本安装Poetry,避免pip安装的版本兼容性问题
  - curl -sSL https://install.python-poetry.org | python3 -
  - export PATH="$HOME/.local/bin:$PATH"

cache:
  paths:
    - .venv/
    - ~/.cache/pip/
    - ~/.local/bin/
    - ~/.cache/pypoetry/

install_dependencies:
  stage: install
  script:
    - poetry install

pre-commit:
  stage: lint
  script:
    - poetry run pre-commit run --all-files

pytest:
  stage: test
  script:
    - poetry run pytest

说明

  • 使用Poetry官方提供的安装脚本,能确保安装到稳定兼容的版本
  • 同样通过缓存机制复用已安装的依赖和虚拟环境,节省时间

内容的提问来源于stack exchange,提问作者Mateusz Kiersnowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 07:07:27