Sunday, November 06, 2022

Configure Geany for Python Programming

I use the GEANY text editor/IDE when I need a quick light weight versatile text editor.  I also use it for writing programming code (Python/PHP/HTML). It's available for Linux (including the Raspberry Pi), Windows, and MacOS. It has too many features to list but it allows plugins and helper apps to run or be launched from inside Geany but you have to install the Apps and configure GEANY to use them.  Since I've been writing Python code lately let's configure GEANY to help us out while coding. I'm running Ubuntu 22.04 LTS with Python 3.10.6 installed (also QT5)


Prerequisite:  You need to have Python 3.x installed and working on your system


Let's install GEANY by running the below command in a Terminal Window;
sudo apt install geany

Once GEANY is installed, let's get it's helper Apps installed by typing the following four (4) commands into a Terminal Window.  Let each command finish before executing the next;

sudo apt install pycodestyle
pycodestyle checks code formatting and style

sudo apt install python3-pyflakes pyflakes3
pyflakes3 checks dependencies and import statements

sudo apt install pylint
Pylint analyses your code without actually running it. It checks for errors, enforces a coding standard, looks for code smells, and can make suggestions about how the code could be refactored.

sudo apt install pep8
This document gives coding conventions for the Python code comprising the standard library in the main Python distribution. 

GEANY only has three configurable menu item (under the BUILD menu)  but you can link those three menu items to bash scripts in order to execute multiple helper Apps.  To do this we need to create a bash file (.sh) with the commands we want the helper Apps to run on our code.

To do this we can use GEANY (lol).  Open Geany and create a NEW file from the FILE menu (or press CTRL n).  Once you have a new blank file open, copy and paste the following bash script into that file and SAVE the file in your HOME directory with the file name CheckPythonCode.sh

#!/bin/bash

# This Bash Script is used by GEANY to check your code for errors
echo "======  pycodestyle  ======"
pycodestyle $1
echo "======  pyflakes3  ======"
pyflakes3 $1
echo "======  pylint  ======"
pylint --msg-template="{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}" --reports=n $1
pylint -f parseable -r n $1


Now that you've saved the your new bash script file as CheckPythonCode.sh in your HOME directory we can edit the menu items in Geany to do what we want them to do.  Close your CheckPythonCode.sh bash script and create another NEW File (CTRL n).  We are going to make a Python hello_world.py program and we are going to add a few comment lines to the beginning to tell our helper Apps to ignore a few error types.  Here is the hello_world.py code, we need to copy and paste the following into our NEW Blank file in Geany and save it wherever you keep your python code file or just in your HOME directory;

# pylint: disable=missing-module-docstring
# pylint: disable=missing-class-docstring
# pylint: disable=missing-function-docstring

# This program prints Hello, world!
print('Hello, world!')

NOTE:  The added comment lines tell our pylint helper App to ignore the errors missing-module-docstring, missing-class-docstring, and missing-function-docstring, which are just comments describing modules, classes, and/or functions.


Now with our hello_world.py file open in Geany go to the BUILD menu as pictured.


Once in the BUILD Menu, select the SET BUILD COMMANDS option, which will bring up the following popup screen;


We need to make the following changes;  
NOTE: clicking on a button LABEL will allow you to change that LABEL.

Button number 1 (LABEL - Compile) should have the following text in the COMMAND section;
python3 -m py_compile "%f"

Button number 2 (LABEL - Check) should have the following text in the COMMAND section;
~/CheckPythonCode.sh "%f"

Button number 3 (LABEL - Lint) should have the following text in the COMMAND section;
pep8 --max-line-length=80 "%f"

LABEL - Execute, should have the following text in the COMMAND section;
python3 "%f"

If your Buttons don't look like the picture or close to that, you have to make sure you have a python file open in Geany while editing these COMMANDs

You can now Compile, check your Formatting/Style and check your Dependencies/Import Statements for whatever python file you have open and active in Geany.  You can also launch and execute your code that you have open in Geany.