[根据 cmets 中的有用补充更新]
如果你的目录下没有__init__.py的文件,又因为各种原因不想,我的做法是
touch __init__.py; pylint $(pwd); rm __init__.py
如果您在该目录中已经有一个__init__.py 文件,它将被删除。
如果您发现自己经常需要此功能,您应该创建一个以更安全的方式执行此操作的函数,以保留预先存在的__init__.py 文件。例如,您可以将以下 pylint_all_the_things 函数放入您的 ~/.bashrc 文件中。 (最后一行导出函数,因此可以从任何子 shell 调用它。)如果您不想编辑 .bashrc,可以将函数体放在可执行的 shell 脚本文件中。
此函数默认在当前目录中运行 pylint,但您可以指定要用作第一个函数参数的目录。
# 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
trap 实用程序用于确保即使对 pylint 的调用失败并且您启用了 set -e 也会进行清理,这会导致函数在到达清理行之前退出。
如果你想在当前工作目录和所有子文件夹上递归调用pylint,你可以这样做
for dir in ./**/ ; do pylint_all_the_things "$dir"; done
这需要在 bash (shopt -s globstar) 中启用 globstar。