Creating an executable script file

To use an executable script file, we have a two-step implementation: make it executable and include a #! (sharp-bang, or shebang) line. We will take a look at the details.

The Linux command to mark a script file as executable looks like the following example:

chmod +x some_script.py

The shebang line will often look like this example:

#!/usr/bin/env python3

This line will direct the OS to use the named program to execute the script file. In this case, we used the /usr/bin/env program to locate the python3 program to run the script. The python3 program will be given the script file as its input.

When a script file is marked executable, and the file includes the #! line, we can use some_script.py at the command line to run the script.

For a more complex application, this top-level script may import other modules and packages. It's important that these top-level executable script files should be as simple as possible to promote reuse of the various components. Key design principles include the following:

  • Keep the script module as small as possible. Any complexity should be in the modules which are imported. 
  • A script module should have no new or distinctive code. It should emphasize importing and using code from other modules.
  • In the long run, no program will ever stand alone. Any valuable software will be extended and repurposed. Even the top-level script for an application may be integrated into an even larger wrapper.

Our design goals must always include the idea of composite, larger-scale programming. A main script file should be as short as possible. Here's our example:

import simulation

if __name__ == "__main__":
with simulation.Setup_Logging(): with simulation.Build_Config() as config: main = simulation.Simulate_Command() main.configure(config) main.run()

All of the relevant working code is imported from a module named simulation. There's no unique or distinctive new code introduced in this module.

In the next section, we'll see how to create a _main_ module .

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset