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

命令行使用方式如下:

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

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的一些设置点 :

在上边说到,在Parameters的设置是:

--in-place --aggressive --aggressive $FilePath$

–in-place 代表会直接修改源文件
–aggressive autopep8默认只修复空白,对齐相关的PEP8问题,加入–aggressive设置,会增加修复如 x == None 修复为 x is None,{“a”: 1, “b”: 2}.has_key(‘a’) 修复为’a’ in {“a”: 1, “b”: 2}
–ignore 忽略PEP8检查项
因为我只打算用autopep8来修复空格,空行这一类的排版问题,同时要忽略每一行长度过长的检测(E501 - Try to make lines fit within –max-line-length characters.),
所以最终设置是:

--in-place --ignore=E501 $FilePath$
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码讲故事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值