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

后端 未结 14 2162
眼角桃花
眼角桃花 2021-01-30 19:45

I have

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

and

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

If

相关标签:
14条回答
  • 2021-01-30 20:36

    [UPDATED based on helpful additions in the comments]

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

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

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

    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__.py file. For example, you could put the following pylint_all_the_things function in your ~/.bashrc file. (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.

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

    # 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 trap utility is used to ensure the cleanup happens even if the call to pylint fails and you have set -e enabled, which causes the function to exit before reaching the cleanup line.

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

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

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

    0 讨论(0)
  • 2021-01-30 20:36

    To run Pylint in all subdirectories,

    pylint $(find [a-z]* -type d)
    

    This solution is simpler and more direct than others. It works with no setup, including on macOS which doesn't have Bash 4.

    The reason for the [a-z]* is because most projects have Git or other magic subdirectories, which would pollute the results. If you have subdirectories starting with Capital Letters, use this variant:

    pylint $(find [a-zA-Z]* -type d)
    
    0 讨论(0)
  • 2021-01-30 20:36

    I used in the root directory:

    pylint *
    
    0 讨论(0)
  • 2021-01-30 20:42

    pytest with pytest-pylint can trivially run pylint on all Python files:

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

    [tool:pytest]
    addopts = --pylint 
    

    Next, run pytest on the command line.

    0 讨论(0)
  • 2021-01-30 20:42

    To run Pylint on all code in Git version control,

    pylint $(git ls-files '*.py')
    

    This is very fast, as Git already knows the names of all of your files. It also works on macOS which lacks Bash 4. However it won't lint files that are very new and haven't been git added to the repos yet.

    0 讨论(0)
  • 2021-01-30 20:42

    There is already an issue for this and hopefully gets fixed soon.

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

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


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

    0 讨论(0)
提交回复
热议问题