python代码格式化工具_autopep8的安装与使用

一、安装

  pip install autopep8

(CloudStorage) D:learnIOTCloudStorageunit>pip install autopep8
Collecting autopep8
  Downloading https://files.pythonhosted.org/packages/5e/41/5bfb10d1a480556ff114cce2ba124416b86197ef7bd6e600b021477856d9/autopep8-1.5.6-py2.py3-none-any.whl
 (44kB)
    100% |████████████████████████████████| 51kB 402kB/s
Requirement already satisfied: toml in d:python3.7.2libsite-packages (from autopep8) (0.10.1)
Collecting pycodestyle>=2.7.0 (from autopep8)
  Downloading https://files.pythonhosted.org/packages/de/cc/227251b1471f129bc35e966bb0fceb005969023926d744139642d847b7ae/pycodestyle-2.7.0-py2.py3-none-any.
whl (41kB)
    100% |████████████████████████████████| 51kB 452kB/s
Installing collected packages: pycodestyle, autopep8
Successfully installed autopep8-1.5.6 pycodestyle-2.7.0

二、验证安装是否成功

  导入时无报错即可

(CloudStorage) D:learnIOTCloudStorage>python
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import autopep8
>>>

三、pycharm配置

  1、进入settings-tools-external tools添加配置

  

   2、填写配置内容

    Name : autopep8

    Tool Seetings:

      Program : 选择autupep8的下载存储路径

      parameters :--in-place --aggressive --aggressive $FilePath$

      working directory : $ProjectFileDir$

     Advanced Options:

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

    

   3、点击ok,再点击应用

四、验证autopep8格式化工具的效果

  before

import math, sys;

def example1():
    ####This is a long comment. This should be wrapped to fit within 72 characters.
    some_tuple=(   1,2, 3,'a'  );
    some_variable={'long':'Long code lines should be wrapped within 79 characters.',
    'other':[math.pi, 100,200,300,9876543210,'This is a long string that goes on'],
    'more':{'inner':'This whole logical line should be wrapped.',some_tuple:[1,
    20,300,40000,500000000,60000000000000000]}}
    return (some_tuple, some_variable)
def example2(): return {'has_key() is deprecated':True}.has_key({'f':2}.has_key(''));
class Example3(   object ):
    def __init__    ( self, bar ):
     #Comments should have a space after the hash.
     if bar : bar+=1;  bar=bar* bar   ; return bar
     else:
                    some_string = """
                       Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
                    return (sys.path, some_string)

after

import math
import sys


def example1():
    # This is a long comment. This should be wrapped to fit within 72
    # characters.
    some_tuple = (1, 2, 3, 'a')
    some_variable = {
        'long': 'Long code lines should be wrapped within 79 characters.',
        'other': [
            math.pi,
            100,
            200,
            300,
            9876543210,
            'This is a long string that goes on'],
        'more': {
            'inner': 'This whole logical line should be wrapped.',
            some_tuple: [
                1,
                20,
                300,
                40000,
                500000000,
                60000000000000000]}}
    return (some_tuple, some_variable)


def example2(): return ('' in {'f': 2}) in {'has_key() is deprecated': True}


class Example3(object):
    def __init__(self, bar):
        # Comments should have a space after the hash.
        if bar:
            bar += 1
            bar = bar * bar
            return bar
        else:
            some_string = """
                       Indentation in multiline strings should not be touched.
Only actual code should be reindented.
"""
            return (sys.path, some_string)

  

  使用方法说明:

  1、pycharm

  选择要格式化的文件---Externer Tools---autopep8

 2、命令行

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

知道、想到、做到、得到
原文地址:https://www.cnblogs.com/Durant0420/p/14564338.html