Search code examples
pythonpython-3.xgithub-actionssuper-linter

Module import error for PYTHON_PYLINT when using super-linter/[email protected]


I've been working on getting super-linter within a GitHub Action to accept my recent Python project import for a while now without success. I am using py3langid to detect English language in a pretty basic Caesar Cipher implementation.

The linting error is:

PYTHON_PYLINT
2024-07-20 21:05:46 [INFO]   Linting PYTHON_PYLINT items...
Error: -20 21:05:47 [ERROR]   Found errors when linting PYTHON_PYLINT. Exit code: 1.
2024-07-20 21:05:47 [INFO]   Command output for PYTHON_PYLINT:
------
************* Module cipher
caesar_cipher/cipher.py:3:0: E0401: Unable to import 'py3langid.langid' (import-error)
-----------------------------------
Your code has been rated at 8.91/10
------

Top of cipher.py

"""Caesar Cipher implementation"""

from py3langid.langid import MODEL_FILE, LanguageIdentifier

...

Folder structure (shortened for brevity)

~/Projects/cryptography/
    .git
    .github/
        workflows/
            tests.yml
    ...
    caesar_ciper/
        cipher.py
        test_cipher.py
    ...
    __init__.py
    requirements.txt

tests.yml

---
name: Test project
on: push
permissions: read-all
jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Lint
        uses: super-linter/[email protected]
        env:
          # To report GitHub Actions status checks
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  test:
    needs: lint
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Install Python
        uses: actions/setup-python@v5
        with:
          python-version: "3.10"
      - name: Install dependencies
        run: pip install -r requirements.txt
      - name: Run tests
        run: pytest

I had not used pylint locally before receiving this error. After it appeared I ran

docker run --rm -v $(pwd):/data cytopia/pylint .

and got the following error

************* Module .
__init__.py:1:0: F0010: error while code parsing: Unable to load file __init__.py:
[Errno 2] No such file or directory: '__init__.py' (parse-error)

After adding ~/Projects/cryptography/__init__.py, pylint ran fine locally without any errors, but my remote workflow still fails with the same error as originally stated.

Any idea what I could be doing wrong? Thanks

EDIT

I also tried creating a .pylintrc on the remote GitHub Actions runner using this SO question to populate the file

PyLint "Unable to import" error - how to set PYTHONPATH?

setting ~/.pylintrc to the following didn't effect the super-linter result

[MASTER]
init-hook='import sys; sys.path.append("./")'

EDIT 2

Tried to update PYTHONPATH on the runner to include the working directory

...
- name: Set PYTHONPATH
  run: export PYTHONPATH=${PYTHONPATH}:${pwd}
...

This also hasn't helped to resolve the linting error

EDIT 3

As per @Azeem suggestion I tried adding the following to tests.yml under lint

...
steps
...
  - name: Create .pylintrc  # <- this
    run: echo "[MASTER]" > ~/.pylintrc && echo "init-hook='import sys; sys.path.append("./")'" >> ~/.pylintrc  # <- this
  - name: Lint
    uses: super-linter/[email protected]
    env:
      PYTHON_PYLINT_CONFIG_FILE: ~/.pylintrc    # <- this
      # To report GitHub Actions status checks
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

This is the error that came back

[FATAL]     -> PYTHON_PYLINT_LINTER_RULES rules file (/action/lib/.automation/~/.pylintrc) doesn't exist. Terminating...

Does anyone have any more ideas? I'm not even coding the project any more, just fighting the linter. I might have to open an issue on GitHub.

Thanks to @Azeem for chipping in

Read error again and tried to set location .pylintrc to /action/lib/.automation/.pylintrc which also failed

/home/runner/work/_temp/91c876a5-f511-4156-91ca-5e23ad639603.sh: line 1: /action/lib/.automation/.pylintrc: No such file or directory

Solution

  • Admittedly this is not a great answer as the problem still exists, I have worked around it and stopped using super-linter due to trying so many options and getting nowhere.

    I have instead installed pylint in my project as well as added raven-actions/actionlint@v2 as a separate step in my workflow.

    The linting part of my CI automation now looks like this:

    ---
    name: Linting
    on: push
    permissions: read-all
    jobs:
      lint:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout code
            uses: actions/checkout@v4
            with:
              fetch-depth: 0
          - name: Install Python
            uses: ./.github/actions/install-py/
          - name: Run pylint
            run: pylint ./**/*.py
          - name: Run actionlint
            uses: raven-actions/actionlint@v2
            with:
              files: ".github/workflows/*.yml"
    

    No more linting errors, and runs much faster as well! I acknowledge this is not an answer to the original issue, but I can sleep in peace now!