由于 ModuleNotFoundError,Github Action 无法运行 pytest

Github Action failing to run pytest due to ModuleNotFoundError

提问人:Mariano 提问时间:11/17/2023 更新时间:11/17/2023 访问量:33

问:

我有一个 python 项目结构,使用 poetry 作为包管理器和 pytest 来运行测试。该项目的结构如下:

.github/
  workflows/
    test_workflow.yml
src/
  __init__.py
  exceptions.py
tests/
  test_utils.py
pyproject.toml
poetry.lock

现在,里面有一些测试,可以从以下位置导入一些异常:test_utils.pyexceptions.py

from src.exceptions import DateNotFoundError, ParameterNotFoundError

这一切都很好,在我的本地环境中运行工作正常,测试通过。poetry run pytestvenv

现在,我正在尝试设置一个 GitHub 操作,以在每次提交时运行测试。我所拥有的是这个:

name: test
run-name: Run unit tests
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v4
    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.11.4'
    - name: Install dependencies
      run: |
        python -m pip install poetry==1.6.1
        poetry install
    - name: Run tests
      run: poetry run pytest

这会在 github 操作中引发以下错误:ModuleNotFoundError: No module named 'src'

这是完整的堆栈跟踪:

==================================== ERRORS ====================================
_________________ ERROR collecting tests/test_utils.py __________________
ImportError while importing test module '/home/runner/work/MyProject/MyProject/tests/test_utils.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../../.cache/pypoetry/virtualenvs/myproject-n2wF6nQj-py3.11/lib/python3.11/site-packages/_pytest/python.py:617: in _importtestmodule
    mod = import_path(self.path, mode=importmode, root=self.config.rootpath)
../../../.cache/pypoetry/virtualenvs/myproject-n2wF6nQj-py3.11/lib/python3.11/site-packages/_pytest/pathlib.py:567: in import_path
    importlib.import_module(module_name)
/opt/hostedtoolcache/Python/3.11.4/x64/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1204: in _gcd_import
    ???
<frozen importlib._bootstrap>:1176: in _find_and_load
    ???
<frozen importlib._bootstrap>:1147: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:690: in _load_unlocked
    ???
../../../.cache/pypoetry/virtualenvs/myproject-n2wF6nQj-py3.11/lib/python3.11/site-packages/_pytest/assertion/rewrite.py:186: in exec_module
    exec(co, module.__dict__)
tests/test_utils.py:3: in <module>
    from src.exceptions import DateNotFoundError, ParameterNotFoundError
E   ModuleNotFoundError: No module named 'src'
=========================== short test summary info ============================
ERROR tests/test_utils.py
=============================== 1 error in 1.42s ===============================
make: *** [Makefile:18: test] Error 1
Error: Process completed with exit code 2.

我已经尝试了不同的 Github 操作来安装 poetry,通过 yaml 文件启用虚拟环境并添加到工作流程中,但都产生了完全相同的结果。谢谢!export PYTHONPATH="$PYTHONPATH:$PWD"

python 持续集成 pytest github-actions python-poetry

评论


答:

1赞 Mariano 11/17/2023 #1

此问题已通过向目录添加文件得到解决。__init__.pytests/