史上最全!!!代码审查工具pylint
pylint最全!!!
代码审查工具pylint
一、安装pylint
1.1终端安装
#pylint的运行环境需要python版本大于2.7
pip install pylint
1.2Pycharm添加Pylint工具
1)Pycharm --> Preferences --> Tools --> External Tools --> +
二、pylint的基本使用
访问 PyLink 的官方文档,了解其功能和使用方法:
Messages overview - Pylint 4.0.0-dev0 documentation
2.1单文件执行
2.1.1Pycharm使用Pylint工具
2.1.1终端输入命令
# pylint <file_name>.py
pylint control_lib\api\global_lib.py
2.2 文件夹执行
2.2.1Pycharm使用Pylint工具
文件夹必须包含_init_.py文件,确保该包的目录中包含一个__init__.py
文件,这样 Python 才会将其识别为一个包。如果文件不存在,需要创建一个空的 文件,保证可以识别成功。
2.2.2终端输入命令
# pylint your_directory/
pylint control_lib
2.3 pylint其他相关用法
2.3.1 生成报告
# 生成txt形式,reports=y实现按类别划分的消息数量、模块依赖关系
pylint --output-format=txt --reports=y control_lib\api\global_lib.py > report.txt
# 第一步:先生成json形式
pylint --output-format=json --reports=y control_lib\api\global_lib.py > report.json# 第二步:编写json文件转html的工具类
import json
# 读取 JSON 文件
with open('../report.json', 'r') as f:
pylint_output = json.load(f)# 初始化 HTML 内容
html_content = '''
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pylint Report</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #f2f2f2; }
tr:nth-child(even) { background-color: #f9f9f9; }
</style>
</head>
<body>
<h1>Pylint Report</h1>
<table>
<thead>
<tr>
<th>Type</th>
<th>Module</th>
<th>Line</th>
<th>Column</th>
<th>Path</th>
<th>Symbol</th>
<th>Message</th>
<th>Message ID</th>
</tr>
</thead>
<tbody>
'''# 解析 JSON 并生成 HTML 表格行
for message in pylint_output:
html_content += f'''
<tr>
<td>{message['type']}</td>
<td>{message['module']}</td>
<td>{message['line']}</td>
<td>{message['column']}</td>
<td>{message['path']}</td>
<td>{message['symbol']}</td>
<td>{message['message']}</td>
<td>{message['message-id']}</td>
</tr>
'''# 结束 HTML 表格和文档
html_content += '''
</tbody>
</table>
</body>
</html># 格式化时间戳并插入到文件名中
filename = '../report/pylint/pylint_report_{}.html'.format(time.strftime("%Y%m%d%H%M%S", time.localtime()))# 使用格式化后的文件名打开文件并写入内容
with open(filename, 'w', encoding='utf-8') as f:
f.write(html_content)
2.3.2 忽略某些警告:
pylint --output-format=json --reports=y --disable=W control_lib/api/global_lib.py > report.json
2.3.3 启用特定检查项
# 不支持直接对消息类型的 (如:E)的检查--enable=E,--enable 参数实际上需要一个具体的消息ID或一组通过逗号分隔的消息ID
#使用 --disable=all 参数来禁用所有默认检查,然后仅使用 --enable=C0301,W0311 来启用你感兴趣的特定检查。
pylint --disable=all --enable=C0301,W0311 control_lib/api/global_lib.py
2.3.4 只显示错误
pylint --output-format=json --reports=y --errors-only control_lib/api/global_lib.py > report.json
2.3.5 配置文件
#第一步:生成配置文件
pylint --generate-rcfile > pylint.conf
#第二步:配置生成的文件
# (如下图)
#第三步:检查配置文件是否被正确加载
pylint --rcfile=pylint.cfg --help
#第四步:按照配置文件的要求,运行python脚本
pylint --rcfile=pylint.cfg --output-format=json control_lib/api/global_lib.py
2.3.6 报告参数
# 包含报告部分
pylint --output-format=txt --reports=y control_lib\api\global_lib.py > report.txt
# 只想查看源代码分析部分,而不希望在输出中包含报告部分,简写-ry
pylint --output-format=txt --reports=n control_lib\api\global_lib.py > report.txt
2.4 查看检查结果
(C) 惯例。违反了编码风格标准
(R) 重构。写得非常糟糕的代码。
(W) 警告。某些 Python 特定的问题。
(E) 错误。很可能是代码中的错误。
(F) 致命错误。阻止 Pylint 进一步运行的错误。
如需对某告警类型获取帮助信息,可以使用"pylint --help-msg "
pylint --help-msg=C0114
更多可看官方文档或终端输入命令:
Messages - Pylint 4.0.0-dev0 documentation
更多推荐
所有评论(0)