天天看點

python開發配置使用autopep8文法規則檢查,指令行使用及pycharm配置使用方法

python開發配置使用autopep8文法規則檢查,指令行使用及pycharm配置使用方法。

關于PEP 8

PEP 8,Style Guide forpythonCode,是Python官方推出編碼約定,主要是為了保證 Python 編碼的風格一緻,提高代碼的可讀性。

官網位址:https://www.python.org/dev/peps/pep-0008/

關于Autopep8

關于autopep8官網的描述是:

autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pep8 utility to determine what parts of the code needs to be formatted. autopep8 is capable of fixing most of the formatting issues that can be reported by pep8.

Autopep8是自動将Python代碼格式化為符合PEP 8風格的工具。它使用pycodestyle工具來确定代碼的哪些部分需要被格式化。Autopep8能夠修複大部分pycodestyle檢測的格式問題。

github位址:https://github.com/hhatto/autopep8

下載下傳安裝Autopep8

注意:需要進行全局安裝,不能到某個項目下的虛拟環境中安裝。

pip3 install      

使用Autopep8

指令行使用方式如下:

autopep8 --in-place --aggressive --aggressive <filename>      

Pycharm配置Autopep8方法:

1)具體流程:選擇菜單「File」–>「Settings」–>「Tools」–>「External Tools」–>設定相關配置 -> 點選加号添加工具

填寫如下配置項,點選「OK」儲存

Settings–>Tools–>External Tools 點選添加按鈕Name:autopep8(可以自定義)

Tools settings:

Programs:autopep8(不能修改)
Parameters:--in-place --aggressive --aggressive $FilePath$
Working directory:$ProjectFileDir$      

Advanced Options:在output filters添加:

$FILE_PATH$\:$LINE$\:$COLUMN$\:.*      

使用autopep8自動格式化你的python代碼:

第一種方式:

編寫完代碼後,右鍵選擇「Extern Tools」–>「autopep8」

第二種方式:

選擇菜單「Tool」–>「Extern Tools」–>「autopep8」即可。

autopep8的一些設定點 :

--in-place --aggressive --aggressive $FilePath$      
--in-place --ignore=E501 $FilePath$