'Cannot import script from repository in pytest using GitHub actions

I am learning to implement automatic testing using GitHub actions. I am trying to solve the following import error.

The error is:

utils\test_capitalize.py:2: in <module>
    from src.capital import capital_case
E   ModuleNotFoundError: No module named 'src'

The structure of my repository is:

example/
|-- setup.cfg
|-- setup.py
|-- pyproject.toml
|-- .github/
|   |-- workflows/
|   |   |-- ci.yml
|-- src/
|   |-- capital.py
|-- utils/
|   |-- test_capitalize.py

The content of ci.yml is:

name: ci

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build-and-test:

    runs-on: windows-latest
    strategy:
      matrix:
        python-version: [3.8]
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v2
        with:
          python-version: ${{ matrix.python-version }}
      - name: Build
        run: |
          python -m pip install .
          python -m pip install src
      - name: Test with pytest
        run: |
          python -m pip install pytest
          pytest

I thought that python -m pip install . already installed all the modules in the repository, do I have a misconception about it?.

Edit: The pytest file is a minimum example (I have tried removing the src before pushing):

import pytest
from src.capital import capital_case


def test_capital_case():
    assert capital_case('semaphore') == 'Semaphore'


Solution 1:[1]

Include these lines in the header of your script before importing the Module.

import sys

sys.path.append("../")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shivam Raj