PyCharm使用autopep8格式化Python代码
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
使用pip安装Autopep8
pip install autopep8
查找autopep8的安装路径:
where autopep8
C:\Users\mahl01\.virtualenvs\python-project-APBqVzYh\Scripts\autopep8.exe
Autopep8命令行使用方式如下
autopep8 --in-place --aggressive --aggressive <filename>
Pycharm配置Autopep8方法:
1、选择菜单「File」–>「Settings」–>「Tools」–>「External Tools」–>点击加号添加工具
2、填写如下配置项,点击「OK」保存
Name:Autopep8 (可随意填写)
Tools settings:
Programs:C:\Users\mahl01\.virtualenvs\python-project-APBqVzYh\Scripts\autopep8.exe
Parameters:--in-place --aggressive --ignore=E123,E133,E50 $FilePath$
Working directory:$ProjectFileDir$
3、选择菜单「Tool」–>「Extern Tools」–>「Autopep8」即可使用autopep8自动格式化python代码了。
black的安装设置方法和Autopep8一致。