美文网首页Python专辑
PyCharm中自动更改代码风格为PEP 8

PyCharm中自动更改代码风格为PEP 8

作者: ShawnDuan | 来源:发表于2018-03-02 12:51 被阅读1165次

最近在学习Python3,使用的IDE是PyCharm。在写代码的过程中发现总有波浪线出现,并提示PEP 8的相关信息,经过搜索,终于解决了这个问题,现在分享给大家。

1. 什么是PEP 8

PEP 8 风格介绍
Pycharm提供自动检查pep8风格的功能,当代码不符合该风格的时候,代码下方会出现波浪线,但是无法自动调整。

2. 安装Autopep8

使用cmd命令行,输入

pip install autopep8

3. 在PyCharm中配置Autopep 8

  • 本文中使用的PyCharm版本是:
PyCharm 2017.3.3 (Community Edition)
Build #PC-173.4301.16, built on January 11, 2018
JRE: 1.8.0_152-release-1024-b11 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
  • File → Settings → Tools → External Tools → 点击加号
  • 在Creat Tool配置页面做如下设置:

Name: AutoPEP8 (也可以是其他名字)
Program: autopep8
Arguments: --in-place --aggressive --aggressive $FilePath$
Working directory: $ProjectFileDir$
Output filters: $FILE_PATH$\:$LINE$\:$COLUMN$\:.*
完成后点击OK,在Settings界面点击Apply。

  • 在代码界面使用:
    将鼠标放在的编辑器中 → 右键 → External Tools → 点击autopep8。

相关文章

网友评论

  • 309dbe4f0b11:配置完以后运行时候显示Error running 'AUTOPEP8': Cannot run program "autopep8" (in directory "D:\Coding\Playground\python"): CreateProcess error=2, 系统找不到指定的文件.

    pycharm我很久之前配成功过一次,之后再也没成功过

本文标题:PyCharm中自动更改代码风格为PEP 8

本文链接:https://www.haomeiwen.com/subject/zrqaxftx.html