See All Titles |
![]() ![]() InterpreterAfter 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.
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. 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.
|
© 2002, O'Reilly & Associates, Inc. |