How to run Pylint for all Python files in a directory

how to run pylint on entire directory
run pylint on directory
run pylint on all files in directory
pylint ignore directory
pylint command not found
pylint ignore multiple directories
pylint output to file
run pylint on current directory

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 not output by pylint -E.

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

My one cent

find . -iname "*.py" | xargs pylint 

How does it work?

find finds all files ends with py and pass to xargs, xargs runs pylint command on each file.

NOTE: You can give any argument to pylint command as well.

EDIT:

According to doc we can use

  1. pylint mymodule.py

  2. pylint directory/mymodule.py

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

Running Pylint, will work if directory is a python package (i.e. has an __init__.py file or it is an implicit namespace To silently run Pylint on a module_name.py module, and get its standard output and error: First of all, we have two basic (but useful) options. sudo apt-get install pylint To Run Pylint. Change the directory to where the file is located on command prompt Get Full Report. Run the command pylint with the file name as shown below: pylint fileName.py Get Errors & Warnings. To return all the errors within the file, run pylint -rn and the file name pylint -rn fileName.py To Get Errors

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

pylint server

pylint_runner · PyPI, Run pylint recursively on all py files in current and sub directories. A simple pylint application that scans the current directory and any sub-directories  If the current working directory is in a Python module, Pylint searches up the hierarchy of Python modules until it finds a pylintrc file. This allows you to specify coding standards on a module-by-module basis. Of course, a directory is judged to be a Python module if it contains an __init__.py file.

For all python files in a directory you can run:

pylint *.py
For all python files in a directory and its subdirectories you can run:
pylint **/*.py

How to run Pylint, Pylint. Change the directory to where the file is located on command prompt To return all the errors within the file, run pylint -rn and the file name pylint --​rcfile ..mantid\tools\Pylint\pylint.cfg fileName.py # note: provide the full path of the​  Just to add to @JayRizzo 's reply, if you would like to run pylint on python files only, use $ pylint /path/to/folder/*.py. If you do not use that, it will run on all the files in the directory (e.g: README.md). You would not want to run pylint on non-python files. 👍

[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 while preserving any pre-existing __init__.py file. For example, you could put the following in your ~/.bashrc file, where pylint_all_the_things is exported so it can be called from any subshell. Alternatively you could put the function body in an executable script.

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

lint all files in a directory · Issue #352 · PyCQA/pylint · GitHub, Nevertheless, Pylint shouldn't emit every time when it finds a folder without an init file, You would not want to run pylint on non-python files. Enable linters. To enable linters other than the default PyLint, open the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) and select the Python: Select Linter command. This command adds "python.linting.<linter>Enabled": true to your settings, where <linter> is the name of the chosen linter.

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

Module that runs pylint on all python scripts found in a directory tree , Module that runs pylint on all python scripts found in a directory tree.. - pylint-​recursive.py. apply pylint to the file specified if it is a *.py file. ''' global TOTAL  According to documentation here, we can use the following command to generate a pylint rc file with all its options present: pylint --generate-rcfile > ~/.pylintrc. The above command will create the file .pylintrc under your home directory. Then you can tweak the rc file to fit your needs.

Frequently Asked Questions, The supported running environment since Pylint 2. "pylint directory/mymodule.​py" will work if "directory" is a python package (i.e. has an __init__.py You can always generate a sample pylintrc file with --generate-rcfile Every option present​  You could therefore disable this message for all files under the test folder by running pylint on those files from within the project folder: pylint -d duplicate-code test/ I was able to verify that I can cut out specific messages from all files in the directory, although I was not getting duplicate code errors and so could not check for that

Editor and IDE integration, Visual Studio, see the Python > Run PyLint command on a project's context menu​. (local-file (file-relative-name temp-file (file-name-directory buffer-file-name)))) (list Then all is well, and most likely Pylint will have expressed some opinions  # before $ python -m mccabe foo/bar.py -m 10 4:0: 'complex_fun' 12 # after $ ./ci/lint.sh $ bad.py:4:0 complex_fun 12 Running pylint. I've already written a post about pylint. In a nutshell, you should carefully edit your .pylintrc file and make sure to collect your Python packages correctly. Putting it all together. Just a simple bash script:

Linting Python in Visual Studio Code, By default, linting for Python is enabled in Visual Studio Code using Pylint, and you In that case, either run VS Code elevated, or manually run the Python You can add any of the settings to your user settings.json file (opened with Exclude file and folder patterns, ignorePatterns, [".vscode/*.py", "**/site-packages/​**/*.py"]  If the current working directory is in a Python module, Pylint searches up the hierarchy of Python modules until it finds a pylintrc file. This allows you to specify coding standards on a module-by-module basis. Of course, a directory is judged to be a Python module if it contains an init.py file. The file named by environment variable PYLINTRC

Comments
  • this is terrible. there is no pylint-centric solution?
  • Not portable if you don't have find. See other answers.
  • Not obvious at all. Should be able to do pylint . but that doesn't work. But this is the pylint answer without resorting to external binaries.
  • To be clear, if the directory does not contain __init__.py then you cannot run pylint from within a directory server that you wish to run it on. Both pylint . and pylint ../server fail. Instead, you must cd ..; pylint server. An alternative is to create a temporary __init__.py as in Ryan Feeley's answer.
  • This doesn't appear to work. Which version of pylint and which OS/Shell did you run this from?
  • Or use both in one line: pylint *.py **/*.py - runs for all py files in current dir and all subdirs
  • Ah, running on Bash 4, with globstar on. So you might need to turn it on: shopt -s globstar; pylint **/*.py. Or do what @andrii suggested.
  • This does not run after installing pylint out of the box
  • touch __init__.py; pylint `pwd`; rm __init__.py
  • Better do trap 'rm -f __init__.py' EXIT to make sure it's removed even if pylint aborts.
  • An important clarification - __init__.py needs to be added in all subfolders with .py files.