Last Updated: February 25, 2016
·
844
· breard-r

Checking the pep8 compliance of your Django project

PEP 8 defines a coding style for Python that you may want to follow. The pep8 package allows you to check each file, but isn't automated. Plus, you may not want to check for auto-generated python files like migrations. A simple way to use the pep8 checker in your Django project is the following command which checks every python file, excepted migrations:

find . -wholename "./*/migrations" -prune -o -name "*\.py" -exec pep8 {} \;

You can automate this check by using a pre-commit git hook (GitHub Gist):

#!/bin/sh

final_exit=0
for pyfile in $(find . -wholename "./*/migrations" -prune -o -name "*\.py" -print); do
    pep8 "$pyfile"
        if (($? > 0)); then
        final_exit=1
    fi
done

if (($final_exit > 0)); then
    echo "PEP8 compliance test failed, aborting commit."
fi
exit $final_exit