为目录和所有子目录中的所有 Python 文件运行 Pylint

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36873096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:28:50  来源:igfitidea点击:

Run Pylint for all Python files in a directory and all subdirectories

pythonpylint

提问by Alan Evangelista

I have

我有

find . -iname "*.py" -exec pylint -E {} ;\

and

FILES=$(find . -iname "*.py")
pylint -E $FILES

If I understand correctly, the first command will run pylint for each of the Python files, the second one will run pylint once for all files. I expected that both commands would return the same output, but they return different results. I think this diff is somehow related to imports and F (failure) pylint messages, which occurs when a import fails and is notoutput by pylint -E.

如果我理解正确,第一个命令将为每个 Python 文件运行 pylint,第二个命令将为所有文件运行一次 pylint。我预计这两个命令会返回相同的输出,但它们返回不同的结果。我认为这个差异在某种程度上与导入和 F(失败)pylint 消息有关,当导入失败并且pylint -E输出时会发生这种情况。

Has someone already experienced this and could explain why the diff happens and what is the best way to run pylint?

有人已经经历过这个并且可以解释为什么会发生差异以及运行 pylint 的最佳方法是什么?

回答by duhaime

Just pass the directory name to the pylint command. To lint all files in ./server:

只需将目录名称传递给 pylint 命令。要 lint 中的所有文件./server

pylint server

回答by sonus21

My one cent

我的一分钱

find . -type f -name "*.py" | xargs pylint 

How does it work?

它是如何工作的?

findfinds all files ends with pyand pass to xargs, xargsruns pylintcommand on each file.

find找到所有以 结尾的文件py并传递给xargs,在每个文件上xargs运行pylint命令。

NOTE: You can give any argument to pylintcommand as well.

注意:您也可以为pylint命令提供任何参数。

EDIT:

编辑:

According to docwe can use

根据文档我们可以使用

  1. pylint mymodule.py

  2. pylint directory/mymodule.py

  1. pylint mymodule.py

  2. pylint 目录/mymodule.py

number 2will work if directory is a python package (i.e. has an __init__.pyfile or it is an implicit namespace package) or if “directory” is in the python path.

如果目录是一个 python 包(即有一个__init__.py文件或者它是一个隐式命名空间包)或者如果“目录”在 python 路径中,数字 2将起作用。

回答by chilicheech

To run pylint on all *.py files in a directory and its subdirectories, you can run:

要对目录及其子目录中的所有 *.py 文件运行 pylint,您可以运行:

shopt -s globstar  # for Bash
pylint ./**/*.py

回答by Ryan Feeley

[UPDATED based on helpful additions in the comments]

[根据评论中的有用补充进行更新]

If you don't have an __init__.pyfile in the directory, and you don't want to for various reasons, my approach is

如果__init__.py目录中没有文件,并且出于各种原因又不想,我的做法是

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

If you already have a __init__.pyfile in that directory, it will be deleted.

如果您__init__.py在该目录中已经有一个文件,它将被删除

If you find yourself needing this functionality often, you should make a function that does this in a safer way that preserves a pre-existing __init__.pyfile. For example, you could put the following pylint_all_the_thingsfunction in your ~/.bashrcfile. (The last line exports the function so it can be called from any subshell.) If you don't want to edit .bashrc, you could put the function body in an executable shell script file.

如果您发现自己经常需要此功能,您应该创建一个以更安全的方式执行此操作的函数,以保留预先存在的__init__.py文件。例如,您可以将以下pylint_all_the_things函数放入您的~/.bashrc文件中。(最后一行导出函数,以便可以从任何子shell 调用它。)如果不想编辑.bashrc,可以将函数主体放在可执行的shell 脚本文件中。

This function defaults to running pylint in your current directory, but you can specify the directory to use as the 1st function argument.

此函数默认在当前目录中运行 pylint,但您可以指定要用作第一个函数参数的目录。

# Run pylint in a given directory, defaulting to the working directory
pylint_all_the_things() {
    local d=${1:-$(pwd)}

    # Abort if called with a non-directory argument.
    if [ ! -d "${d}" ]; then
        echo "Not a directory: ${d}"
        echo "If ${d} is a module or package name, call pylint directly"
        exit 1
    fi

    local module_marker="${d}/__init__.py"

    # Cleanup function to later remove __init__.py if it doesn't currently exist
    [[ ! -f ${module_marker} ]] && local not_a_module=1
    cleanup() {
        (( ${not_a_module:-0} == 1 )) && rm "${module_marker}"
    }
    trap cleanup EXIT

    # Create __init__.py if it doesn't exist
    touch "${module_marker}"
    pylint "${d}"
    cleanup
}
export -f pylint_all_the_things

The traputility is used to ensure the cleanup happens even if the call to pylintfails and you have set -eenabled, which causes the function to exit before reaching the cleanup line.

trap实用程序用于确保即使调用pylint失败并且您已set -e启用清理也会发生,这会导致函数在到达清理行之前退出。

If you want to call pylintrecursively on the current working directory and all subfolders, you could do something like

如果您想pylint在当前工作目录和所有子文件夹上递归调用,您可以执行以下操作

for dir in ./**/ ; do pylint_all_the_things "$dir"; done

Which will require globstar to be enabled in bash (shopt -s globstar).

这将需要在 bash ( shopt -s globstar) 中启用 globstar 。

回答by NN_

Did you try prospector (https://pypi.org/project/prospector/) or pylint_runner ( https://pypi.org/project/pylint_runner/)

您是否尝试过探矿者(https://pypi.org/project/prospector/)或 pylint_runner(https://pypi.org/project/pylint_runner/

回答by Acumenus

pytestwith pytest-pylintcan trivially run pylint on all Python files:

pytestwithpytest-pylint可以在所有 Python 文件上轻松运行 pylint:

In your setup.cfgfile in the root directory of your project, ensure you have at minimum:

setup.cfg项目根目录下的文件中,确保至少有:

[tool:pytest]
addopts = --pylint 

Next, run pyteston the command line.

接下来,pytest在命令行上运行。

回答by holzkohlengrill

There is already an issue for thisand hopefully gets fixed soon.

已经有这个问题,希望很快得到解决。

If you do not prefer to use xargsyou can just do a plain find-exec:

如果你不喜欢使用,xargs你可以做一个简单的 find-exec:

find . -type f -name "*.py" -exec pylint -j 0 --exit-zero {} \;



The problem I had with pylint Project-Diris that all the absolute imports were not working.

我遇到的问题pylint Project-Dir是所有绝对导入都不起作用。

回答by DenisTs

Im using the "pylint_runner" in order to run pylint on all files in the directory and the subdirectories. Python 3.7.4

我使用“pylint_runner”来对目录和子目录中的所有文件运行 pylint。蟒蛇 3.7.4

pylint_runner 0.54

pylint_runner 0.54

pylint 2.4.1

pylint 2.4.1

https://pypi.org/project/pylint_runner/

https://pypi.org/project/pylint_runner/

Here is the command to run it from the Docker container:

这是从 Docker 容器运行它的命令:

docker run -i --rm --name my_container \
  -v "$PWD":"$PWD" -w "$PWD" \
    python:3.7 \
      /bin/sh -c "pip3 install -r requirements.txt; pylint_runner -v"

requirements.txt- should exist in the "$PWD" directory and contain "pylint_runner" entry.

requirements.txt- 应该存在于“$PWD”目录中并包含“pylint_runner”条目。

回答by Santosh Pillai

And if you want to run your custom configuration file use below command

如果您想运行您的自定义配置文件,请使用以下命令

pylint --rcfile=.pylintrc <directory_name>

回答by JeremyDouglass

If your goal is to run pylint on all files in the current working directory and subfolders, here is one workaround. This script runs pylint on the current directory. If __init__.pydoes not exist, it creates it, runs pylint, then removes it.

如果您的目标是对当前工作目录和子文件夹中的所有文件运行 pylint,这是一种解决方法。此脚本在当前目录上运行 pylint。如果__init__.py不存在,它会创建它,运行 pylint,然后将其删除。

#! /bin/bash -
if [[ ! -e __init__.py ]]; then
    touch __init__.py
    pylint `pwd`
    rm __init__.py
else
    pylint `pwd`
fi