[Python] Adjusting $PYTHONPATH for Pytest in GitHub Actions

Discussão em 'Python' iniciado por Stack, Março 9, 2025.

  1. Stack

    Stack Membro Participativo

    The prelude for the existence of this problem is that I am trying to figure out the best way to import modules from parallel folders. The current build looks like such:

    Repository
    | Encompassing_Folder
    || src
    ||- __init__.py
    ||- alice.py
    |
    || tests
    ||- test_a.py
    |
    |- __init__.py


    I use direct imports so when running test_a.py manually, I specify the path with python -m Ecnompassing_Folder.tests.test_a which allows the builder to find the parent folder and its full contents.

    The goal is to have Pytest, which I'm trying to set-up in GitHub Actions, see the parent folder as well. Below I give my workflow file if that is of any use.

    So far I have tried changing the PYTHONPATH to ${{ github.workspace }} and to the direct path seen in the file presently, but the majority of my efforts have gone into figuring out how not to have to solve this problem in the first place.

    name: Python application

    on:
    push:
    branches: [ "main", "release/*" ]
    pull_request:
    branches: [ "main" ]

    permissions:
    contents: read

    jobs:
    build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    - name: Set up Python 3.10
    uses: actions/setup-python@v3
    with:
    python-version: "3.10"
    - name: Install dependencies
    run: |
    python -m pip install --upgrade pip
    pip install flake8 pytest
    if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Lint with flake8
    run: |
    # stop the build if there are Python syntax errors or undefined names
    flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
    # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
    flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
    - name: Test with pytest
    env:
    PYTHONPATH: ./Encompassing_Folder:./Encompassing_Folder/src
    run: |
    pytest

    Continue reading...

Compartilhe esta Página