Flake8 是由Python官方发布的一款辅助检测Python代码是否规范的工具,相对于目前热度比较高的Pylint来说,Flake8检查规则灵活,支持集成额外插件,扩展性强。
Flake8 是“将 PEP 8、Pyflakes(类似 Pylint)、McCabe(代码复杂性检查器)和第三方插件整合到一起,以检查 Python 代码风格和质量的一个 Python 工具”。与pyLint的功能一样,用于对代码的静态检测。
执行 pip install flake8 安装 flake8 ,然后执行 flake8 [options] path/to/dir 或者 flake8 [options] path/to/module.py 可以查看报出的错误和警告。
和 Pylint 类似,Flake8 允许通过配置文件来自定义检查的内容。它有非常清晰的文档,包括一些有用的提交钩子,可以将自动检查代码纳入到开发工作流程之中。
Flake8 也可以集成到一些流行的编辑器和 IDE 当中,但在文档中并没有详细说明。要将 Flake8 集成到喜欢的编辑器或 IDE 中,可以搜索插件(例如 Sublime Text 的 Flake8 插件)。
(BlogChecker) D:\pyve\BlogChecker>flake8 LinkCheck.py
LinkCheck.py:8:1: F401 'datetime as dt' imported but unused
LinkCheck.py:50:36: W291 trailing whitespace
LinkCheck.py:52:28: W291 trailing whitespace
LinkCheck.py:77:59: W291 trailing whitespace
LinkCheck.py:97:1: W293 blank line contains whitespace
LinkCheck.py:111:67: E712 comparison to False should be 'if cond is False:' or '
if not cond:'
LinkCheck.py:117:18: E712 comparison to False should be 'if cond is False:' or '
if not cond:'
LinkCheck.py:117:27: W291 trailing whitespace
LinkCheck.py:121:1: W293 blank line contains whitespace
LinkCheck.py:138:52: W291 trailing whitespace
LinkCheck.py:139:46: E231 missing whitespace after ':'
LinkCheck.py:154:9: E265 block comment should start with '# '
LinkCheck.py:155:9: E722 do not use bare except'
LinkCheck.py:162:53: W291 trailing whitespace
LinkCheck.py:169:44: W291 trailing whitespace
LinkCheck.py:174:60: W291 trailing whitespace
LinkCheck.py:175:77: W291 trailing whitespace
LinkCheck.py:176:76: W291 trailing whitespace
LinkCheck.py:193:74: W291 trailing whitespace
LinkCheck.py:209:13: W291 trailing whitespace
LinkCheck.py:210:51: W291 trailing whitespace
LinkCheck.py:211:25: E127 continuation line over-indented for visual indent
LinkCheck.py:211:47: W291 trailing whitespace
LinkCheck.py:212:25: E127 continuation line over-indented for visual indent
LinkCheck.py:212:46: E231 missing whitespace after ':'
LinkCheck.py:213:9: E722 do not use bare except'
LinkCheck.py:218:1: W293 blank line contains whitespace
LinkCheck.py:225:59: W291 trailing whitespace
LinkCheck.py:233:54: W291 trailing whitespace
LinkCheck.py:234:33: E128 continuation line under-indented for visual indent
LinkCheck.py:234:56: W291 trailing whitespace
LinkCheck.py:235:33: E128 continuation line under-indented for visual indent
LinkCheck.py:238:51: W291 trailing whitespace
LinkCheck.py:239:33: E128 continuation line under-indented for visual indent
LinkCheck.py:239:56: W291 trailing whitespace
LinkCheck.py:240:33: E128 continuation line under-indented for visual indent
LinkCheck.py:244:52: W291 trailing whitespace
LinkCheck.py:245:33: E128 continuation line under-indented for visual indent
LinkCheck.py:245:56: W291 trailing whitespace
LinkCheck.py:246:33: E128 continuation line under-indented for visual indent
LinkCheck.py:248:17: E265 block comment should start with '# '
LinkCheck.py:249:61: W291 trailing whitespace
LinkCheck.py:253:55: W291 trailing whitespace
LinkCheck.py:254:33: E128 continuation line under-indented for visual indent
LinkCheck.py:254:56: W291 trailing whitespace
LinkCheck.py:255:33: E128 continuation line under-indented for visual indent
LinkCheck.py:257:13: E265 block comment should start with '# '
LinkCheck.py:266:1: W293 blank line contains whitespace
LinkCheck.py:274:1: W293 blank line contains whitespace
LinkCheck.py:286:1: W293 blank line contains whitespace
LinkCheck.py:290:1: W293 blank line contains whitespace
LinkCheck.py:306:1: W293 blank line contains whitespace
LinkCheck.py:310:1: W293 blank line contains whitespace
LinkCheck.py:322:80: E501 line too long (144 > 79 characters)
LinkCheck.py:323:28: W291 trailing whitespace
LinkCheck.py:324:1: W293 blank line contains whitespace
LinkCheck.py:325:80: E501 line too long (87 > 79 characters)
LinkCheck.py:327:8: W291 trailing whitespace
LinkCheck.py:334:34: W291 trailing whitespace
LinkCheck.py:336:1: W293 blank line contains whitespace
LinkCheck.py:352:9: E265 block comment should start with '# '
LinkCheck.py:353:9: E265 block comment should start with '# '
LinkCheck.py:357:16: W292 no newline at end of file
不管是pyLint还是Flake8,我觉得都需要做适当的配置,才能适用特定的项目。
pyLink和Flake8检查出来的结果会稍有不同,pyLink默认太在意函数和变量以及类的命名,给我的代码爆出一大堆warning。但是,通过阅读PEP-0008,我的理解是,有好几种不同的命名规则供选择适用,并没有强制哪一种必须用在什么地方。这一点Flake8做的比较好,默认情况下,不会对我的命名“提出异议”。
建议使用这个flake8命令,忽略掉两个蛋疼的warning:flake8 --ignore W291,W293 yourPython.py
Flake8的说明文档:https://flake8.readthedocs.io
除了单独检查一个py文件,Flake8还可以检查一个文件夹里面所有的文件:
flake8 path/to/code/to/check.py
# or
flake8 path/to/code/