Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
howtos:check_python_code [2012/03/29 20:57] – add howto to check code with pylint and friends enricohowtos:check_python_code [2018/08/07 15:51] (current) – add new way of using pylint craynic
Line 1: Line 1:
-====== Check your Python code with pep8 and pylint ======+====== Checking Python Code Style and Syntax ======
  
-The following steps enable you to check your Python code with code checkers like +The following steps enable you to check your Python code for syntax errorscoding style and standards ([[http://www.python.org/dev/peps/pep-0008/|PEP 8]])
-[[http://pypi.python.org/pypi/pylint|Pylint]], [[http://pypi.python.org/pypi/pyflakes|Pyflakes]] +
-and [[http://pypi.python.org/pypi/pep8|Pep8]] for syntax errors, coding style and standards.+
  
  
-===== Helper script =====+===== 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 You need a little helper script to combine the above (and maybe other) tools
Line 15: Line 18:
 #!/bin/sh #!/bin/sh
  
- +echo "======  pycodestyle  ======" 
-pep8 $1+pycodestyle $1 
 +echo "======  pyflakes  ======"
 pyflakes $1 pyflakes $1
-pylint --output-format=parseable $1+echo "======  pylint  ======" 
 +pylint --msg-template="{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}" --reports=n $1 
 +pylint -f parseable -r n $1
 </file> </file>
  
Line 31: Line 37:
  
  
-===== Setup Geany =====+ 
 +==== Setup Geany ====
  
 {{ :howtos:images:check_python_code.png?direct&300|}} {{ :howtos:images:check_python_code.png?direct&300|}}
  
-Open a Python file in Geany or simply create a new file and set the filetype to +  - Open a Python file in Geany or simply create a new file and set the filetype to Python. 
-Python. Then open the "Set Build Commandsdialog in the Build menu. +  - Open the //Set Build Commands// dialog in the Build menu. 
-Set for second build command a label, e.g. "Check" and the following command:+  - Setup the custom command 
 +    * Set a label, e.g. "Check" 
 +    * Set the Command field to <code> 
 +check_python_code %f 
 +</code> 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`  
 +  - Paste the following text in the "Error regular expression" field. <code> 
 +([^:]+):([0-9]+):([0-9:]+)? .* 
 +</code> This is used by Geany to parse the output of build commands for errors to be highlighted in the editor.
  
-<code>check_python_code %f</code> 
  
-This assumes that you saved the above mentioned helper script into a path +==== Using pylint on Windows ====
-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" +Gösta Ljungdahl mentioned, the following little batch script is useful to use pylint on Windows with Geany:
-field. This regular expression is used by Geany to parse the output of build +
-commands for errors to be highlighted in the editor.+
  
-<code>([^:]+):([0-9]+):([0-9:]+)? .*</code>+<code dos 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 
 +</code>
  
-===== Usage =====+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 ===== 
 + 
 +[[https://pypi.python.org/pypi/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 ==== 
 + 
 +  - Install the necessary tools <code bash> 
 +pip install flake8 pep8-naming 
 +</code> 
 +  - Configure Geany as described in the [[howtos:check_python_code#setup_geany|previous section]], the only difference being the command to execute (use the same regular expression) <code> 
 +flake8 --show-source "%f"</code> 
 + 
 + 
 +===== Usage in Geany =====
  
 After saving the helper script and setting up Geany's build commands, you are 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+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 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+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. 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 You can see the errors for review, click on them to jump to the corresponding
Line 66: Line 101:
  
  
-{{tag>howto python pep8 pyflakes python}}+{{tag>howto python pep8 pycodestyle pyflakes python flake8}}
  
Print/export