The term for this is "linting". A Python module called Pylint is available.

It checks for coding standards and errors with full customizability. It can be run from the command line, as part of a continuous integration workflow, integrated into various IDEs.

On Debian and many derivatives, it can be installed with apt install pylint. Use the module like this:

python3 -m pylint your_file.py
Answer from Soviut on Stack Overflow
🌐
PyPI
pypi.org › project › pep8
pep8 · PyPI
Plugin architecture: Adding new checks is easy. Parseable output: Jump to error location in your editor. Small: Just one Python file, requires only stdlib. You can use just the pep8.py file for this purpose.
      » pip install pep8
    
Published   Oct 24, 2017
Version   1.7.1
🌐
Vim
vim.org › scripts › script.php
pep8 - Check your python source files with PEP8 : vim online
created by Davide Lo Re script type utility description You need to install this tool: http://github.com/cburroughs/pep8.py It's a simple program that just checks if your python code is pep-8 compliant. Pressing F5 will run it using the "quickfix" feature. This way you can "navigate" through ...
🌐
GitHub
github.com › PyCQA › pycodestyle
GitHub - PyCQA/pycodestyle: Simple Python style checker in one Python file · GitHub
$ pycodestyle --show-source --show-pep8 testing/data/E40.py testing/data/E40.py:2:10: E401 multiple imports on one line import os, sys ^ Imports should usually be on separate lines.
Starred by 5.2K users
Forked by 752 users
Languages   Python 99.9% | Makefile 0.1%
🌐
Pep8
pep8.org
PEP 8: The Style Guide for Python Code
Users who don’t want to use type checkers are free to ignore them. However, it is expected that users of third party library packages may want to run type checkers over those packages. For this purpose PEP 484 recommends the use of stub files: .pyi files that are read by the type checker ...
🌐
Codewof
codewof.co.nz › style › python3
Python 3 Style Checker for beginners - codeWOF
This style checker will check your code against the main conventions recommended for Python in PEP 8 (Style Guide for Python Code) and PEP 257 (Docstring Conventions). Fine tuning your code to pass these common conventions makes it easy for others to read your Python code · Privacy notice: ...
🌐
Pythonchecker
pythonchecker.com
pythonchecker.com
We cannot provide a description for this page right now
Find elsewhere
🌐
OnWorks
onworks.net › home › programs › pep8 online
pep8
This is the command pep8 that can ... · Run in Ubuntu Run in Fedora Run in Windows Sim Run in MACOS Sim · pep8 - A tool to check your Python code against some of the style conventions in PEP 8....
🌐
Pep8online
pep8online.com
PEP8 Online
We cannot provide a description for this page right now
🌐
GitHub
github.com › Bryukh › pep8online
GitHub - Bryukh/pep8online: Web service for pep8 check
This is web service for pep8 requirements checking.
Starred by 60 users
Forked by 11 users
Languages   JavaScript 97.3% | CSS 2.5% | Python 0.2% | JavaScript 97.3% | CSS 2.5% | Python 0.2%
🌐
Finxter
blog.finxter.com › home › learn python blog › what’s the best pep8 python style checker?
What's the Best PEP8 Python Style Checker? - Be on the Right Side of Change
May 8, 2020 - These will be presented in this article: PythonChecker is the most powerful and interactive Python code style checker on the web. Your code gets a style rank percentage that indicates how good your code is according to the PEP8 style guide.
🌐
Reddit
reddit.com › r/learnpython › tool for checking if your code is pythonic/confirms to pep8. must check.
r/learnpython on Reddit: Tool for checking if your code is Pythonic/confirms to PEP8. Must check.
November 4, 2015 -

Many of you are probably aware of PEP8 which is style guide for coding in Python. But most of you might be unaware that there is a tool called PEP8 which can be installed via pip as-

pip install pep8

After installing it, you can check whether your code confirms to PEP8 by running this command-

pep8 test.py

Here test.py is your program. And it will tell you how you can make your code look great by pointing out mistakes line by line. (Source: Python Guide by Kenneth Reitz). Awesome.

Edit: On a side note, I recommend reading Python Guide by Kenneth Reitz. It tells a lot of things about how one should write an elegant/readable program.

🌐
GitHub
github.com › treyhunner › pep8
GitHub - treyhunner/pep8: Simple Python style checker in one Python file · GitHub
Options: --version show program's version number and exit -h, --help show this help message and exit -v, --verbose print status messages, or debug with -vv -q, --quiet report only file names, or nothing with -qq -r, --repeat show all occurrences of the same error --exclude=patterns exclude files or directories which match these comma separated patterns (default: .svn,CVS,.bzr,.hg,.git) --filename=patterns when parsing directories, only check filenames matching these comma separated patterns (default: *.py) --select=errors select errors and warnings (e.g. E,W6) --ignore=errors skip errors and w
Starred by 47 users
Forked by 24 users
Languages   Python
🌐
Ntpsec
lists.ntpsec.org › pipermail › devel › 2018-August › 006501.html
Python PEP8 online checker
Gack, too much work. get a local one: # pip install pycodestyle The to check: # pycodestyle ntpq.py > Clockmaker is littered with (~80) errors. Yup. gpsd is PEP8 clean, but NTPsec has a way to go. > Is using an online checker recommend ? Not by me. Looks like a PITA.
🌐
GitHub
github.com › vyahello › pep8-checker
GitHub - vyahello/pep8-checker: ✅ PEP8 web linter (pycodestyle, bottle, aws lambda, docker, heroku)
This project allows to check your python code complies with pep8 conventions.
Starred by 3 users
Forked by 3 users
Languages   Python 44.5% | Shell 34.4% | Smarty 15.9% | Dockerfile 5.0% | Procfile 0.2% | Python 44.5% | Shell 34.4% | Smarty 15.9% | Dockerfile 5.0% | Procfile 0.2%
🌐
GitHub
github.com › vrana › pep8
GitHub - vrana/pep8: Simple Python style checker in one Python file · GitHub
Simple Python style checker in one Python file. Contribute to vrana/pep8 development by creating an account on GitHub.
Author   vrana
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!

🌐
Readthedocs
pep8.readthedocs.io
pep8’s documentation — pep8 1.7.1 documentation
pep8 is a tool to check your Python code against some of the style conventions in PEP 8. Contents: Introduction · Features · Disclaimer · Installation · Example usage and output · Configuration · Error codes · Related tools · Advanced usage · Automated tests · Skip file header · ...
🌐
Ubuntu
manpages.ubuntu.com › manpages › jammy › man1 › pep8.1.html
Ubuntu Manpage: pep8 - A tool to check your Python code against some of the style conventions in PEP 8.
path user config file location (default: $HOME/.config/pep8) USAGE EXAMPLES Display how often each error was found: % pep8 --statistics -qq example/lib/ Show source code and more verbose explanation from PEP 8: % pep8 --show-source --show-pep8 foo.py
🌐
Readthedocs
pep8.readthedocs.io › en › release-1.7.x › advanced.html
Advanced usage — pep8 1.7.1 documentation
import unittest import pep8 class TestCodeFormat(unittest.TestCase): def test_pep8_conformance(self): """Test that we conform to PEP8.""" pep8style = pep8.StyleGuide(quiet=True) result = pep8style.check_files(['file1.py', 'file2.py']) self.assertEqual(result.total_errors, 0, "Found code style errors (and warnings).")