SoFunction
Updated on 2024-11-21

Pycharm configuration autopep8 implementation process analysis

About PEP 8

PEP 8, Style Guide for PythonCode, is an official coding convention introduced by Python, mainly to ensure a consistent style of Python coding and to improve code readability.

Official website address:/dev/peps/pep-0008/

About Autopep8

Autopep8 is a tool that automatically formats Python code to conform to the PEP 8 style. It uses the pycodestyle tool to determine which parts of the code need to be formatted.Autopep8 is able to fix most of the formatting problems detected by pycodestyle.

github address:/hhatto/autopep8

Download and install Autopep8

pip install autopep8

Using Autopep8

The command line is used as follows

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

Pycharm Configuration Autopep8 Methods

(1) Specific process: Select menu "File" -> "Settings" -> "Tools" -> "External Tools" -> set the relevant configurations -> Click plus sign to add tools

Fill in the following configuration items and click "OK" to save.

Settings->Tools->External Tools Click to add button Name: autopep8 (can be customized)

Tools settings:

Programs: autopep8 (cannot be modified)
Parameters:--in-place --aggressive --aggressive $FilePath$
Working directory:$ProjectFileDir$

Advanced Options:existoutput filtersincrease:$FILE_PATH$\:$LINE$\:$COLUMN$\:.*

3) Automatically format your python code with autopep8

import math


def example1():
  some_tuple = (1, 2, 3, 'a')
  some_variable = {
    'long': 'Long code lines should be wrapped within 79 characters.',
    'other': [, 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
    else:
      some_string = """
            Indentation in multiline strings should not be  actual code should be reindented.
"""

The first way:

After writing the code, right click and select "Extern Tools" - > "autopep8"

The second way:

Select "Tool" - > "Extern Tools" - > "autopep8" in the menu.

This is the whole content of this article.