Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Script Mode

In interactive mode, Python displays the results of expressions. In script mode, however, Python doesn't automatically display results.

In order to see output from a Python script, we'll introduce the print statement. This statement takes a list of values and prints their string representation on the standard output file. The standard output is typically directed to the Terminal window. We'll revisit this in depth in The print Statement.

print "PI = ", 355.0/113.0

We can have the Python interpreter execute our script files. Application program scripts can be of any size or complexity. For the following examples, we'll create a simple, two-line script, called example1.py.

Example 3.1. example1.py

print 65, "F"
print ( 65 - 32 ) * 5 / 9, "C"

There are several ways we can start the Python interpreter and have it evaluate our script file.

  • Explicitly from the command line. In this case we'll be running Python and providing the name of the script as an argument.

  • Implicitly from the command line. In this case, we'll either use the GNU/Linux shell comment (sharp-bang marker) or we'll depend on the file association in Windows.

  • Manually from within IDLE. It's important for newbies to remember that IDLE shouldn't be part of the final delivery of a working application. However, this is a great way to start development of an application program.

Running Python scripts from the command-line applies to all operating systems. It is the core of delivering final applications. We may add an icon for launching the application, but under the hood, an application program is essentially a command-line start of the Python interpreter.

Explicit Command Line

The simplest way to execute a script is to provide the script file name as a parameter to the python interpreter. In this style, we explicitly name both the interpreter and the input script. Here's an example.

Example 3.2. Command Line Execution

python example1.py

This will provide the example1.py file to the Python interpreter for execution.

Implicit Command-Line Execution

We can streamline the command that starts our application. For POSIX-standard operating systems (GNU/Linux, UNIX and MacOS), we make the script file itself executable and directing the shell to locate the Python interpreter for us. For Windows users, we associate our script file with the python.exe interpreter. There are one or two steps to this.

  1. Associate your file with the Python interpreter. Except for Windows, you make sure the first line is the following: #!/usr/bin/env python. For Windows, you must assure that .py files are associated with python.exe and .pyw files are associated with pythonw.exe.

    The whole file will look like this:

    #!/usr/bin/env python
    print 65, "F"
    print ( 65 - 32 ) * 5 / 9, "C"
    
    
  2. For POSIX-standard operating systems, do a chmod +x example1.py to make the file example1.py executable. You only do this once, typically the first time you try to run the file. For Windows, you don't need to do this.

Now you can run a script in most GNU/Linux environments by saying:

./example1.py

Windows Setup. Windows users will need to be sure that python.exe is on their PATH. This is done with the System control panel. Click on the Advanced tab. Click on the Environment Variables… button. Click on the System variables Path line, and click the Edit… button. This will often have a long list of items, sometimes starting with “%SystemRoot%”. At the end of this list, add “;” and the direction location of Python.exe. On my machine, I put it in C:\Python23.

For Windows programmers, the windows command interpreter uses the last letters of the file name to associate a file with an interpreter. You can have Windows run the python.exe program whenever you double-click a .py file. This is done with the Folder Options control panel. The File Types tab allows you to pair a file type with a program that processes the file.

POSIX Setup. We have to be sure that the Python interpreter is in value of the PATH that our shell uses. We can't delve into the details of each of the available UNIX Shells. However, the general rule is that the person who administers your POSIX computer should have installed Python and updated the /etc/profile to make Python available to all users. If, for some reason that didn't get done, you can update your own .profile to add Python to your PATH variable.

Another Script Example

Throughout the rest of this book, we're going to use this script processing mode as the standard way to run Python programs. Many of the examples will be shown as though a file was sent to the interpreter.

For debugging and testing, it is sometimes useful to import the program definitions, and do some manipulations interactively. We'll touch on this in the section called “Hacking Mode”.

Here's a second example. We'll create a new file and write another small Python program. We'll call it example2.py.

Example 3.3. example2.py

#!/usr/bin/env python
"""Compute the odds of spinning red (or black) six times in a row
on an American roulette wheel. """
print (18.0/38.0)**6

This is a one-line Python program with a two line module document string. That's a good ratio to strive for.

After we finish editing, we mark this as executable using chmod +x example2.py. Since this is a property of the file, this is remains true no matter how many times we edit, copy or rename the file.

When we run this, we see the following.

$ 

./example2.py

0.0112962280375

Which says that spinning six reds in a row is about a one in eighty-nine probability.


 
 
  Published under the terms of the Open Publication License Design by Interspire