Automatically fix PEP 8 style violations in Python files before committing to version control.
Integrate into a CI pipeline to enforce consistent Python code style across a team codebase.
Reformat an entire Python project directory to the official style guide in one command.
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.
← hhatto on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.