I know there are tools which validate whether your Python code is compliant with PEP8, for example there is both an online service and a python module.
However, I cannot find a service or module which can convert my Python file to a self-contained, PEP8 valid Python file. Does anyone know if there are any?
I assume it's feasible since PEP8 is all about the appearance of the code, right?
If you're using eclipse + PyDev you can simply activate autopep8 from PyDev's settings: Windows -> Preferences -> type 'autopep8' in the search filter.
Check the 'use autopep8.py for code formatting?' -> OK
Now eclipse's CTRL-SHIFT-F code formatting should format your code using autopep8 :)
Unfortunately "pep8 storming" (the entire project) has several negative side-effects:
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:
Suppose you've done your work off of
master
and are ready to commit:Or to clean the new lines you've commited since the last commit:
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!
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:
Apply this to a specific file:
or to your project (recursively), the verbose option gives you some feedback of how it's going:
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):
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/pandasTrue == np.bool_(True)
but notTrue is np.bool_(True)
!)You can check how many violations of each type (before and after):
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.
@Andy Hayden gave good overview of autopep8. In addition to that there is one more package called pep8ify which also does the same thing.
However both packages can remove only lint errors but they cannot format code.
Above code remains same after pep8ifying also. But the code doesn't look good yet. You can use formatters like yapf, which will format the code even if the code is PEP8 compliant. Above code will be formatted to
Some times this even destroys Your manual formatting. For example
will be converted to
But You can tell it to ignore some parts.
Taken from my old blog post: Automatically PEP8 & Format Your Python Code!
There is many.
IDEs usually have some formatting capability built in. IntelliJ Idea / PyCharm does, same goes for Python plugin for Eclipse, and so on.
There are formatters/linters that can target multiple languages. https://coala.io is a good example of those.
Then there are the single purpose tools, of which many are mentioned in other answers.