Pycharm Autopep8的配置

Autopep8简介

官方解释:autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pycodestyle 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 pycodestyle.

官网地址:https://pypi.org/project/autopep8/#installation

Python语言的编码遵从PEP8规范,Autopep8工具能够依据PEP8规范,快速对代码文件进行规范检查并自动排版。对于变量名,类名,函数名等,会给出提示信息,需要工程师手动修改。

 

Autopep8的安装。有了pip工具,可以用命令轻松安装:

$ pip install autopep8

 

Autopep8的命令行格式运行:

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

 

在Pycharm中集成Autopep8:

Step1:

File -> Settings… ->Tools -> External Tools -> 点击“+”号添加

 

 

Step2:

在添加界面,有5处需要添加

 

Name:autopep8

Program:C:\Python27\Scripts\autopep8.exe(在python的Scripts目录下)

Arguments:--in-place --aggressive --aggressive $FilePath$

Working directory:$ProjectFileDir$

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

 

配置完成后,点击OK结束。

 

Step3:

右键点击需要规范的文件,在菜单中找到External Tools -> autopep8。完成!

 

 

规范完了之后,文件中存在一些波浪线的代码,就是需要手动修改的地方。

 

上面是完全按照标准的PEP8规范进行代码格式化的。但是有一些规范,可以忽略,此时可以在Parameters中加入—ignore参数。如:

Parameters:--in-place --aggressive --ignore=E123,E133,E50 $FilePath$

--ignore后面的E123等就是PEP8的规范代号,具体所指的规则,可以在这里查看到:

https://pep8.readthedocs.io/en/latest/intro.html#configuration

用户可以根据需要,添加需要忽略的规则。

 

posted @ 2018-09-28 21:15  心笔轩  阅读(3741)  评论(0编辑  收藏  举报