129

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?

18 Answers 18

110

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

pylint ./server

Note that this requires the __init__.py file to exist in the target directory.

10
  • 52
    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.
    – James
    Commented Mar 31, 2018 at 17:10
  • 19
    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. Commented Aug 3, 2018 at 14:16
  • 1
    @rahulrachh can you say more about what you mean by score? You mean pass / fail score, or do you mean something more like codecoverage score?
    – duhaime
    Commented Jan 28, 2020 at 10:58
  • 1
    Hmm, you'd need to call pylint on each file individually in that case. Just create a loop and call pylint in the loop.
    – duhaime
    Commented Jan 28, 2020 at 14:57
  • 4
    this is a great answer, thank you. Or: pylint ${PWD} if you want to pylint modules here and underneath (since pylint . is not a thing). Commented Dec 24, 2020 at 20:57
80

My one cent

find . -type f -name "*.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

  3. pylint ./module

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

7
  • 65
    this is terrible. there is no pylint-centric solution?
    – dopatraman
    Commented Aug 1, 2017 at 22:39
  • 6
    Not portable if you don't have find. See other answers. Commented Oct 16, 2018 at 11:54
  • 4
    Not that this is my default solution, but if you have to use this solution then you should makes sure to explicitly tell it to ignore your vitural environment: find . -type f -not -path "./venv/*" -name "*.py" | xargs pylint
    – jimjim
    Commented Mar 26, 2020 at 13:59
  • Any ides how to run the same on windows? Commented Jun 20, 2020 at 16:59
  • 2
    Tiny adjustment from @jimjim: if your virtual envs are hidden with a dot: find . -type f -not -path "./.venv/*" -name "*.py" | xargs pylint
    – gkedge
    Commented Nov 5, 2020 at 2:36
72

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, and also Windows. However it won't lint files that are very new and haven't been git added to the repos yet.

My thanks to national treasure Julia Evans for the git ls-files trick -- here's her original use case, automating workflows with "entr": https://jvns.ca/blog/2020/06/28/entr/

6
  • 2
    I like this answer because it also seems to work correctly on Windows under PowerShell, MacOS, Linux, etc...
    – jgstew
    Commented Mar 12, 2021 at 22:28
  • No need for git add
    – Noam-N
    Commented Nov 3, 2021 at 17:54
  • in my observation the regular find command is faster than the git ls-files command.
    – krema
    Commented Jan 4, 2022 at 10:40
  • to add also untracked files but ignoring the files in the standard Git exlusion (.gitignore) we can use following command pylint $( git ls-files --exclude-standard --other -o '*.py'; git ls-files --exclude -o '*.py'; )
    – krema
    Commented Jan 4, 2022 at 11:38
  • 1
    Assumes a version control system managing the folder and that the system is git, though. Except for this, I like this answer. Commented Nov 1, 2022 at 9:34
35

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

shopt -s globstar  # for Bash
pylint ./**/*.py
3
  • 7
    This doesn't appear to work. Which version of pylint and which OS/Shell did you run this from? Commented Feb 22, 2019 at 16:16
  • 5
    Or use both in one line: pylint *.py **/*.py - runs for all py files in current dir and all subdirs
    – andrii
    Commented Feb 24, 2019 at 5:16
  • 1
    This does not run after installing pylint out of the box Commented Sep 15, 2019 at 4:33
11

The pylint docs makes mention of a --recursive flag, which tells pylint to find all modules and packages by itself without needing the user to manually specify all of them.

Therefore, all you need to do is:

pylint --recursive=y .

Looks like the flag was introduced in pylint v2.13.0. but it seems that from v2.15.0 onwards, you won't need the --recursive flag, and that will be pylint's default behaviour.

From 2.15.0, you won't need the flag to detect namespace packages.

2
  • (2.15.10 with --recursive=y): Still only scans the __init__.py in my directory, not the other.py file next to it. Commented Jan 24, 2023 at 12:19
  • It must be accepted answer since 2022. Thanks. Commented Jan 11 at 5:55
8

I used in the root directory:

pylint *
3
  • 1
    --ignore <directory> if you should want to ignore let's say your virtualenv
    – swateek
    Commented Jun 22, 2021 at 15:33
  • 2
    Will crash if pylint tries to lint random non-Python files. Commented Jan 24, 2023 at 12:16
  • why is this not the accepted answer? It works exactly as expected and no external dependencies Commented Jul 1, 2023 at 5:16
7

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

5
  • 1
    touch __init__.py; pylint `pwd`; rm __init__.py Commented Jun 4, 2018 at 8:59
  • 1
    Better do trap 'rm -f __init__.py' EXIT to make sure it's removed even if pylint aborts.
    – yugr
    Commented Aug 25, 2018 at 11:23
  • An important clarification - __init__.py needs to be added in all subfolders with .py files.
    – yugr
    Commented Aug 25, 2018 at 17:35
  • 1
    remember not to delete __init__.py if you've had it in the first place. Commented Oct 16, 2018 at 11:39
  • I did touch __init__.py; pylint . but I get no output. Any idea why?
    – krubo
    Commented Jun 24, 2019 at 16:30
5

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 0.54

pylint 2.4.1

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

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

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.

4

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

pylint --rcfile=.pylintrc <directory_name>
3

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.

2

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

2

Pylint v3.x seems to have a new --recursive flag

pylint --recursive=y .

This option makes pylint attempt to discover all modules (files ending with .py extension) and all explicit packages (all directories containing a init.py file).

https://pylint.pycqa.org/en/latest/user_guide/usage/run.html#on-module-packages-or-directories

1
  • Amazing, mate. Thanks a lot. Effing future is here! Commented Apr 5 at 20:37
1

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__.py does not exist, it creates it, runs pylint, then removes it.

#! /bin/bash -
if [[ ! -e __init__.py ]]; then
    touch __init__.py
    pylint `pwd`
    rm __init__.py
else
    pylint `pwd`
fi
3
  • Better do trap 'rm -f __init__.py' EXIT to make sure it's removed even if pylint aborts. This would also allow to get rid of else branch.
    – yugr
    Commented Aug 25, 2018 at 11:22
  • 1
    An important clarification - __init__.py needs to be added in all subfolders with .py files. I ended up doing something like files=; for d in $(find -name \*.py | xargs dirname | sort -u); do if ! test -f $d/__init__.py; then touch $d/__init__.py; files="$files $PWD/$d/__init__.py"; fi; done; trap "rm -f $files" EXIT
    – yugr
    Commented Aug 25, 2018 at 17:36
  • Good point -- you should submit your approach as a separate answer with a full explanation. Commented Aug 25, 2018 at 21:07
1

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

using ./server or something similar only works if there is a __init__.py in all subdirs, it will not match all python files in all subdirs. using find and xargs is an option, but this also works:

pylint **py

0
  1. touch __init__.py in the current directory
  2. touch __init__.py in every subdirectory that you want pylint to look at
  3. pylint $(pwd) (or equivalently pylint /absolute/path/to/current/directory)
0

What also works is the following command:

pylint ./**/*.py

or if you have your application code in a specific folder:

pylint ./app/**/*.py

0

If your pylint version is lower than 2.15, try using the --recursive=y option.
example> In your project root, pylint --recursive=y .

It's okay if there's no __init__.py. It should be helpful.
(To check the version of pylint, you can use pylint --version.)

For more details, please refer to the link and explanation below.

https://pylint.readthedocs.io/en/stable/user_guide/usage/run.html

By default the pylint command only accepts a list of python modules and packages. On versions below 2.15, specifying a directory that is not an explicit package (with init.py) results in an error:

pylint mydir ************* Module mydir mydir/init.py:1:0: F0010: error while code parsing: Unable to load file mydir/init.py: [Errno 2] No such file or directory: 'mydir/init.py' (parse-error) Thus, on versions before 2.15, or when dealing with certain edge cases that have not yet been solved, using the --recursive=y option allows for linting a namespace package:

pylint --recursive=y mydir mymodule mypackage This option makes pylint attempt to discover all modules (files ending with .py extension) and all explicit packages (all directories containing a init.py file).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.