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.
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. $ py -m pylint --version
__main__.py 2.1.
astroid 2.0.
Python 3.6. (v3.6.0:41df79263a11, Dec , ::) [MSC v. bit (AMD64)]
生成默认配置文件:将在当前目录生成pylint.conf文件,包含pylint的默认配置项;
$ py -m pylint --persistent=n --generate-rcfile > pylint.conf $ ll -h pylint.conf
-rw-r--r-- guowli 18K Nov : pylint.conf
4-检查单个文件(模块)
$ cat test.py
import time def shwotime():
print(time.asctime()) shwotime() $ py -m pylint --rcfile=pylint.conf test.py
************* Module test
test.py::: C0111: Missing module docstring (missing-docstring)
test.py::: C0111: Missing function docstring (missing-docstring) -----------------------------------
Your code has been rated at 5.00/ $ py -m pylint -ry --rcfile=pylint.conf test.py
************* Module test
test.py::: C0111: Missing module docstring (missing-docstring)
test.py::: C0111: Missing function docstring (missing-docstring) Report
======
statements analysed. Statistics by type
------------------ +---------+-------+-----------+-----------+------------+---------+
|type |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module | |NC |NC |0.00 |0.00 |
+---------+-------+-----------+-----------+------------+---------+
|class | |NC |NC | | |
+---------+-------+-----------+-----------+------------+---------+
|method | |NC |NC | | |
+---------+-------+-----------+-----------+------------+---------+
|function | |NC |NC |0.00 |0.00 |
+---------+-------+-----------+-----------+------------+---------+ Raw metrics
----------- +----------+-------+------+---------+-----------+
|type |number |% |previous |difference |
+==========+=======+======+=========+===========+
|code | |50.00 |NC |NC |
+----------+-------+------+---------+-----------+
|docstring | |0.00 |NC |NC |
+----------+-------+------+---------+-----------+
|comment | |0.00 |NC |NC |
+----------+-------+------+---------+-----------+
|empty | |50.00 |NC |NC |
+----------+-------+------+---------+-----------+ Duplication
----------- +-------------------------+------+---------+-----------+
| |now |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines | |NC |NC |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |NC |NC |
+-------------------------+------+---------+-----------+ Messages by category
-------------------- +-----------+-------+---------+-----------+
|type |number |previous |difference |
+===========+=======+=========+===========+
|convention | |NC |NC |
+-----------+-------+---------+-----------+
|refactor | |NC |NC |
+-----------+-------+---------+-----------+
|warning | |NC |NC |
+-----------+-------+---------+-----------+
|error | |NC |NC |
+-----------+-------+---------+-----------+ Messages
-------- +------------------+------------+
|message id |occurrences |
+==================+============+
|missing-docstring | |
+------------------+------------+ -----------------------------------
Your code has been rated at 5.00/
- Pylint结果的级别:error,warning,refactor,convention;
- 可以根据首字母确定相应的级别,例如,C表示convention(规范)、W表示warning(告警);
- 级别之后的数字表示告警所在文件中的行号和列号;
- 参数 “-ry”开启报告,“-rn”关闭报告(只显示警告和错误),默认为关闭报告;
5-检查整个工程
$ ll -Ri testproject/
testproject/:
total
-rw-r--r-- guowli Sep : __init__.py
-rw-r--r-- guowli Sep : Chapter06_Modules.py
-rw-r--r-- guowli Sep : Chapter06_ModuleTest.py
drwxr-xr-x guowli Nov : TestPackage/ testproject/TestPackage:
total
-rw-r--r-- guowli Sep : __init__.py
-rw-r--r-- guowli Sep : ModuleTest.py $ py -m pylint --rcfile=pylint.conf testproject/
************* Module testproject.Chapter06_Modules
testproject\Chapter06_Modules.py::: C0103: Module name "Chapter06_Modules" doesn't conform to snake_case naming style (invalid-name)
testproject\Chapter06_Modules.py::: C0111: Missing module docstring (missing-docstring)
testproject\Chapter06_Modules.py::: E0401: Unable to import 'Chapter06_ModuleTest' (import-error)
testproject\Chapter06_Modules.py::: E0401: Unable to import 'TestPackage' (import-error)
testproject\Chapter06_Modules.py::: E0401: Unable to import 'TestPackage.ModuleTest' (import-error)
testproject\Chapter06_Modules.py::: C0103: Constant name "mp" doesn't conform to UPPER_CASE naming style (invalid-name)
testproject\Chapter06_Modules.py::: C0411: standard import "import os" should be placed before "import Chapter06_ModuleTest as cMT" (wrong-import-order)
testproject\Chapter06_Modules.py::: 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::: C0103: Module name "Chapter06_ModuleTest" doesn't conform to snake_case naming style (invalid-name)
testproject\Chapter06_ModuleTest.py::: C0111: Missing module docstring (missing-docstring)
testproject\Chapter06_ModuleTest.py::: C0111: Missing function docstring (missing-docstring)
testproject\Chapter06_ModuleTest.py::: C0111: Missing function docstring (missing-docstring)
testproject\Chapter06_ModuleTest.py::: R1705: Unnecessary "else" after "return" (no-else-return)
************* Module testproject.TestPackage.ModuleTest
testproject\TestPackage\ModuleTest.py::: C0103: Module name "ModuleTest" doesn't conform to snake_case naming style (invalid-name)
testproject\TestPackage\ModuleTest.py::: C0111: Missing module docstring (missing-docstring)
testproject\TestPackage\ModuleTest.py::: C0111: Missing function docstring (missing-docstring)
************* Module testproject.TestPackage
testproject\TestPackage\__init__.py::: C0103: Module name "TestPackage" doesn't conform to snake_case naming style (invalid-name)
testproject\TestPackage\__init__.py::: C0111: Missing module docstring (missing-docstring) -----------------------------------
Your code has been rated at 3.18/
6-集成到PyCharm
![](https://img2018.cnblogs.com/blog/819128/201811/819128-20181122230943798-2072573280.png)
![](/Anliven/DataDir/WizNote/temp/8aa5e13f-b2ae-4680-8b5d-aec7e2347afe/128/index_files/05cf6427-07c7-48c2-a383-047d448e15c4.png)
![](https://img2018.cnblogs.com/blog/819128/201811/819128-20181122230958354-1807657351.png)
![](/Anliven/DataDir/WizNote/temp/8aa5e13f-b2ae-4680-8b5d-aec7e2347afe/128/index_files/7141429.png)
![](https://img2018.cnblogs.com/blog/819128/201811/819128-20181122231013195-956689194.png)
7-其他工具
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
Python - 使用Pylint检查分析代码的更多相关文章
- Xcode静态检查分析代码
Clang静态分析和Instruments来剖析代码有一些不同,Clang更致力于在编译的过程中通过自身的一套判断机制来找出代码中潜在的隐患. 在XCode 3.2之后的版本里,Clang已经被集 ...
- 网站图片挂马检测及PHP与python的图片文件恶意代码检测对比
前言 周一一早网管收到来自阿里云的一堆警告,发现我们维护的一个网站下有数十个被挂马的文件.网管直接关了vsftpd,然后把警告导出邮件给我们. 取出部分大致如下: 服务器IP/名称 木马文件路径 更新 ...
- Python小数据池,代码块
今日内容一些小的干货 一. id is == 二. 代码块 三. 小数据池 四. 总结 python小数据池,代码块的最详细.深入剖析 一. id is == 二. 代码块 三. 小数据池 四. ...
- python 小数据池,代码块, is == 深入剖析
python小数据池,代码块的最详细.深入剖析 一. id is == 二. 代码块 三. 小数据池 四. 总结 一,id,is,== 在Python中,id是什么?id是内存地址,那就有人问了, ...
- 检查.net代码中占用高内存函数(翻译)
哈哈,昨天没事做,在CodeProject瞎逛,偶然看到这篇文章,居然读得懂,于是就翻译了一下,当练习英语,同时增强对文章的理解,发现再次翻译对于文章的一些细节问题又有更好的理解.下面是翻译内容,虽然 ...
- 常用排序算法的python实现和性能分析
常用排序算法的python实现和性能分析 一年一度的换工作高峰又到了,HR大概每天都塞几份简历过来,基本上一天安排两个面试的话,当天就只能加班干活了.趁着面试别人的机会,自己也把一些基础算法和一些面试 ...
- python的引用计数分析(二)
python所有对象引用计数被减少1的情况: 一.对象的别名被赋予新的对象; a = 23345455 # 增加了一个引用 b = a # 增加了一个引用 print(sys.getrefcount( ...
- python的计数引用分析(一)
python的垃圾回收采用的是引用计数机制为主和分代回收机制为辅的结合机制,当对象的引用计数变为0时,对象将被销毁,除了解释器默认创建的对象外.(默认对象的引用计数永远不会变成0) 所有的计数引用+1 ...
- 《构建之法》教学笔记——Python中的效能分析与几个问题
<构建之法:现代软件工程>中第2章对效能分析进行了介绍,基于的工具是VSTS.由于我教授的学生中只有部分同学选修了C#,若采用书中例子讲解,学生可能理解起来比较困难.不过所有这些学生都学习 ...
随机推荐
- P3383 【模板】线性筛素数
因为数据很大所以要用线性筛.. #include<iostream> #include<cstdio> using namespace std; typedef long lo ...
- 用SAX解析xml文件,java
(此文为(https://www.imooc.com/video/4482)之随笔) 1.用SAX解析xml文件大致分为三步 写了一个XML文件作为例子 (1)main方法代码如下: import j ...
- TCP/IP数据加密传输及CA简述
TCP/IP跨主机之间的通信数据封装发送的都是明文数据,现代通讯中会有安全问题. 三个安全问题 如:A发送消息给B的三个安全问题机密性:明文传输如:ftp,http,smtp,telnet等完整性:数 ...
- Javaweb拦截器
http://blog.csdn.net/reggergdsg/article/details/52962774
- python 常忘代码查询 和autohotkey补括号脚本和一些笔记和面试常见问题
笔试一些注意点: --,23点43 今天做的京东笔试题目: 编程题目一定要先写变量取None的情况.今天就是因为没有写这个边界条件所以程序一直不对.以后要注意!!!!!!!!!!!!!!!!!!!!! ...
- Nexus 3 搭建 npm 私服 (windows)
Nexus 3 搭建 npm 私服备忘 下载与安装 在官网下载Nexus Repository Manager OSS 3.x, 解压至任意位置. 管理员运行 powershell, 切换到 nexu ...
- 【机器学习】异常检测算法(I)
在给定的数据集,我们假设数据是正常的 ,现在需要知道新给的数据Xtest中不属于该组数据的几率p(X). 异常检测主要用来识别欺骗,例如通过之前的数据来识别新一次的数据是否存在异常,比如根据一个用户以 ...
- 20155312 张竞予 Exp6 信息搜集与漏洞扫描
Exp6 信息搜集与漏洞扫描 目录 基础问题回答 (1)哪些组织负责DNS,IP的管理. (2)什么是3R信息. (3)评价下扫描结果的准确性. 实验总结与体会 实践过程记录 (1)各种搜索技巧的应用 ...
- Naïve Media Player
我的GitHub链接:https://github.com/Joyce45/hwt 一.播放器?怎么做? 提到播放器,首先想到的就是XAML控件库里的MediaElement,于是我又大致阅读了一下该 ...
- re正则表达式的使用
1.查找电话号码 #! coding=utf-8import re"""查找字符串中的文本"""txt="your number ...