Python - 使用Pylint检查分析代码
1-简介
- 检查语法错误,是否遵守编码风格标准、潜在的问题等;
- 支持自定义配置:例如显示或隐藏特定的警告,并且可以通过编写插件来添加功能;
- 使用Pylint检查文件时,需要直接将模块或包名作为参数;
- 可以在命令行以脚本方式运行(pylint),也可作为模块(pylint.lint)导入,建议作为命令行工具使用;
2-帮助信息
- 在命令行下运行“pylint -h”或“pylint --help”获取帮助信息;
--generate-rcfile 生成一个配置文件示例; 可以使用重定向把这个配置文件保存下来用做以后使用; 也可以在前面加上其它选项,使这些选项的值被包含在这个产生的配置文件里; 如:“pylint --persistent=n --generate-rcfile > pylint.conf”,查看 pylint.conf,可以看到 persistent=no,而不再是其默认值 yes;--rcfile=<file> 指定一个配置文件; 把使用的配置放在配置文件中,这样不仅规范了自己代码,也可以方便地和别人共享这些规范;-i <y_or_n>, --include-ids=<y_or_n> 在输出中包含 message 的 id, 然后通过“pylint --help-msg=<msg-id>”来查看这个错误的详细信息,这样可以具体地定位错误。-r <y_or_n>, --reports=<y_or_n> 表示 Pylint 的输出中是否包含报告部分;--files-output=<y_or_n> 将每个 module /package 的 message 输出到一个以 pylint_module/package. [txt|html] 命名的文件中; 如果有 report 的话,输出到名为 pylint_global.[txt|html] 的文件中。默认是输出到屏幕上不输出到文件里;-f <format>, --output-format=<format> 设置输出格式; 可以选择的格式有 text, parseable, colorized, msvs (visual studio) 和 html, 默认的输出格式是 text;--disable-msg=<msg ids> 禁止指定 id 的 message; 例如:输出中包含了 W0402 这个 warning 的 message, 如果不希望它在输出中出现,可以使用“--disable-msg= W0402”;
3-安装
$ pip3 show pylint Name: pylint Version: 2.1.1 Summary: python code static checker Home-page: https://github.com/PyCQA/pylint Author: Python Code Quality Authority Author-email: code-quality@python.org License: GPL Location: c:\python36\lib\site-packages Requires: colorama, isort, astroid, mccabe Required-by:$ py --version Python 3.6.0$ py -m pylint --version __main__.py 2.1.1 astroid 2.0.4 Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)]
生成默认配置文件:将在当前目录生成pylint.conf文件,包含pylint的默认配置项;
$ py -m pylint --persistent=n --generate-rcfile > pylint.conf$ ll -h pylint.conf -rw-r--r-- 1 guowli 1049089 18K Nov 12 10:23 pylint.conf
4-检查单个文件(模块)
$ cat test.py import timedef shwotime():print(time.asctime())shwotime()$ py -m pylint --rcfile=pylint.conf test.py ************* Module test test.py:1:0: C0111: Missing module docstring (missing-docstring) test.py:4:0: C0111: Missing function docstring (missing-docstring)----------------------------------- Your code has been rated at 5.00/10$ py -m pylint -ry --rcfile=pylint.conf test.py ************* Module test test.py:1:0: C0111: Missing module docstring (missing-docstring) test.py:4:0: C0111: Missing function docstring (missing-docstring)Report ====== 4 statements analysed.Statistics by type ------------------+---------+-------+-----------+-----------+------------+---------+ |type |number |old number |difference |%documented |%badname | +=========+=======+===========+===========+============+=========+ |module |1 |NC |NC |0.00 |0.00 | +---------+-------+-----------+-----------+------------+---------+ |class |0 |NC |NC |0 |0 | +---------+-------+-----------+-----------+------------+---------+ |method |0 |NC |NC |0 |0 | +---------+-------+-----------+-----------+------------+---------+ |function |1 |NC |NC |0.00 |0.00 | +---------+-------+-----------+-----------+------------+---------+Raw metrics -----------+----------+-------+------+---------+-----------+ |type |number |% |previous |difference | +==========+=======+======+=========+===========+ |code |5 |50.00 |NC |NC | +----------+-------+------+---------+-----------+ |docstring |0 |0.00 |NC |NC | +----------+-------+------+---------+-----------+ |comment |0 |0.00 |NC |NC | +----------+-------+------+---------+-----------+ |empty |5 |50.00 |NC |NC | +----------+-------+------+---------+-----------+Duplication -----------+-------------------------+------+---------+-----------+ | |now |previous |difference | +=========================+======+=========+===========+ |nb duplicated lines |0 |NC |NC | +-------------------------+------+---------+-----------+ |percent duplicated lines |0.000 |NC |NC | +-------------------------+------+---------+-----------+Messages by category --------------------+-----------+-------+---------+-----------+ |type |number |previous |difference | +===========+=======+=========+===========+ |convention |2 |NC |NC | +-----------+-------+---------+-----------+ |refactor |0 |NC |NC | +-----------+-------+---------+-----------+ |warning |0 |NC |NC | +-----------+-------+---------+-----------+ |error |0 |NC |NC | +-----------+-------+---------+-----------+Messages --------+------------------+------------+ |message id |occurrences | +==================+============+ |missing-docstring |2 | +------------------+------------+----------------------------------- Your code has been rated at 5.00/10
- Pylint结果的级别:error,warning,refactor,convention;
- 可以根据首字母确定相应的级别,例如,C表示convention(规范)、W表示warning(告警);
- 级别之后的数字表示告警所在文件中的行号和列号;
- 参数 “-ry”开启报告,“-rn”关闭报告(只显示警告和错误),默认为关闭报告;
5-检查整个工程
$ ll -Ri testproject/ testproject/: total 133940649673951194 -rw-r--r-- 1 guowli 1049089 25 Sep 21 09:37 __init__.py5348024557504430 -rw-r--r-- 1 guowli 1049089 5241 Sep 21 09:37 Chapter06_Modules.py3377699720529875 -rw-r--r-- 1 guowli 1049089 2719 Sep 12 09:17 Chapter06_ModuleTest.py 17732923532773334 drwxr-xr-x 1 guowli 1049089 0 Nov 12 10:46 TestPackage/testproject/TestPackage: total 81970324836976601 -rw-r--r-- 1 guowli 1049089 818 Sep 21 09:37 __init__.py 25895697857382360 -rw-r--r-- 1 guowli 1049089 1338 Sep 21 09:37 ModuleTest.py$ py -m pylint --rcfile=pylint.conf testproject/ ************* Module testproject.Chapter06_Modules testproject\Chapter06_Modules.py:1:0: C0103: Module name "Chapter06_Modules" doesn't conform to snake_case naming style (invalid-name) testproject\Chapter06_Modules.py:1:0: C0111: Missing module docstring (missing-docstring) testproject\Chapter06_Modules.py:4:0: E0401: Unable to import 'Chapter06_ModuleTest' (import-error) testproject\Chapter06_Modules.py:5:0: E0401: Unable to import 'TestPackage' (import-error) testproject\Chapter06_Modules.py:6:0: E0401: Unable to import 'TestPackage.ModuleTest' (import-error) testproject\Chapter06_Modules.py:22:0: C0103: Constant name "mp" doesn't conform to UPPER_CASE naming style (invalid-name) testproject\Chapter06_Modules.py:8:0: C0411: standard import "import os" should be placed before "import Chapter06_ModuleTest as cMT" (wrong-import-order) testproject\Chapter06_Modules.py:9:0: C0411: standard import "import pprint" should be placed before "import Chapter06_ModuleTest as cMT" (wrong-import-order) ************* Module testproject.Chapter06_ModuleTest testproject\Chapter06_ModuleTest.py:1:0: C0103: Module name "Chapter06_ModuleTest" doesn't conform to snake_case naming style (invalid-name) testproject\Chapter06_ModuleTest.py:1:0: C0111: Missing module docstring (missing-docstring) testproject\Chapter06_ModuleTest.py:6:0: C0111: Missing function docstring (missing-docstring) testproject\Chapter06_ModuleTest.py:18:0: C0111: Missing function docstring (missing-docstring) testproject\Chapter06_ModuleTest.py:19:4: R1705: Unnecessary "else" after "return" (no-else-return) ************* Module testproject.TestPackage.ModuleTest testproject\TestPackage\ModuleTest.py:1:0: C0103: Module name "ModuleTest" doesn't conform to snake_case naming style (invalid-name) testproject\TestPackage\ModuleTest.py:1:0: C0111: Missing module docstring (missing-docstring) testproject\TestPackage\ModuleTest.py:5:0: C0111: Missing function docstring (missing-docstring) ************* Module testproject.TestPackage testproject\TestPackage\__init__.py:1:0: C0103: Module name "TestPackage" doesn't conform to snake_case naming style (invalid-name) testproject\TestPackage\__init__.py:1:0: C0111: Missing module docstring (missing-docstring)----------------------------------- Your code has been rated at 3.18/10
6-集成到PyCharm
![](/assets/blank.gif)
![](/assets/blank.gif)
![](/assets/blank.gif)
7-参考信息
- 如何使用 Pylint 来规范 Python 代码风格:http://python.jobbole.com/87415/
8-其他工具
pycodestyle
- 根据PEP8中的某些样式约定来检查Python代码的工具
- https://pypi.org/project/pycodestyle/
- 使用pip安装pycodestyle:pip install pycodestyle
- 从终端运行pycodestyle:pycodestyle code.py
flake8
- 结合了pyflakes和pycodestyle的工具
- https://pypi.org/project/flake8/
- 使用pip安装flake8:pip install flake8
- 从终端运行flake8:flake8 code.py
转载于:https://www.cnblogs.com/anliven/p/10004587.html
Python - 使用Pylint检查分析代码相关推荐
- Python门店菜品标签分析-代码
- python--如何使用 Pylint 来检查分析Python 代码
Pylint 是什么 Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准(Pylint 默认使用的代码风格是 PEP 8,具体信息,请参阅参考 ...
- Python 进阶 — Pylint 静态代码检查工具
目录 文章目录 目录 Pylint 错误类型 安装 使用 Pylint 与 Flake8 一般,Pylint 也是一款 Python 的静态代码检查工具,它会分析 Python 代码中的错误,查找不符 ...
- python中pylint使用方法(pylint代码检查)
一.Pylint 是什么 Pylint 是一个 Python 代码分析工具,它分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码. Pylint 是一个 Python 工具,除 ...
- vscode python第三方库检测_VSCode中使用Pylint检查python代码
为什么使用lint 在日常开发中,不同开发人员会写下不同风格的代码,导致代码可维护性变差,为了解决风格不一致问题,我们可以制定代码规范,让开发人员都遵守同样的规范编写代码.在开发过程中,部分代码存在质 ...
- VSCode中使用Pylint检查python代码
为什么使用lint 在日常开发中,不同开发人员会写下不同风格的代码,导致代码可维护性变差,为了解决风格不一致问题,我们可以制定代码规范,让开发人员都遵守同样的规范编写代码.在开发过程中,部分代码存在质 ...
- 硬核!Python 四种变量的代码对象和反汇编分析
作者 | 大奎 整理 | 阳哥 来源丨Python数据之道 在Python基础的学习过程中,对变量和参数的理解有助于我们从更基础层面了解Python语言的运行.在这个过程中,还是有不少冷门和细节的地方 ...
- python关联分析代码_1行代码实现关联分析(Apriori)算法
本文不涉及关联分析算法的计算原理,只注重代码实现. 最近公司分了个任务,要求写一篇面向python初学者的关联分析演习材料.遇到这种情况,我的解决办法当然是优先使用已有模块,然后写一篇模块使用方法指南 ...
- python体育竞技分析代码200行_使用Python进行体育竞技分析(预测球队成绩)
使用Python进行体育竞技分析(预测球队成绩) 发布时间:2020-09-18 06:38:27 来源:脚本之家 阅读:69 今天我们用python进行体育竞技分析,预测球队成绩 一. 体育竞技分析 ...
- 在windows下安装pyLint,对python进行语法检查
操作的url=http://thinkhole.org/wp/2006/01/16/installing-pylint-on-windows/ 1.安装python.配置系统环境变量,增加python ...
最新文章
- dlink打印服务器重置,单口打印服务器 D-Link DP-302简析
- python画五角星填充不同颜色_Python绘制分形树(一)
- python封装函数、实现将任意的对象序列化到磁盘上_序列化(serialization)
- 简单的busybox创建_用Busybox创建文件系统
- .net core 基于Dapper 的分库分表开源框架(core-data)
- 怎么用计算机算成250,万能计算器
- windows安装Linux卡logo,Dell xps 15 windows ubuntu16.04 UEFI 双系统安装 卡在logo界面 卡***问题解决...
- CodeForces-4C Registration system
- 计算机专业直接工作简历,计算机专业个人简历工作经验怎么写
- 基于AIML2.0写一个机器人
- js 利用audio buffers[int16Array]计算分贝
- 用MFC开发1连连看辅助器
- 一文带你了解Serverless架构及应用场景
- Centos下搭建个人网站
- 在线JSON转TSV工具
- IT运维的相关需求分析报告(第一篇)
- Asp.Net Core 2.1 取消HTTPS配置
- js 中日期转换成时间戳
- python分割_Python文件合并与分割操作方法工具
- android 文件浏览器源码,android 文件管理器源码