Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
howtos:check_python_code [2012/03/29 20:57] – add howto to check code with pylint and friends enrico | howtos:check_python_code [2018/08/07 15:51] (current) – add new way of using pylint craynic | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | 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 errors, coding style and standards ([[http://www.python.org/ |
- | [[http:// | + | |
- | and [[http://pypi.python.org/ | + | |
- | ===== 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 " | |
- | pep8 $1 | + | pycodestyle |
+ | echo " | ||
pyflakes $1 | pyflakes $1 | ||
- | pylint --output-format=parseable $1 | + | echo " |
+ | pylint --msg-template=" | ||
+ | pylint -f parseable | ||
</ | </ | ||
Line 31: | Line 37: | ||
- | ===== Setup Geany ===== | + | |
+ | ==== Setup Geany ==== | ||
{{ : | {{ : | ||
- | 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. | + | - Open the //Set Build Commands// dialog in the Build menu. |
- | Set for second build command a label, e.g. " | + | - Setup the custom |
+ | * Set a label, e.g. " | ||
+ | * Set the Command field to < | ||
+ | check_python_code %f | ||
+ | </ | ||
+ | - Paste the following text in the "Error regular expression" | ||
+ | ([^:]+): | ||
+ | </ | ||
- | < | ||
- | This assumes that you saved the above mentioned helper script into a path | + | ==== Using pylint |
- | 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 | + | Gösta Ljungdahl mentioned, |
- | field. This regular expression | + | |
- | commands for errors to be highlighted in the editor. | + | |
- | < | + | < |
+ | @echo off | ||
+ | set configfile=c:\cygwin64\home\gostal\.pylintrc | ||
+ | echo " | ||
+ | c:\Anaconda\envs\python3\Scripts\pylint.exe | ||
+ | </ | ||
- | ===== 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:// | ||
+ | |||
+ | 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 | ||
+ | </ | ||
+ | - Configure Geany as described in the [[howtos: | ||
+ | flake8 --show-source " | ||
+ | |||
+ | |||
+ | ===== Usage in Geany ===== | ||
After saving the helper script and setting up Geany' | After saving the helper script and setting up Geany' | ||
- | ready to use this by simply pressing F9 or using the menu Build -> Check to | + | ready to use this by simply |
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> | + | {{tag> |