How to run Pylint for all Python files in a directory

I have

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

and

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

If I understood correctly, the first command will launch pylint for each of the Python files, the second will launch pylint once for all files. I expected both teams to return the same result, but they will return different results. I think this diff is somehow related to import messages and F (fail) error messages that occur when the import fails and is not printed with the pylint -E command.

Someone already experienced this and could explain why diff happens, and what is the best way to start pylint?

+36
python pylint
source share
10 answers

My one cent

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

How it works?

find finds all files ending in py and transferred to xargs , xargs runs the pylint command for each file.

NOTE. You can also specify any argument for the pylint .

EDIT:

According to the document, we can use

  1. pylint mymodule.py

  2. pylint / mymodule.py reference

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

+34
source share

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

 pylint server 
+29
source share

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 
+14
source share

[UPDATED based on useful additions in the comments]

If you do not have the __init__.py file in the directory, and you do not want for various reasons, my approach

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

If you already have the __init__.py file in this directory, it will be deleted .

If you often need these functions, you must create a function that does this, while preserving any previously existing __init__.py file. For example, you can put the following in your ~/.bashrc , where pylint_all_the_things exported pylint_all_the_things that it can be called from any subshell. Alternatively, you can put the function body in an executable script.

By default, this function starts pylint in the current directory, but you can specify the directory that will be used as the first argument to the function.

 # 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 does not 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 does not exist touch "${module_marker}" pylint "${d}" cleanup } export -f pylint_all_the_things 

The trap utility is used to ensure that cleanup happens even if the call to pylint fails and you have set -e turned on, which causes the function to exit before the cleanup line is reached.

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

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

Which will require that globstar be included in bash ( shopt -s globstar ).

+4
source share

Have you tried psospector ( https://pypi.org/project/prospector/ ) or pylint_runner ( https://pypi.org/project/pylint_runner/ )

+2
source share

There is already a problem for this , and we hope that it will be fixed soon.

If you do not prefer to use xargs , you can simply do find-exec:

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


The problem that I encountered in pylint Project-Dir is that all absolute imports did not work.

+2
source share

I use pylint_runner to run pylint for all files in a directory and subdirectories. Python 3.7.4

pylint_runner 0.54

Pilint 2.4.1

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

Here is the command to run 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 - must exist in the $ PWD directory and contain the entry pylint_runner.

+2
source share

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

 #! /bin/bash - if [[ ! -e __init__.py ]]; then touch __init__.py pylint 'pwd' rm __init__.py else pylint 'pwd' fi 
+1
source share

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

 pylint --rcfile=.pylintrc <directory_name> 
+1
source share
  1. touch __init__.py in the current directory
  2. touch __init__.py in every subdirectory you want pylint to look at
  3. pylint $(pwd) (or equivalently pylint/absolute/path/to/current/directory )
0
source share

All Articles