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

GitHub Actions缓存Python依赖后找不到mypy命令该如何解决?

错误原因

你当前配置的缓存路径~.cache/pip是pip的下载缓存目录,仅用于存储pip拉取的安装包文件,避免重复下载依赖包,并不会缓存实际安装到运行环境中的依赖文件及可执行程序。因此缓存命中时你跳过了依赖安装步骤,运行环境中根本没有安装mypy等依赖,自然会抛出命令未找到的错误。
另外你填写的缓存路径存在拼写错误,正确的pip下载缓存路径应为~/.cache/pip,缺少的斜杠会导致缓存路径匹配失效。

解决方案

提供两种常用的稳定方案,按需选择即可:

方案1:缓存虚拟环境(更推荐,隔离性强)

将所有依赖安装到独立虚拟环境中,直接缓存整个虚拟环境目录,缓存命中后直接激活即可使用所有依赖:

steps:
  - name: Check out repository code
    uses: actions/checkout@v4

  - name: Set up Python
    uses: actions/setup-python@v5
    with:
      python-version: "3.10" # 替换为你需要的Python版本

  - name: Cache virtual environment
    uses: actions/cache@v3
    id: venv-cache
    with:
      path: venv
      key: ${{ runner.os }}-venv-${{ hashFiles('**/requirements.txt') }}
      restore-keys: |
        ${{ runner.os }}-venv-

  - name: Create venv and install dependencies
    if: steps.venv-cache.outputs.cache-hit != 'true'
    run: |
      python -m venv venv
      source venv/bin/activate
      pip install --upgrade pip
      pip install -r requirements.txt

  - name: Run mypy
    run: |
      source venv/bin/activate
      mypy .

方案2:直接缓存用户级依赖安装目录

不需要虚拟环境的场景下,使用--user参数将依赖安装到用户目录,同时缓存该目录和pip下载缓存,再将pip可执行文件路径加入系统PATH即可:

steps:
  - name: Check out repository code
    uses: actions/checkout@v4

  - name: Set up Python
    uses: actions/setup-python@v5
    with:
      python-version: "3.10" # 替换为你需要的Python版本

  - name: Cache pip dependencies
    uses: actions/cache@v3
    id: pip-cache
    with:
      paths: |
        ~/.cache/pip
        ~/.local
      key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
      restore-keys: |
        ${{ runner.os }}-pip-

  - name: Install dependencies
    if: steps.pip-cache.outputs.cache-hit != 'true'
    run: pip install --user -r requirements.txt

  - name: Add pip executable path to system PATH
    run: echo "$HOME/.local/bin" >> $GITHUB_PATH

  - name: Run mypy
    run: mypy .

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 23:27:03