pylint not finding modules from within Github Actions workflow

I am trying to use pylint on a small demo python backend project from within a github action. This fails with the following error:

Run pylint src
  pylint src
  shell: /usr/bin/bash -e {0}
  env:
    pythonLocation: /opt/hostedtoolcache/Python/3.10.9/x64
    LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.10.9/x64/lib
************* Module src
src:1:0: F0001: No module named src (fatal)
Error: Process completed with exit code 1.

while the command pylint src run locally in the project root directory succeeds as follows:

(base) bob@Roberts-Mac-mini myproject % pylint src

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

Here is my github actions workflow:

name: Python application

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

permissions:
  contents: read

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - 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 pylint pytest pytest-cov
        if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
    - name: Lint with pylint
      run: |
        pylint src
      continue-on-error: false
    - name: Test with pytest
      run: |
        pytest
    - name: pytest coverage
      run:
        pytest --cov=./ --cov-report=xml:tests/coverage.xml
    - name: Upload coverage to Codecov
      uses: codecov/codecov-action@v3
    

and this is my project tree/filesystem:

(base) bob@Roberts-Mac-mini myproject % tree
.
├── LICENSE
├── README.md
├── src
│   ├── __init__.py
│   ├── __pycache__
│   │   └── __init__.cpython-311.pyc
│   └── addsub
│       ├── __init__.py
│       ├── __pycache__
│       │   ├── __init__.cpython-311.pyc
│       │   ├── __init__.cpython-38.pyc
│       │   ├── add.cpython-311.pyc
│       │   ├── add.cpython-38.pyc
│       │   ├── subtract.cpython-311.pyc
│       │   └── subtract.cpython-38.pyc
│       ├── add.py
│       └── subtract.py
└── tests
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-311.pyc
    │   ├── __init__.cpython-38.pyc
    │   ├── test_add.cpython-311-pytest-7.2.1.pyc
    │   ├── test_add.cpython-38-pytest-7.2.1.pyc
    │   ├── test_sub.cpython-311-pytest-7.2.1.pyc
    │   └── test_sub.cpython-38-pytest-7.2.1.pyc
    ├── coverage.xml
    ├── test_add.py
    └── test_sub.py

7 directories, 23 files

>Solution :

You need to checkout the repository first, so your workflow can access it.

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    
    - name: Check out
      uses: actions/checkout@v3

    - name: Set up Python 3.10
      uses: actions/setup-python@v3
      with:
        python-version: "3.10"
    .....
    .....

Leave a Reply