对于我这种习惯了 Java 这种编译型语言,在使用 Python 这种动态语言的时候,发现错误经常只能在执行的时候发现,总感觉有点不放心。

而且有一些错误由于隐藏的比较深,只有特定逻辑才会触发,往往导致需要花很多时间才能将语法错误慢慢排查出来。其实有一些错误是很明显的,假如能在写程序的时候发现这些错误,就能提高工作效率。

这时候 Python 静态语法检查工具就出现了。

pep8/pycodestyle   PEP(Python Enhancement Proposal)的缩写,翻译过来就是 Python增强建议书

相信大家多多少少都见过 PEP 8,那 PEP 8 到底是个啥?

其实 PEP 8 是一种 Python 代码规范指南,可以参阅官网:https://www.python.org/dev/peps/pep-0008/,其目的是为了保持代码的一致性、可读性。

检查自己代码是否符合 PEP 8 规范,一个简单的工具就是:pep8。

安装

  1. $ pip install pep8
 
  • 1

在使用时发现 pep8 给出了一个警告:

  1. $ pep8 gkcx.py
  2. /usr/local/lib/python3.5/dist-packages/pep8.py:2124: UserWarning:
  3. pep8 has been renamed to pycodestyle (GitHub issue #466)
  4. Use of the pep8 tool will be removed in a future release.
  5. Please install and use `pycodestyle` instead.
  6. $ pip install pycodestyle
  7. $ pycodestyle ...
  8. '\n\n'
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

意思是 pep8 已被 pycodestyle 替代!

使用

基本使用方法:$ pycodestyle [file name or directory name]

  1. $ pycodestyle gkcx.py
  2. gkcx.py:11:80: E501 line too long (135 > 79 characters)
  3. gkcx.py:14:1: E302 expected 2 blank lines, found 1
  4. gkcx.py:15:80: E501 line too long (94 > 79 characters)
  5. ...省略部分
  6. gkcx.py:67:1: E305 expected 2 blank lines after class or function definition, found 1
  7. gkcx.py:71:80: E501 line too long (100 > 79 characters)
  8. gkcx.py:82:25: E231 missing whitespace after ','
  9. gkcx.py:82:29: E231 missing whitespace after ','
  10. gkcx.py:84:1: W293 blank line contains whitespace
  11. gkcx.py:84:1: W391 blank line at end of file
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 参数 --statistics -qq :对结果进行汇总
  1. $ pycodestyle gkcx.py --statistics -qq
  2. 3 E203 whitespace before ':'
  3. 1 E225 missing whitespace around operator
  4. 16 E231 missing whitespace after ':'
  5. 3 E302 expected 2 blank lines, found 1
  6. 1 E305 expected 2 blank lines after class or function definition, found 1
  7. 6 E501 line too long (135 > 79 characters)
  8. 1 W291 trailing whitespace
  9. 1 W293 blank line contains whitespace
  10. 1 W391 blank line at end of file
 
  • 参数 --show-source:更详细的输出
  1. $ pycodestyle gkcx.py --show-source
  2. gkcx.py:14:1: E302 expected 2 blank lines, found 1
  3. def get_school_id(school_name):
  4. ^
  5. gkcx.py:15:80: E501 line too long (94 > 79 characters)
  6. Referer = "https://gkcx.eol.cn/soudaxue/queryschool.html?&keyWord1={}".format(school_name)
  7. ^
  8. gkcx.py:18:19: E203 whitespace before ':'
  9. "messtype" : "jsonp",
  10. ^
  11. gkcx.py:19:12: E231 missing whitespace after ':'
  12. "_":"1530074932531",
  13. ^
  14. ...省略部分
  • 参数 --ignore:忽略指定输出
  1. $ pycodestyle gkcx.py --ignore=E225,E501,E231
  2. gkcx.py:14:1: E302 expected 2 blank lines, found 1
  3. gkcx.py:18:19: E203 whitespace before ':'
  4. gkcx.py:20:19: E203 whitespace before ':'
  5. gkcx.py:21:19: E203 whitespace before ':'
  6. gkcx.py:32:1: E302 expected 2 blank lines, found 1
  7. gkcx.py:41:1: E302 expected 2 blank lines, found 1
  8. gkcx.py:55:15: W291 trailing whitespace
  9. gkcx.py:67:1: E305 expected 2 blank lines after class or function definition, found 1
  10. gkcx.py:84:1: W293 blank line contains whitespace
  11. gkcx.py:84:1: W391 blank line at end of file

错误码含义

  • E...:错误
  • W...:警告
  • 100 型:缩进问题
  • 200 型:空格问题
  • 300 型:空行问题
  • 400 型:导入问题
  • 500 型:行长度问题
  • 600 型:已弃用
  • 700 型:声明问题
  • 900 型:语法错误

Pyflakes

一个用于检查 Python 源文件错误的简单程序。

Pyflakes 分析程序并且检查各种错误。它通过解析源文件实现,无需导入它,因此在模块中使用是安全的,没有任何的副作用。

  • 不会检查代码风格
  • 由于它是单独检查各个文件,因此它也相当的快,当然检测范围也有一定的局限

安装

  1. pip install pyflakes
 
  • 1

使用

$ pyflakes [ file name or directory name]

  1. $ pyflakes gkcx.py
  2. gkcx.py:3: 'bs4.BeautifulSoup' imported but unused
  3. gkcx.py:6: 're' imported but unused

Pylint

PyLint 是 Python 源代码分析器,可以分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码,是一个可以用于验证多个文件的模块和包的工具。

缺省情况下,PyLint 启用许多规则。它具有高度可配置性,从代码内部处理程序控制它。另外,编写插件添加到自己的检查中是可能的。

安装

  1. $ pip install pylint
  2. $ pylint --version
  3. pylint 2.0.0
  4. astroid 2.0.1
  5. Python 3.5.2 (default, Nov 23 2017, 16:37:01)
  6. [GCC 5.4.0 20160609]
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

使用

基本使用:pylint [options] module_or_package

  1. $ pylint gkcx.py
  2. ************* Module gkcx
  3. gkcx.py:11:0: C0301: Line too long (135/100) (line-too-long)
  4. gkcx.py:25:47: C0326: Exactly one space required after comma
  5. response = requests.request("GET", data_url,headers=headers,params=params)
  6. ^ (bad-whitespace)
  7. gkcx.py:36:0: C0301: Line too long (113/100) (line-too-long)
  8. gkcx.py:1:0: C0111: Missing module docstring (missing-docstring)
  9. gkcx.py:10:0: C0103: Constant name "headers" doesn't conform to UPPER_CASE naming style (invalid-name)
  10. ...省略部分
  11. gkcx.py:14:18: W0621: Redefining name 'school_name' from outer scope (line 68) (redefined-outer-name)
  12. gkcx.py:32:0: C0111: Missing function docstring (missing-docstring)
  13. gkcx.py:33:4: W0622: Redefining built-in 'id' (redefined-builtin)
  14. gkcx.py:32:12: W0621: Redefining name 'school' from outer scope (line 72) (redefined-outer-name)
  15. ------------------------------------------------------------------
  16. Your code has been rated at 3.33/10 (previous run: 3.33/10, +0.00)
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

发现 Pylint 还会给代码整体打一个分数,我们就可以根据提示一步步调优,提高分数!10 分满分。

如果运行两次 Pylint,它会同时显示出当前和上次的运行结果,从而可以看出代码质量是否得到了改进。

错误代码含义

  • C:惯例,违反了编码风格标准
  • R:重构,代码非常糟糕
  • W:警告,某些 Python 特定的问题
  • E:错误,很可能是代码中的错误
  • F:致命错误,阻止 Pylint 进一步运行的错误

flake8

Flake8 是由 Python 官方发布的一款辅助检测 Python 代码是否规范的工具,相对于目前热度比较高的 Pylint 来说,Flake8 检查规则灵活,支持集成额外插件,扩展性强。Flake8 是对下面三个工具的封装:

  1. PyFlakes:静态检查 Python 代码逻辑错误的工具。
  2. Pep8: 静态检查 PEP8 编码风格的工具。
  3. NedBatchelder’s McCabe :静态分析 Python 代码复杂度的工具。

不光对以上三个工具的封装,Flake8还提供了扩展的开发接口。

官方文档:https://pypi.python.org/pypi/flake8/

安装

  1. $ pip install flake8
  2. $ flake8 --version
  3. 3.5.0 (mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.6.0) CPython 3.5.2 on Linux
 
  • 1
  • 2
  • 3
  • 4

使用

基本使用方法:flake8 [file name or directory name]

  1. $ flake8 gkcx.py
  2. gkcx.py:3:1: F401 'bs4.BeautifulSoup' imported but unused
  3. gkcx.py:6:1: F401 're' imported but unused
  4. gkcx.py:11:80: E501 line too long (135 > 79 characters)
  5. gkcx.py:14:1: E302 expected 2 blank lines, found 1
  6. ...省略部分
  7. gkcx.py:71:80: E501 line too long (100 > 79 characters)
  8. gkcx.py:82:25: E231 missing whitespace after ','
  9. gkcx.py:82:29: E231 missing whitespace after ','
  10. gkcx.py:84:1: W293 blank line contains whitespace
  11. gkcx.py:84:1: W391 blank line at end of file
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

PyFlakes 和 Pep8 的输出将合并起来一起返回。可以看出 flake8 不止检查代码错误,还会对代码规范不对的地方进行检查,比如:一行代码过长。

Flake8 提供一个扩展选项:–max-complexity,如果函数的 McCabe 复杂度比给定的值更高将发出一个告警。该功能对于发现代码过度复杂非常有用,根据 Thomas J. McCabe, Sr 研究,代码复杂度不宜超过 10,而 Flake8 官网建议值为 12。

  • McCabe 复杂度默认情况下是不会输出的,需要通过 --max-complexity 指定:
  1. $ flake8 gkcx.py --max-complexity=5
  2. gkcx.py:3:1: F401 'bs4.BeautifulSoup' imported but unused
  3. gkcx.py:6:1: F401 're' imported but unused
  4. ...省略部分
  5. gkcx.py:67:1: E305 expected 2 blank lines after class or function definition, found 1
  6. gkcx.py:67:1: C901 'If 67' is too complex (6)
  7. gkcx.py:71:80: E501 line too long (100 > 79 characters)
  8. gkcx.py:82:25: E231 missing whitespace after ','
  9. gkcx.py:82:29: E231 missing whitespace after ','
  10. gkcx.py:84:1: W391 blank line at end of file
  11. gkcx.py:84:1: W293 blank line contains whitespace
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 可以通过 --ignore 忽略指定输出:
  1. $ flake8 gkcx.py --ignore E501,E231,E203
  2. gkcx.py:3:1: F401 'bs4.BeautifulSoup' imported but unused
  3. gkcx.py:6:1: F401 're' imported but unused
  4. gkcx.py:14:1: E302 expected 2 blank lines, found 1
  5. gkcx.py:32:1: E302 expected 2 blank lines, found 1
  6. gkcx.py:38:22: E225 missing whitespace around operator
  7. gkcx.py:41:1: E302 expected 2 blank lines, found 1
  8. gkcx.py:55:15: W291 trailing whitespace
  9. gkcx.py:67:1: E305 expected 2 blank lines after class or function definition, found 1
  10. gkcx.py:84:1: W391 blank line at end of file
  11. gkcx.py:84:1: W293 blank line contains whitespace
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 通过 --select 参数设置只展示指定输出:
  1. $ flake8 gkcx.py --select F401
  2. gkcx.py:3:1: F401 'bs4.BeautifulSoup' imported but unused
  3. gkcx.py:6:1: F401 're' imported but unused
 
  • 1
  • 2
  • 3

错误码含义

Flake8 基础错误返回码一共有三类:

  • E***/W***:PEP8 中的 error 和 warning。
  • F***:通过 PyFlakes 检测出的 error,其实 PyFlakes 本身是不提供错误返回码的,flake8 对 pyflakes 返回的错误消息进行了分类。
  • C9**:通过 McCabe 检测出的代码复杂度。

总结

Python 静态代码检查工具不止这几个,大家可以挑选合适的进行使用。本人也没有进行深入地探究,有兴趣进行高阶使用的可以参照官网。

另外,各个工具应该都有对应的插件,比如 vim、vscode、eclipse、pycharm 上应该都能集成上述工具,大家可以网上找下适合自己 ide 的插件进行安装。

有些人估计看到那么多异常也会烦,但是毕竟是可以培养大家代码规范的,后续工作后,肯定也有相应的代码规范,建议大家养成习惯。


如果觉得有用,欢迎关注我的微信,一起学习,共同进步,不定期推出赠书活动~

python 代码检测工具的更多相关文章

  1. python代码检查工具pylint 让你的python更规范

    1.pylint是什么? Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准(Pylint 默认使用的代码风格是 PEP 8,具体信息,请参阅 ...

  2. Python代码统计工具

    目录 Python代码统计工具 声明 一. 问题提出 二. 代码实现 三. 效果验证 Python代码统计工具 标签: Python 代码统计 声明 本文将对<Python实现C代码统计工具(一 ...

  3. StyleCop(C#代码检测工具)

    StyleCop(C#代码检测工具)   一.StyleCop是微软的一个开源的静态代码分析工具,检查c#代码一致性和编码风格. 二.下载地址   http://stylecop.codeplex.c ...

  4. Python代码分析工具

    Python代码分析工具:PyChecker.Pylint - CSDN博客 https://blog.csdn.net/permike/article/details/51026156

  5. Jenkins整合SonarQube代码检测工具

    借鉴博客:https://blog.csdn.net/kefengwang/article/details/54377055 上面这博客写得挺详细的,挺不错.它这个博客没有提供下载的教程,这个博客提供 ...

  6. c++代码检测工具

    cppcheck是一款静态代码检查工具,可以检查如内存泄漏等代码错误,使用起来比较简单,即提供GUI界面操作,也可以与VS开发工具结合使用. 1.安装 一般会提供免安装版,安装完成后将cppcheck ...

  7. eslint 代码检测工具

    jshint 检测工具不够灵活下,道格拉斯(何许人也?json创造者,javascript重要任务,犀牛那本书就是他写的). 文档地址: 中文地址 English 安装 利用npm全局安装eslint ...

  8. 恶意代码检测工具 -- Mathematics Malware Detected Tools

    Mathematics Malware Detected Tools 重要:由于缺少测试数据,部分结论可能不正确.更多更准确的结论,还需要进行大量实验. 概述 mmdt(Mathematics Mal ...

  9. python代码检查工具(静态代码审查)

    python静态代码检查 我们知道python是一门脚本语言,不像C#/Java等编译型语言可以在编译阶段就报出代码错误,脚本语言往往需要在运行期执行到这段代码时才会抛出代码错误. 那么在实际商业项目 ...

随机推荐

  1. 有关于MVC SignalR的问题

    刚拜读 @Learning hard 的 [Asp.net 开发系列之SignalR篇]专题一:Asp.net SignalR快速入门 跟着博文一步步操作,这是新人的学习方式 然而笔者的开发环境和 @ ...

  2. 《Redis开发与运维》读书笔记

    一.初始Redis 1.Redis特性与优点 速度快.redis所有数据都存放于内存:是用C语言实现,更加贴近硬件:使用了单线程架构,避免了多线程竞争问题 基于键值对的数据结构,支持的数据结构丰富.它 ...

  3. Go实现基于WebSocket的弹幕服务

    拉模式和推模式 拉模式 1.数据更新频率低,则大多数请求是无效的 2.在线用户量多,则服务端的查询负载高 3.定时轮询拉取,实时性低 推模式 1.仅在数据更新时才需要推送 2.需要维护大量的在线长连接 ...

  4. 4 ;XHTML表格

    1.表格的基本格式 2.<table>标签下的常用属性 3.<table>标签下的边框设置 4.<tr><th><td>标签下的常用属性 5 ...

  5. javascript中数组的常用算法深入分析

    Array数组是Javascript构成的一个重要的部分,它可以用来存储字符串.对象.函数.Number,它是非常强大的.因此深入了解Array是前端必修的功课.本文将给大家详细介绍了javascri ...

  6. 从项目需求角度,使用纯CSS方案解决垂直居中

    CSS是HTML元素的剪刀手,它极度的丰富了web页面的修饰.在众多CSS常见的样式需求中,有一奇葩式的存在[垂直居中],因为不管是从逻辑实现方面还是从正常需求量来讲,这都没理由让这个需求在实践过程中 ...

  7. phpcms中content主要使用的详情列表关系

    从首页(index.html)中点开的内容网页叫单网页(page.html) 从列表(list.html)中点开的网页叫内容页(show.html) 从导航栏的栏目中下拉的列表栏目叫栏目列表页(cat ...

  8. Android 滑动定位+吸附悬停效果实现

    在前两篇文章中,分别介绍了tablayout+scrollview 和 tablayout+recyclerview 实现的滑动定位的功能,文章链接: Android 实现锚点定位 Android t ...

  9. Android Studio NDK JNI动态注册本地方法

    概述 可能大家觉得javah生成的函数名又臭又长,不太好看.这里可以提供另外一种方法来动态注册c++函数,让其根Java中的native方法关联起来. 实现 这里通过JNIEnv的Resisterna ...

  10. 章节二、2-String 引用数据类型-字符串类

    一.创建String(字符串对象)的两种方式 1.String str1 = "nihao"("nihao"值存储在常量值中) 2.String str2 = ...