我在 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
└── ...
也许我可能是相对导入的问题? 我会尝试使用 init.py 来初始化