使用 super-linter/[email protected] 时出现 PYTHON_PYLINT 模块导入错误

问题描述 投票:0回答:1

我一直在努力在 GitHub Action 中获取超级 linter 以接受我最近导入的 Python 项目一段时间,但没有成功。我正在使用

py3langid
在一个非常基本的凯撒密码实现中检测英语。

linting 错误是:

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
------

cipher.py 顶部

"""Caesar Cipher implementation"""

from py3langid.langid import MODEL_FILE, LanguageIdentifier

...

文件夹结构(为简洁起见缩短)

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

测试.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

在收到此错误之前,我没有在本地使用过

pylint
。它出现后我就跑了

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

并出现以下错误

************* 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)

添加

~/Projects/cryptography/__init__.py
后,
pylint
在本地运行良好,没有任何错误,但我的远程工作流程仍然失败,并出现与最初所述相同的错误。

知道我可能做错了什么吗?谢谢

编辑

我还尝试使用此SO问题在远程GitHub Actions运行器上创建一个

.pylintrc
来填充文件

PyLint“无法导入”错误 - 如何设置 PYTHONPATH?

~/.pylintrc
设置为以下内容不会影响超级 linter 结果

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

编辑2

尝试更新运行器上的

PYTHONPATH
以包含工作目录

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

这也无助于解决 linting 错误

编辑3

根据@Azeem的建议,我尝试将以下内容添加到

tests.yml
下的
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 }}

这是返回的错误

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

还有人有更多的想法吗?我什至不再为该项目编写代码,只是与 linter 作斗争。我可能需要在 GitHub 上打开一个问题。

感谢@Azeem 的参与

再次读取错误并尝试将位置

.pylintrc
设置为
/action/lib/.automation/.pylintrc
,但也失败了

/home/runner/work/_temp/91c876a5-f511-4156-91ca-5e23ad639603.sh: line 1: /action/lib/.automation/.pylintrc: No such file or directory
python-3.x github-actions super-linter
1个回答
0
投票

诚然,这不是一个很好的答案,因为问题仍然存在,我已经解决了这个问题并停止使用

super-linter
,因为尝试了很多选项但一无所获。

我在我的项目中安装了

pylint
,并在我的工作流程中添加了
raven-actions/actionlint@v2
作为单独的步骤。

我的 CI 自动化的 linting 部分现在看起来像这样:

---
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"

不再出现 linting 错误,并且运行速度也更快!我承认这不是原始问题的答案,但我现在可以安心睡觉了!

© www.soinside.com 2019 - 2024. All rights reserved.