Pycharm配置autopep8:自動調整代碼為PEP8風格


關於PEP 8

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

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

關於Autopep8

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

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

下載安裝Autopep8

pip install autopep8

使用Autopep8

命令行使用方式如下

$ autopep8 --in-place --aggressive <filename>
$ autopep8 --in-place --aggressive Student.py

Pycharm配置Autopep8方法

1)選擇菜單「File」–>「Settings」–>「Tools」–>「External Tools」–>點擊加號添加工具

 

2)填寫如下配置項,點擊「OK」保存

 

Name:autopep8 (可隨意填寫)

Tools settings:

    Programs:autopep8

    Parameters:--in-place --aggressive $FilePath$

    Working directory:$ProjectFileDir$

 

點擊Output Filters,點擊彈窗右側加號添加Filter, 在Regular expression to match output中

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

 

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

 

第一種方式:

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

 

第二種方式:

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

 

格式化后顯示

 

在命令行中使用方式如下:

autopep8 --in-place --aggressive Student.py

  

 


免責聲明!

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



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