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
» pip install pep8
Videos
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.
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.
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!