You can use autopep8! Whilst you make yourself a cup of coffee this tool happily removes all those pesky PEP8 violations which don't change the meaning of the code.

Install it via pip:

pip install autopep8

Apply this to a specific file:

autopep8 py_file --in-place

or to your project (recursively), the verbose option gives you some feedback of how it's going:

autopep8 project_dir --recursive --in-place --pep8-passes 2000 --verbose

Note: Sometimes the default of 100 passes isn't enough, I set it to 2000 as it's reasonably high and will catch all but the most troublesome files (it stops passing once it finds no resolvable pep8 infractions)...

At this point I suggest retesting and doing a commit!

If you want "full" PEP8 compliance: one tactic I've used is to run autopep8 as above, then run PEP8, which prints the remaining violations (file, line number, and what):

pep8 project_dir --ignore=E501

and manually change these individually (e.g. E712s - comparison with boolean).

Note: autopep8 offers an --aggressive argument (to ruthlessly "fix" these meaning-changing violations), but beware if you do use aggressive you may have to debug... (e.g. in numpy/pandas True == np.bool_(True) but not True is np.bool_(True)!)

You can check how many violations of each type (before and after):

pep8 --quiet --statistics .

Note: I consider E501s (line too long) are a special case as there will probably be a lot of these in your code and sometimes these are not corrected by autopep8.

As an example, I applied this technique to the pandas code base.

Answer from Andy Hayden on Stack Overflow
🌐
PyPI
pypi.org › project › autopep8
Pypi
JavaScript is disabled in your browser. Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
🌐
Python
peps.python.org › pep-0008
PEP 8 – Style Guide for Python Code | peps.python.org
Function annotations should use PEP 484 syntax (there are some formatting recommendations for annotations in the previous section).
🌐
Reddit
reddit.com › r/learnpython › vscode pep 8 formatting
r/learnpython on Reddit: VSCode PEP 8 Formatting
May 16, 2023 -

Hello everyone!

I'm interested in formatting my code so that it matches PEP 8 guidelines. I've read through PEP 8, but as you can imagine, referencing the documentation and checking my code for format is pretty time consuming.

I know that it is possible to install extensions that do this automatically in VSCode, but in order to learn best practices, I actually want to make the code changes myself.

I did some searching through the VSCode docs, but I wasn't able to find quite what I was looking for. Here's what I want:

  • A formatter which runs every time the file is saved

  • I do not want it to make any changes automatically

  • I want all the non-compliant code to be listed somewhere like the "Problems" tab in the terminal window.

  • If it adds non-intrusive squiggles in my code that's a plus (especially if I can toggle to hide it)

  • Ideally verbose enough that I know what the issue is

  • If there is links to the relevant PEP 8 guideline in the output that would be great as well.

Does anything like this exist? I'm sure the popular tools already do what I'm looking for, but I wasn't able to figure out how to configure it to the way I want. Surprisingly, there's a lot more people are looking to make the formatting less manual, not more. :)

Thank you all!

Top answer
1 of 6
218

You can use autopep8! Whilst you make yourself a cup of coffee this tool happily removes all those pesky PEP8 violations which don't change the meaning of the code.

Install it via pip:

pip install autopep8

Apply this to a specific file:

autopep8 py_file --in-place

or to your project (recursively), the verbose option gives you some feedback of how it's going:

autopep8 project_dir --recursive --in-place --pep8-passes 2000 --verbose

Note: Sometimes the default of 100 passes isn't enough, I set it to 2000 as it's reasonably high and will catch all but the most troublesome files (it stops passing once it finds no resolvable pep8 infractions)...

At this point I suggest retesting and doing a commit!

If you want "full" PEP8 compliance: one tactic I've used is to run autopep8 as above, then run PEP8, which prints the remaining violations (file, line number, and what):

pep8 project_dir --ignore=E501

and manually change these individually (e.g. E712s - comparison with boolean).

Note: autopep8 offers an --aggressive argument (to ruthlessly "fix" these meaning-changing violations), but beware if you do use aggressive you may have to debug... (e.g. in numpy/pandas True == np.bool_(True) but not True is np.bool_(True)!)

You can check how many violations of each type (before and after):

pep8 --quiet --statistics .

Note: I consider E501s (line too long) are a special case as there will probably be a lot of these in your code and sometimes these are not corrected by autopep8.

As an example, I applied this technique to the pandas code base.

2 of 6
44

Unfortunately "pep8 storming" (the entire project) has several negative side-effects:

  • lots of merge-conflicts
  • break git blame
  • make code review difficult

As an alternative (and thanks to @y-p for the idea), I wrote a small package which autopep8s only those lines which you have been working on since the last commit/branch:

Basically leaving the project a little better than you found it:

pip install pep8radius

Suppose you've done your work off of master and are ready to commit:

# be somewhere in your project directory
# see the diff with pep, see the changes you've made since master
pep8radius master --diff
# make those changes
pep8radius master --diff --in-place

Or to clean the new lines you've commited since the last commit:

pep8radius --diff
pep8radius --diff --in-place

# the lines which changed since a specific commit `git diff 98f51f`
pep8radius 98f51f --diff

Basically pep8radius is applying autopep8 to lines in the output of git/hg diff (from the last shared commit).

This script currently works with git and hg, if your using something else and want this to work please post a comment/issue/PR!

🌐
Package Control
packagecontrol.io › packages › Python PEP8 Autoformat
Python PEP8 Autoformat - Packages - Package Control
Python PEP8 Autoformat is a Sublime Text (2|3) plugin to interactively reformat Python source code according to PEP8 (Style Guide for Python Code).
🌐
Built In
builtin.com › data-science › autopep8-vs-black
Python Auto Formatter: Autopep8 vs. Black | Built In
Autopep8 and Black are both great tools to auto format your Python code to conform to the PEP 8 style guide. Black is the most popular tool of its kind based on GitHub activity, while autopep8 is slightly less popular. Autopep8: Autopep8 is an auto formatter for Python that edits code to align with the PEP 8 style guide...
🌐
Agiliq
books.agiliq.com › projects › essential-python-tools › en › latest › linters.html
Linters and formatters — Essential Python Tools 3.7 documentation
autopep8 automatically formats Python code to the PEP8 style. It fixes most of the formatting issues that are reported by pycodestyle.
🌐
GitHub
github.com › google › yapf
GitHub - google/yapf: A formatter for Python files · GitHub
The formatting style used by YAPF is configurable and there are many "knobs" that can be used to tune how YAPF does formatting. See the style.py module for the full list. To control the style, run YAPF with the --style argument. It accepts one of the predefined styles (e.g., pep8 or google), a path to a configuration file that specifies the desired style, or a dictionary of key/value pairs.
Starred by 14K users
Forked by 900 users
Languages   Python
Find elsewhere
🌐
GitHub
github.com › hhatto › autopep8
GitHub - hhatto/autopep8: A tool that automatically formats Python code to conform to the PEP 8 style guide. · GitHub
autopep8 automatically formats Python code to conform to the PEP 8 style guide. It uses the pycodestyle utility to determine what parts of the code needs to be formatted.
Starred by 4.7K users
Forked by 288 users
Languages   Python
🌐
Pyrfecter
pyrfecter.com › format-python-code
Format Python code online · Pyrfecter
June 26, 2025 - If you have a very large code base or if your formatting needs to be lightning-fast, give Ruff a try: ... autopep8 formats Python code according to PEP 8 - Style Guide for Python Code. It builds on top of pycodestyle (formerly known as "pep8") by automatically fixing issues that pycodestyle finds.
🌐
Avil Page
avilpage.com › 2015 › 05 › automatically-pep8-your-python-code.html
Automatically PEP8 & Format Your Python Code | Avil Page
May 23, 2015 - Formatting code to PEP8 style is boring & time consuming process. So instead of manually formatting code, You can use autopep8 package which automatically formats Python code to conform to the PEP 8 style guide.
🌐
GitHub
github.com › psf › black
GitHub - psf/black: The uncompromising Python code formatter · GitHub
Black is a PEP 8 compliant opinionated formatter. Black reformats entire files in place. Style configuration options are deliberately limited and rarely added. It doesn't take previous formatting into account (see Pragmatism for exceptions).
Starred by 41.4K users
Forked by 2.7K users
Languages   Python
🌐
Real Python
realpython.com › python-pep8
How to Write Beautiful Python Code With PEP 8 – Real Python
January 12, 2025 - If you want to learn more about ... or visit pep8.org, which contains the same information but has been formatted for better readability. In these documents, you’ll find the rest of the PEP 8 guidelines that you didn’t encounter in this tutorial. Do you have any pet peeves that are different from what PEP 8 suggests, or do you fully subscribe to the style guide for Python ...
🌐
Hexmos
hexmos.com › freedevtools › c › security › autopep8
Autopep8 - Python Code Formatter for PEP 8 Compliance - security Cheatsheets | Online Free DevTools by Hexmos
# Automatically format a Python file to conform to PEP 8 autopep8 myfile.py # Format a Python file in-place (modifies the file directly) autopep8 --in-place myfile.py # Format all Python files in a directory and subdirectories recursively autopep8 ...
🌐
Formatter.org
formatter.org › python-formatter
Python Formatter Online – Beautify and Format Python Code
Beautify and format Python code online to improve readability, fix indentation, and keep formatting consistent.
🌐
Pep8
pep8.org
PEP 8: The Style Guide for Python Code
The preferred way of wrapping long lines is by using Python’s implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses.
🌐
Encode64
encode64.com › home › formatters › python formatter
Free Online Python Formatter – Black & PEP 8 Style Code Beautifier | Encode64
Clean up your Python code in one click with a Black-style formatter. This online tool enforces consistent indentation, spacing and line length so your scripts, APIs and notebooks stay readable, reviewable and PEP 8–friendly.
🌐
Medium
medium.com › @dineshsuthar03 › mastering-python-code-quality-pep-8-pylint-and-black-69b71d945e7d
Mastering Python Code Quality: PEP 8, Pylint, and Black | by Dinesh Suthar | Medium
September 25, 2023 - Commit and Push: Once satisfied with the changes, commit and push your code to your version control system. Adhering to PEP 8 guidelines and using tools like Pylint and Black can significantly improve the quality and maintainability of your ...
🌐
Decode It
decodeit.app › home › code formatters & beautifiers
Python Code Formatter - Free PEP 8 Beautifier
Format Python code with PEP 8 standards and proper indentation. Free tool for beautifying functions, classes, data science code, and modern Python 3.12 syntax.