使用如下命令:
pip3 install pylint
安裝完后可以看到在你的python3的目錄底下的Scripts目錄下有pylint.exe了
然后就可以使用pylint 評估你的代碼了,如:
E:\Learning\AWS\Python>pylint learning.py No config file found, using default configuration ************* Module learning C: 7, 0: No space allowed before bracket print (queue.pop()) ^ (bad-whitespace) C: 8, 0: No space allowed before bracket print (queue) ^ (bad-whitespace) C: 9, 0: No space allowed before bracket print (queue.popleft()) ^ (bad-whitespace) C: 23, 0: No space allowed before bracket print ("row:") ^ (bad-whitespace) C: 24, 0: No space allowed before bracket print (row) ^ (bad-whitespace) C: 34, 0: Trailing newlines (trailing-newlines) C: 1, 0: Missing module docstring (missing-docstring) C: 4, 0: Constant name "queue" doesn't conform to UPPER_CASE naming style (invalid-name) C: 11, 0: Constant name "freshfruit" doesn't conform to UPPER_CASE naming style (invalid-name) C: 15, 0: Constant name "matrix" doesn't conform to UPPER_CASE naming style (invalid-name) C: 30, 0: Constant name "comb" doesn't conform to UPPER_CASE naming style (invalid-name) ----------------------------------- Your code has been rated at 3.89/10
安裝完畢可以在PyCharm中關聯pylint:
進入PyCharm,從菜單欄,依次進入: File -> Settings -> Tools -> External Tools。
“+”,進行添加。需要填寫的部分分別是:“Name”,“Tool Settings -> Programs”、“Tool Settings -> Parameters”、“Tool Settings -> Working directory”。
配置完畢,選擇一個Python程序,右鍵點擊,快捷菜單中會有“Extensions Tools -> Pylint”,點擊運行即可。輸出結果在執行程序結果的窗口(IDE下半部分)。
Pylint 的輸出
Pylint的默認輸出格式是原始文
本(raw text)格式 ,可以通過 -f <format>,--output-format=<format> 來指定別的輸出格式如
html等等。在
Pylint的輸出中有如下兩個部分:源代碼分析部分
和報告部分。
源代碼分析部分:
對於每一個 Python 模塊,Pylint 的結果中首先顯示一些"*"字符 , 后面緊跟模塊的名字,然后是一系列的 message, message 的格式如下:
1
|
MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE
|
MESSAGE_TYPE 有如下幾種:
(C) 慣例。違反了編碼風格標准
(R) 重構。寫得非常糟糕的代碼。
(W) 警告。某些 Python 特定的問題。
(E) 錯誤。很可能是代碼中的錯誤。
(F) 致命錯誤。阻止 Pylint 進一步運行的錯誤。