Python standalone program tutorial

This tutorial illustrates how to make a standalone python program, or program script, that can also be used like a linux command or as a python module. First, basic file formats used in the python-verse are outlied, then the develeopment of a python script with v4 command line pasrsing is presetned. Then, the modification of this program for use as a linux commadn and as a python module is presented.

A. The Python-Verse

Conventional python programs and modules are contained in text files with a .py extension. The following popular variations to this with their assoicated file extensions are given below:

Container File Extension Description
Python .py convetnional python program or script
Ipython .ipy interactive python program. The Ipython interpreter is useful for develoing python programs in a terminal environment. Ipython supports the command extension terminal execution "!" and magic commands with the prefix "%" that are useful for interactive program development.
Jupyter Notebook .ipynb jupyter Ipython workspace, may contain Ipython program cells, makdown cells, variable values and a progam execution state
Liunx command (optional) A conventional python program (.py) whith two modifications: (a) a first line python comment to inform Linux that the program is a python script and that it is to be executed with the python interpreter and (b) an execution file setting that informs Linux that the file is a script that can be executed. Linux command execution ignores file name extensions

Note, standalone programs that are executed by the python interpreter such as conventional Python programs or Linux commands, should not contain the command extensions of Ipython that are also available in Jupyter Notebooks.

B. Python Standalone Program

Python programs can be written as independent commands (like other basic Linux commands). These progams do not require an interactive environemnt (like jupyter notebook). In this tutorial, a simple application (vcorner) is used to illustrate the development of a standalone program. The program is discussed with the following sections:

  1. Title and pakackage imports
  2. Command line argument parsing and command documentation
  3. Checks on the correct comand argument specification and reading files
  4. Executiing the program function on the input files, and writing the output image file

Following the program description the convertion to a Linux/macOS command is descibed and the full progam is shown

1. Title and package imports

All python progarm should start with a program description. For packages, this program only requires the v4.vx module and numpy.

2. Command line argument parsing and command documentation

Python provides command line argument values in the order specified in a list called sys.argv. Any standalone program should provide a convenient mechanism for argument specification such as python argparse or v4 vaparse. Both of these provide a mechanism for documenting the parameters that the program accepts.


At this point, minimal program documentation has been achieved. The help option, "-" in this case, will provide users with information on how to use the program. For example:

  $ python vcorner.py -
vcorner: copy lower left corner
if= input file
of= output file
s= size of result (default 10)

3. Checks on the correct comand argument specification and reading files

4. Executing the program function on the input file, and writing the output file

Note, the two 'for loop' method presented above is for individual pixel exposition. Python programmers would, in general, choose to use a vector or array operation that may be more efficieint, such as:

tmimage.i = np.copy(im[yoffset:,:tm.shape[1]])


C. Conversion to a Linux/macOS command

The usual way to excute a python program is to run it with python, for example:

python vcorner.py if=infile of=outfile

If we convert the program to a linux command then the command would be:

vcorner if=infile of=outfile

Conversion is accoplished by changing the file name (optional), adding a comment line to the begining of the program, and changing the program file persmission setting. In detail, these three steps are:

  1. Remove .py from the program name, for example with the Linux mv command:

    $ mv corner.py vcorner

  2. Add the following comment to the very first line of the program

    #!/usr/bin/env python

  3. Modify the file permisions of the program file so that the system recognises it as a file that can be executed as a program with the Linux chmod command:

    $ chmod +x vcorner

The first step, to change the file name to the style of linux commands, is optional. Once steps 2 and 3 have been taken the program can be givn any name with any extension, including .py, and it will still behave as a command.

Example and program testing


The complete program with the Linux command capability is given below

D. Conversion to a python module

The current program contains two primary componnets (a) a program fucntion that performs computation on image data and (b) a command line interface that parses the command line paramters, reads the input data, and writes the results. It may be useful to make the computation functions available to other python programs as a "library". The standard python module organiztion may be used to achieve this.

First we need to partition the program into the two primary components and then only execute the command line interface if the prorgam is not being loaded as a module. Teh interface should only be executed when the program is being used as a python script or a linux command. The overall program structure to provide script, linux command, and module use options is as follows:

#!/usr/bin/env python
# first line is required  for use as a Linux command

def main():
    """ The command line interface goes here """
    . . .
def corner ():
    """ The program processing function goes here """
    . . .
# Last two lines are neeeded to prevent command line
# "main()" execution when used as a python module
if __name__ == "__main__":
    main()

For our simple demonstration program, the action function may be rewrtiten as a simple function "corner()" that only involves np arrays as shown below:

Testing use as a module

An example of using this modified progarm as a python module is shown below:

Final program with script, Linux command, and python module capabilites