问题
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?
回答1:
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
pylint mymodule.py
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.
回答2:
Just pass the directory name to the pylint command. To lint all files in ./server:
pylint server
回答3:
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
回答4:
[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).
回答5:
Did you try psospector (https://pypi.org/project/prospector/) or pylint_runner ( https://pypi.org/project/pylint_runner/ )
回答6:
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.
回答7:
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.
回答8:
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
回答9:
And if you want to run your custom configuration file use below command
pylint --rcfile=.pylintrc <directory_name>
回答10:
touch __init__.pyin the current directorytouch __init__.pyin every subdirectory that you want pylint to look atpylint $(pwd)(or equivalentlypylint /absolute/path/to/current/directory)
来源:https://stackoverflow.com/questions/36873096/how-to-run-pylint-for-all-python-files-in-a-directory