Linux/Unix > Python Essential Reference, Second Edition > 1. A Tutorial Introduction > Running Python | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238110137110163131200150148053063

### Running Python

Python programs are executed by an interpreter. On UNIX machines, the interpreter is started by typing `python`. On Windows and the Macintosh, the interpreter is launched as an application (either from the Start menu or by double-clicking the interpreter’s icon). When the interpreter starts, a prompt appears at which you can start typing programs into a simple read-evaluation loop. For example, in the following output, the interpreter displays its copyright message and presents the user with the `>>>` prompt, at which the user types the familiar “Hello World” command:

    Python 2.1 (#1, Apr 20 2001, 14:34:45)
    [GCC 2.95.2 19991024 (release)] on sunos5
    Type "copyright", "credits" or "license" for more information.
    >>> print "Hello World"
    Hello World
    >>>

Programs can also be placed in a file such as the following:

    # helloworld.py
    print "Hello World"

Python source files have a `.py` suffix. The `#` character denotes a comment that extends to the end of the line.

To execute the `helloworld.py` file, you provide the filename to the interpreter as follows:

    % python helloworld.py
    Hello World
    %

On Windows, Python programs can be started by double-clicking a .py file. This launches the interpreter and runs the program in a console window. In this case, the console window disappears immediately after the program completes its execution (often before you can read its output). To prevent this problem, you should use an integrated development environment such as Idle or Pythonwin. An alternative approach is to launch the program using a `.bat` file containing a statement such as `python -i helloworld.py` that instructs the interpreter to enter interactive mode after program execution.

On the Macintosh, programs can be executed from the included integrated development environment. In addition, the `BuildApplet` utility (included in the distribution) turns a Python program into a document that automatically launches the interpreter when opened.

Within the interpreter, the `execfile()` function runs a program, as in the following example:

    >>> execfile("helloworld.py")
    Hello World

On UNIX, you can also invoke Python using `#!` in a shell script:

    #!/usr/local/bin/python
    print "Hello World"

The interpreter runs until it reaches the end of the input file. If running interactively, you can exit by typing the EOF (end of file) character or by selecting Exit from a pull-down menu. On UNIX, EOF is Ctrl+D; on Windows, Ctrl+Z. A program can also exit by calling the `sys.exit()` function or raising the `SystemExit` exception. For example:

    >>> import sys
    >>> sys.exit()

or

    >>> raise SystemExit

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.