导入错误:在 GitHub Actions 中运行 pytest 时没有名为“src”的模块

问题描述 投票:0回答:1

我在 GitHub Actions 中使用 pytest 运行 Python 测试时遇到问题。测试在我的本地计算机上正常工作,但在 GitHub Actions 工作流程中,我收到 ImportError: No module named 'src' 错误。

    ============================= test session starts ==============================
  platform linux -- Python 3.11.6, pytest-7.4.2, pluggy-1.3.0 
    ...
    tests/test_compare.py:1: in <module>
        from src.functions.compare import imageCompare
    E   ModuleNotFoundError: No module named 'src'
    ...

GitHub Actions 工作流程:

runs-on: ubuntu-latest
steps:
    - name: Get Code
      uses: actions/checkout@v4

    - name: Install Python
      uses: actions/setup-python@v4

    - name: Install Dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt

    - name: Activate Virtual Environment
      run: |
        python -m venv complianceEnv
        source complianceEnv/bin/activate

    - name: Run Tests
      run: |
        make test

当地环境:

Python版本:3.11.4 操作系统:MacOS 虚拟环境:是

目录结构:

compliance-image-compare/
├── src/
│   └── functions/
│       └── compare.py
├── tests/
│   └── test_compare.py
├── Makefile
└── ...
python makefile pytest github-actions virtualenv
1个回答
0
投票

也许我可能是相对导入的问题? 我会尝试使用 init.py 来初始化

© www.soinside.com 2019 - 2024. All rights reserved.