This is an old revision of the document!


Check your Python code with pep8 and pylint

The following steps enable you to check your Python code with code checkers like Pylint, Pyflakes and Pep8 for syntax errors, coding style and standards.

Helper script

You need a little helper script to combine the above (and maybe other) tools to be run at once. Save this script somewhere into your $PATH or at some other location of your choice and make it executable.

check_python_code
#!/bin/sh
 
echo "======  pep8  ======"
pep8 $1
echo "======  pyflakes  ======"
pyflakes $1
echo "======  pylint  ======"
pylint --output-format=parseable $1

You can also set environment variables like PYTHONPATH to extend or reduce the path for Python to look for modules in this script, e.g. by simply exporting the variable:

export PYTHONPATH="$PYTHONPATH:/home/username/.pylint.d"

Setup Geany

Open a Python file in Geany or simply create a new file and set the filetype to Python. Then open the “Set Build Commands” dialog in the Build menu. Set for second build command a label, e.g. “Check” and the following command:

check_python_code %f

This assumes that you saved the above mentioned helper script into a path in your $PATH environment variable. If not, just specify the full path to the script. %f is a placeholder replaced by Geany on execution with the filename of the current file in the editor.

Then use the following regular expression for the “Error regular expression” field. This regular expression is used by Geany to parse the output of build commands for errors to be highlighted in the editor.

([^:]+):([0-9]+):([0-9:]+)? .*

Usage

After saving the helper script and setting up Geany's build commands, you are ready to use this by simply pressing F9 or using the menu Build → Check to trigger the execution of the helper script and so let the tools check your code. All errors and warnings reported by the tools are sent to the Compiler tab in the messages windows at the bottom of the Geany window. You can see the errors for review, click on them to jump to the corresponding code line and Geany will also mark the reported code lines directly in the editor with red squiggly underlines.

Ideally, you won't see much errors in your code :). Good luck.

Print/export