pep8 is a tool that can check whether your code conform to the PEP 8 style guide. A style guide is there to make your code easier to read by yourself and other developers.

Pylint is a source code analyzer that can find common programming errors and code smells. For example, pylint can find function redefinitions, unused parameters, undefined symbols, bad imports etc. There are newer tools in this catalog, but pylint is what I use. When using pylint, I usually run pylint -E , which only list warnings and errors. Without using -E option, the output will be too long to digest. If you do read the full output, you might be surprised to see how much pylint can understand your code, especially on a medium or large size project. Another reason for using -E is that it runs much faster, since it does less analysis.

pep8 and pylint are configurable, not all checks are good for all projects. I myself disable some checks by default. Some pylint checks can be disabled at line level, function level or module level. Only disable a check globally when you think it brings more trouble than benefit.

Here is a Makefile that integrate virtualenv, pep8, pylint and pytest:

PYTHON_MODULES := mypkg PYTHONPATH := . VENV := .venv PYTEST := env PYTHONPATH=$( PYTHONPATH ) PYTEST=1 $( VENV )/bin/py.test PYLINT := env PYTHONPATH=$( PYTHONPATH ) $( VENV )/bin/pylint --disable=I0011 --msg-template= "{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}" PEP8 := env PYTHONPATH=$( PYTHONPATH ) $( VENV )/bin/pep8 --repeat --ignore=E202,E501,E402 PYTHON := env PYTHONPATH=$( PYTHONPATH ) $( VENV )/bin/python PIP := $( VENV )/bin/pip DEFAULT_PYTHON := /usr/bin/python3 VIRTUALENV := /usr/local/bin/virtualenv REQUIREMENTS := -r requirements.txt default : check-coding-style venv : test -d $( VENV ) || $( VIRTUALENV ) -p $( DEFAULT_PYTHON ) -q $( VENV ) requirements : @ if [ -d wheelhouse ]; then \ $( PIP ) install -q --no-index --find-links=wheelhouse $( REQUIREMENTS ); \ else \ $( PIP ) install -q $( REQUIREMENTS ); \ fi bootstrap : venv requirements check-coding-style : bootstrap $( PEP8 ) $( PYTHON_MODULES ) $( PYLINT ) -E $( PYTHON_MODULES ) pylint-full : check-coding-style $( PYLINT ) $( PYTHON_MODULES ) test : check-coding-style $( PYTEST ) $( PYTHON_MODULES ) check : $( PYTEST ) $( PYTHON_MODULES ) .PHONY : default venv requirements bootstrap check-coding-style pylint-full test check

To run all of pep8/pylint/pytest, just type "make test". It will create virtualenv in .venv directory, install dependencies, and run the checkers. To only run pytest, type "make check". You may want to run pytests only when you have a large project, where pylint runs can significantly slow down your development-test cycle.

This makefile is a trimmed version that only focus on tools mentioned in this section to make it easier to understand. You may also read the full version. That version has more make targets and some target has dependencies on external scripts that I may write posts about later on. You need to adapt it yourself.

If you use git, you can config a pre-commit hook that automatically runs "make test" for you when you run "git commit". If any tests fail, the commit will abort. To do that, create a file .git/hooks/pre-commit with the following content:

# !/bin/ sh make test

Then make it executable by

chmod +x .git/hooks/pre-commit

You can also config post-commit and post-merge hook that triggers a Jenkins CI build. But this post is already too long. So I won't talk about the details.