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

使用Github Actions执行测试时遇文件找不到错误求助

问题原因

你遇到的FileNotFoundError是因为Python脚本的当前工作目录和本地运行时不一致:

  • 本地运行时,你大概率是在src目录下执行python test.py,此时../test/data/pipeline.json会正确指向仓库根目录下的test/data/pipeline.json。
  • 但在Github Actions中,你是在仓库根目录执行python src/test.py,此时Python的工作目录是根目录,../test/data会尝试从根目录向上查找,自然找不到目标文件。
两种解决方案

方案1:修改test.py中的路径(推荐,更健壮)

不要依赖当前工作目录,而是基于test.py文件的绝对路径来构建目标文件路径,这样不管在哪里执行脚本都不会出错:

import os

# 获取test.py文件的绝对路径所在目录
script_dir = os.path.dirname(os.path.abspath(__file__))
# 拼接得到pipeline.json的绝对路径
pipeline_path = os.path.join(script_dir, "../test/data/pipeline.json")
# 用normpath简化路径(可选)
pipeline_path = os.path.normpath(pipeline_path)

with open(pipeline_path, "r") as p:
    # 你的业务代码

方案2:修改Github Actions的执行命令

在执行测试脚本前先切换到src目录,保持和本地运行时的工作目录一致:
修改python-test.yml中Run tests步骤的最后一行:

- name: Run tests
  shell: bash -l {0}
  run: |
    conda install pdal
    python -m pip install --upgrade pip
    pip install dask distributed pdal
    cd src && python test.py
额外建议

如果还有其他测试文件依赖相对路径,优先用方案1的绝对路径写法,避免后续再出现类似路径问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 11:00:48