< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039196038240039088173205162105045222218066183101016112088125

Interpreter

After installing Python, some special environment variables can be configured in order to guarantee the maximum usage of the Python environment. The following list shows some important environment variables recognized by the Python interpreter.

PYTHONPATH—  This variable contains a list of directories used by the interpreter, as search path, when importing modules. The best installation strategy is to put extra Python modules in the /lib/python$(ver)/site-packages subdirectory under the root Python installation, so you can tell between standard Python packages and add-ons. Then you just need to set PYTHONPATH to your Python search path.

PYTHONSTARTUP—  This variable contains the name of the directory that must have all its files automatically loaded at the time of starting the Python interpreter.

PATH(or path)—  This is a system environment variable that contains the directory where the Python interpreter is located.

TCL_LIBRARY, TK_LIBRARY—  These variables set the names of the directories where we can find the libraries for both the Tcl and Tk systems. You don't need to set these variables unless you move your Tcl or Tk files after building and installing them (the same as for Python).

Each system has a different way to set up these variables. For example, UNIX users running all bourne shell compatible shells, could type

					
PYTHONPATH=".:/usr/local/python/lib"
export PYTHONPATH

				

On the other hand, Windows and DOS users are familiar with the following syntax:

					
set PYTHONPATH=.;c:\ python\ lib

				

The Macintosh people must use the EditPythonPrefs program that comes along with their version of the Python distribution. Note that this application is also used to set up the values for the command line options that are passed to the interpreter.

When your system is able to locate the Python installation, you can call the interpreter by typing the command

					
python

				

invoking the interpreter without arguments, connecting the standard input to a tty device, executing commands interactively.

					
python filename

				

If you inform a filename, the interpreter tries to read and execute the contents of the file.

The next line shows the general syntax to start up the Python interpreter.

					
python [options] [-c cmd | filename | -] [file_arguments]

				

The command line options in Table 17.5 are available on Windows and UNIX systems.

Table 17.5. Python Interpreter Command Line Options
Option Description
-d Generates parser debugging information.
-i Enters interactive mode after program execution.
-O Sets optimized mode that optimizes bytecompiled files.
-OO Acts like -O, but also strips docstrings.
-S Prevents inclusion of the site initialization module.
-t Reports warnings about inconsistent tab usage.
-u Sets unbuffered binary stdout and stdin.
-v Sets the verbose mode.
-x Skips the first line of the source program.
-X Disables class-based exceptions. Note that release 2.0 doesn't contain this option anymore—it has been removed. Standard exceptions cannot be strings anymore. They always have to be classes. Also note that since release 2.0 the exceptions module was converted from Python to a built-in C module.
-c cmd Executes the provided Python command cmd. It's important to use double quotes here because the Python command can contain spaces.
- using - as a filename makes the interpreter read from the standard input.

Note

Note that Python 2.0 brings the new -U command line option to you. This option tells the Python compiler to interpret all 8-bit string literals as Unicode string literals. You should hang on to this one as the support for 8-bit strings might be abandoned in future releases.



Whenever you inform the script's filename and additional arguments to the interpreter, that information gets stored in the sys.argv variable, which is a list of strings. To be part of this list, the arguments must appear after the filename or after the -.

When commands are read from a tty, the interpreter is said to be in interactive mode. In this mode, it prompts for the next command with the primary prompt, which is by default three greater than signs (>>>); for continuation lines, it prompts with the secondary prompt, which is by default three dots (...). Note that these prompts can be modified by changing the values of sys.ps1 and sys.ps2, respectively. Users might want to modify the default values of these variables by putting these definitions in a file that can be found in a directory in the $PYTHONSTARTUP directory.

When you start the interpreter, a welcome message is printed stating its version number and a copyright notice before printing the first prompt as follows:

					
Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>>

				

If you are using Python on a UNIX system, you can extend its line-editing features by using the GNU readline library. To check whether you have this library installed on your system, just press CONTROL+P on the primary prompt. If the letter P is echoed back to you, it means that you don't have access to the library. Otherwise, you can check the documentation and use all the editing and history features that are provided by the library.

To exit the interpreter, you can type an EOF character (Control+D on UNIX, Control+Z on DOS or Windows) at the primary prompt, import the sys module and call the sys.exit() function, or just raise the SystemExit exception.

In order to launch the Python applications, you have a different kind of approach, depending on your system. The UNIX people need to adjust the shebang in the first line of the Python program to point to the Python interpreter. On Windows, you can either click on the program icon or use batch files to transport arguments to the script (or to the interpreter). Note that you can also open your files without opening the interpreter; you just need to rename them to .pyw. This extension is associated with the pythonw.exe application, which is responsible for executing the script without opening a command window for the interpreter. If you are using a Macintosh system, you need to use some special programs that come as part of the Python distribution for Macintoshes. The first one is called BuildApplet. This program takes your program and generates a file that automatically starts up the interpreter and executes the code, when opened. The other program is called BuildApplication. This one takes your program and generates a standalone application that doesn't need a Python installation running behind the scenes. This application is useful for cases in which you want to distribute your Python application to other Macs that don't have Python installed.


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

$PYTHONSTARTUP directory
-U command line option
[nd] option
[nd]c cmd option
[nd]d option
[nd]i option
[nd]O option
[nd]OO option
[nd]S option
[nd]t option
[nd]u option
[nd]v option
[nd]x option
[nd]X option
applications
      BuildApplet
      BuildApplication
      EditPythonPrefs
      launching 2nd
BuildApplet
BuildApplication
calling
      interpreters
changing
      prompts
closing
      interpreters
command line options
      -U
command-line options
      interpreters, Windows and UNIX 2nd
directories
      $PYTHONSTARTUP
editing
      prompts
EditPythonPrefs
environment variables
      recognized by interpreters 2nd
exiting
      interpreters
interactive mode
interpreters
      calling
      closing
      environment variables recognized by 2nd
      starting 2nd
launching
      applications 2nd
Macintosh
      launching Python applications
      setting up environment variables
modes
      interactive
modifying
      prompts
opening
      applications 2nd
options
     command-line
            interpreters, Windows and UNIX 2nd
PATH variable
programs
      BuildApplet
      BuildApplication
      EditPythonPrefs
      launching 2nd
prompts
      changing
PYTHONPATH variable
PYTHONSTARTUP variable
quitting
      interpreters
software
      BuildApplet
      BuildApplication
      EditPythonPrefs
      launching 2nd
starting
      applications 2nd
      interpreters 2nd
sys.argv variable
TCL_LIBRARY variable
TK_LIBRARY variable
UNIX
      command-line options, interpreters 2nd
      launching Python applications
variables
     environment
            recognized by interpreters 2nd
      PATH
      PYTHONPATH
      PYTHONSTARTUP
      sys.argv
      TCL_LIBRARY
      TK_LIBRARY
Windows
      command-line options, interpreters 2nd
      launching Python applications

© 2002, O'Reilly & Associates, Inc.