首页 > 解决方案 > 使用 Azure 函数,我们如何在命令行上运行 pylint 任务来检查每个函数?

问题描述

我们正在构建一个 Python 3.8 Azure 函数项目。我们安装了以下版本的 Pylint ...

$ pylint --version
pylint 2.6.0
astroid 2.4.2
Python 3.8.3 (v3.8.3:6f8c8320e9, May 13 2020, 16:29:34) 
[Clang 6.0 (clang-600.0.57)]

我们的目录结构如下所示...

function1
    __init__.py
function2
    __init__.py
...
tests
    __init.py

每个函数的init .py 文件都以传统的 Azure 函数方式布局......

import logging
...
def main(req: func.HttpRequest) -> func.HttpResponse:
    ...

但是,我想知道如何在命令行上针对我们所有的函数运行 Pylint,而不必指定每个函数。我试过这个

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

我们的意图是最终在我们的管道中创建一个任务,该任务将针对所有功能运行 Pylint。

标签: python-3.xcommand-lineazure-functionspylintazure-function-app

解决方案


这是 pylint 的已知问题:https ://github.com/PyCQA/pylint/issues/352

问题是您在执行扫描__init__.py的根目录中没有文件。pylint这就是 pylint 没有启动的原因。

在问题得到解决之前的解决方法:

在根目录中添加__init__.py,然后在 pylint 中添加,如果不再需要,则在扫描后将其删除。

解决方案:您位于根目录"./"中,所有./function1, ./function2,./tests都可用。

touch __init__.py;
pylint $(pwd)
rm __init__.py

它适用于任何地方:在 azure-pipelines.yaml 和 gitlab-ci.yml 以及本地。

我在我的模板项目中检查了它:

- script: |
    touch __init__.py
    pylint .
    rm __init__.py
  displayName: 'pylint .'

- script: |
    touch __init__.py
    pylint $(pwd)
    rm __init__.py
  displayName: 'pylint $pwd'

pylint .https ://dev.azure.com/Jet-Cat/azure-pipeline-pylint-template/_build/results?buildId=61&view=logs&j=00b6a206-eb91-585b-3d14-cf1a4d7b1970&t=9a9400f1-b707-5d54-e2fd -4fe1d8bc6dda

pylint $(pwd)http ://dev.azure.com/Jet-Cat/azure-pipeline-pylint-template/_build/results?buildId=61&view=logs&j=00b6a206-eb91-585b-3d14-cf1a4d7b1970&t=b5464ffe-109e-5907-849b -650dd16a8b4d


推荐阅读