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
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
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
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
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
The simplest way to execute a script is to provide the script file
name as a parameter to the
this style, we explicitly name both the interpreter and the input
script. Here's an example.
Example 3.2. Command Line
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.
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
files are associated with
The whole file will look like this:
print 65, "F"
print ( 65 - 32 ) * 5 / 9, "C"
- For POSIX-standard operating systems, do a
to make the file
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
Windows Setup. Windows users will need to be sure that
python.exe is on their
is done with the System control panel. Click on the
tab. Click on the
button. Click on the
Path line, and click the
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
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
whenever you double-click a
.py file. This is done
control panel. The
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
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
.profile to add Python to your
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
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
Example 3.3. example2.py
"""Compute the odds of spinning red (or black) six times in a row
on an American roulette wheel. """
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
When we run this, we see the following.
Which says that spinning six reds in a row is about a one in