Python - 使用Pylint檢查分析代碼


1-簡介

Home Page  : https://www.pylint.org/
  • 檢查語法錯誤,是否遵守編碼風格標准、潛在的問題等;
  • 支持自定義配置:例如顯示或隱藏特定的警告,並且可以通過編寫插件來添加功能;
  • 使用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 time


def 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-檢查整個工程

在工程根目錄下添加init.py文件,即把工程當做一個python包,可以對整個工程進行pylint;
$ ll -Ri testproject/
testproject/:
total 13
 3940649673951194 -rw-r--r-- 1 guowli 1049089   25 Sep 21 09:37 __init__.py
 5348024557504430 -rw-r--r-- 1 guowli 1049089 5241 Sep 21 09:37 Chapter06_Modules.py
 3377699720529875 -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 8
 1970324836976601 -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

File --》Settings --》Tools --》External Tools

 

保存並應用后,Tools菜單下將會顯示pylint工具選項;

 

如果想要pylint當前文件,只需要點擊此選項即可;

 

7-其他工具

pycodestyle

  • 根據PEP8中的某些樣式約定來檢查Python代碼的工具
  • https://pypi.org/project/pycodestyle/
  • 使用pip安裝pycodestyle:pip install pycodestyle
  • 從終端運行pycodestyle:pycodestyle code.py


flake8

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM