Table of Contents

Checking Python Code Style and Syntax

The following steps enable you to check your Python code for syntax errors, coding style and standards (PEP 8)

Using pycodestyle and pylint

The following steps enable you to check your code with Pylint, Pyflakes and Pycodestyle (formerly known as pep8).

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 "======  pycodestyle  ======"
pycodestyle $1
echo "======  pyflakes  ======"
pyflakes $1
echo "======  pylint  ======"
pylint --msg-template="{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}" --reports=n $1
pylint -f parseable -r n $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

  1. Open a Python file in Geany or simply create a new file and set the filetype to Python.
  2. Open the Set Build Commands dialog in the Build menu.
  3. Setup the custom command
    • Set a label, e.g. “Check”
    • Set the Command field to
      check_python_code %f

      Note: 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. OR You can also use each command i.e. `pep8`, `pyflakes` or `pylint` directly in place of `check_python_code`

  4. Paste the following text in the “Error regular expression” field.
    ([^:]+):([0-9]+):([0-9:]+)? .*

    This is used by Geany to parse the output of build commands for errors to be highlighted in the editor.

Using pylint on Windows

Gösta Ljungdahl mentioned, the following little batch script is useful to use pylint on Windows with Geany:

pylint.bat
@echo off
set configfile=c:\cygwin64\home\gostal\.pylintrc
echo "======  pylint  ======"
c:\Anaconda\envs\python3\Scripts\pylint.exe --rcfile=%configfile% --msg-template="{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}" %1

Of course, you need to tweak the paths to the config file and pylint.exe. The rest should be similar to the explainations above.

Using flake8

Flake8 is a wrapper around several tools (PyFlakes, pep8 and Ned Batchelder's McCabe script), which also allows integration with plugins.

Using it offers a convenient and simple alternative to using the Helper Script approach described earlier in this document.

Setup

  1. Install the necessary tools
    pip install flake8 pep8-naming
  2. Configure Geany as described in the previous section, the only difference being the command to execute (use the same regular expression)
    flake8 --show-source "%f"

Usage in Geany

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.