Linux/Unix > Python Essential Reference, Second Edition > 10. Execution Environment > Interpreter Options and Environment | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238111161105085094121179040058246

### Interpreter Options and Environment

The interpreter has a number of options that control its runtime behavior and environment. On UNIX and Windows, options are given to the interpreter in the form of command-line options such as the following:

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

On the Macintosh, options to the Python interpreter are set using a separate program, `EditPythonPrefs`.

The following command-line options are available:

Option | Description
--- | ---
`-d` | Generates parser debugging information.
`-h` | Prints a list of all available command-line options.
`-i` | Enters interactive mode after program execution.
`-O` | Optimized mode.
`-OO` | Optimized mode plus removal of documentation strings.
`-S` | Prevents inclusion of the `site` initialization module.
`-t` | Reports warnings about inconsistent tab usage.
`-tt` | Inconsistent tab usage results in a `TabError` exception.
`-u` | Unbuffered binary `stdout` and `stdin`.
`-U` | Unicode literals. All string literals are handled as Unicode.
`-v` | Verbose mode.
`-V` | Prints the version number and exits.
`-x` | Skips the first line of the source program.
`-c` `cmd` | Executes `cmd` as a string.
`-``Wfilter` | Adds a warning filter.

The `-d` option debugs the interpreter and is of limited use to most programmers. The `-i option` starts an interactive session immediately after a program has finished execution, and is useful for debugging. The `-O` and `-OO` options apply some optimization to byte-compiled files, and are described in Chapter 8, “Modules and Packages.” The `-S` option omits the site initialization module described in the later section “Site Configuration Files.” The `-t`, `-tt`, and `-v` options report additional warnings and debugging information. The `-x optionP 94` ignores the first line of a program in the event that it’s not a valid Python statement (for example, when the first line starts the Python interpreter in a script). The `-U` option forces the interpreter to treat all string literals as Unicode. The `-W` option is used to specify a warning filter and is described further in the warnings module in Appendix A, “The Python Library.”

The program name appears after all the interpreter options. If no name is given, or the hyphen () character is used as a filename, the interpreter reads the program from standard input. If standard input is an interactive terminal, a banner and prompt are presented. Otherwise, the interpreter opens the specified file and executes its statements until an end-of-file marker is reached. The `-c` `cmd` option can be used to execute short programs in the form of a command-line option.

Command-line options appearing after the program name or hyphen () are passed to the program in `sys.argv`, as described in the section “Reading Options and Environment Variables” in Chapter 9, “Input and Output.”

Additionally, the interpreter reads the following environment variables:

Variable | Description
--- | ---
`PYTHONPATH` | Colon-separated module search path
`PYTHONSTARTUP` | File executed on interactive startup
`PYTHONHOME` | Location of the Python installation
`PYTHONINSPECT` | Implies the `-i` option
`PYTHONUNBUFFERED` | Implies the `-u` option
`PYTHONCASEOK` | Use non–case-sensitive matching for module names used by import (Python 2.1)

`PYTHONPATH` specifies a module search path that is inserted into the beginning of `sys.path`, which is described in Chapter 8. `PYTHONSTARTUP` specifies a file to execute when the interpreter runs in interactive mode. The `PYTHONHOME` variable is used to set the location of the Python installation but is rarely needed, since Python knows how to find its own libraries and the site-packages directory where extensions are normally installed. If a single directory such as `/usr/local` is given, the interpreter expects to find all files in that location. If two directories are given, such as `/usr/local:/usr/local/sparc-solaris-2.6`, the interpreter searches for platform-independent files in the first directory and platform-dependent files in the second. `PYTHONHOME` has no effect if no valid Python installation exists at the specified location.

On Windows, some of the environment variables such as `PYTHONPATH` are read from Registry entries found in `HKEY_LOCAL_MACHINE/Software/Python`. On the Macintosh, the environment variables can be adjusted using the `EditPythonPrefs` program.

| 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.