explaingit

hhatto/autopep8

4,668PythonAudience · developerComplexity · 2/5Setup · easy

TLDR

A command-line tool that automatically reformats Python code to match PEP 8 style conventions, fixing indentation, spacing, line length, and other issues without changing the program's behavior.

Mindmap

mindmap
  root((autopep8))
    What it does
      PEP 8 style fixer
      Auto-reformatter
      In-place rewrite
      Diff preview mode
    Tech Stack
      Python
      pycodestyle
      pip install
    Use Cases
      Pre-commit formatting
      CI style enforcement
      Project cleanup
    Audience
      Python developers
      Code reviewers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Automatically fix PEP 8 style violations in Python files before committing to version control.

USE CASE 2

Integrate into a CI pipeline to enforce consistent Python code style across a team codebase.

USE CASE 3

Reformat an entire Python project directory to the official style guide in one command.

Tech stack

Pythonpycodestyle

Getting it running

Difficulty · easy Time to first run · 5min

In plain English

autopep8 is a command-line tool that automatically reformats Python code to match PEP 8, the official Python style guide. PEP 8 defines conventions for things like indentation width, spacing around operators, line length limits, and how to write comments. When code does not follow these conventions, it can be harder to read and may trigger warnings from linters and code review tools. autopep8 fixes those issues without you having to edit the code by hand. The tool works by scanning a Python file for style violations using another tool called pycodestyle, and then applying fixes for each category of problem it finds. The README shows a clear before-and-after example: messy code with extra semicolons, inconsistent spacing, cramped imports, and long lines is transformed into clean, properly formatted code. The content inside strings is left untouched, since reformatting text within string literals would change the program's behavior. You can run it in a few modes. By default it prints a preview of the changes. With the --in-place flag it rewrites the file directly. The --diff flag shows only what would change without applying anything. For fixes that go beyond whitespace (such as removing deprecated method calls), you pass one or more --aggressive flags, with each additional flag allowing more opinionated changes. The tool can operate on a single file, a list of files, or an entire directory tree with the --recursive flag. You can also limit it to a specific range of line numbers, exclude certain files or folders, tell it to fix only specific error codes, or ignore certain codes you do not care about. Installation is through pip. It requires pycodestyle as a dependency and works on standard Python environments.

Copy-paste prompts

Prompt 1
Write a shell command using autopep8 that reformats all Python files in my project directory in-place while excluding the tests/ folder.
Prompt 2
How do I configure autopep8 to fix only E501 line-too-long errors and ignore all other PEP 8 warnings?
Prompt 3
Show me how to set up a git pre-commit hook using autopep8 to automatically format Python files before every commit.
Prompt 4
Using autopep8 with the --aggressive flag, what kinds of changes go beyond whitespace? Give me examples of before and after.
Open on GitHub → Explain another repo

← hhatto on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.