[ Team LiB ] |
A.1 Installing the Python InterpreterBecause you need the Python interpreter to run Python scripts, your first step to using Python is usually installing Python. Unless a Python is already available on your machine, you'll need to fetch, install, and possibly configure a Python on your computer. You only need to do this once per machine, and perhaps not at all, if you will be running a frozen binary. A.1.1 Where to Get PythonFirst off, before doing anything, make sure you don't already have a recent Python on your machine. For instance, if you are working on Linux, or some Unix systems, Python is probably already installed. Type python at a shell prompt and see what happens; alternatively, try searching the usual places (/usr/bin, /usr/local/bin, etc.). On Windows, check if there is a Python entry in the programs menu you find in your Start button, at the bottom left of the screen. Make sure the Python you find is version 2.2 or later; you'll need that to run some of the examples in this edition. If there is no Python to be found, you will need to install one yourself. You can always fetch the latest and greatest standard Python release from http://www.python.org, Python's official web site; look for the Downloads link on that page, and grab a release for the platform you will be working on. There, you'll find prebuilt Python executables (unpack and run); self-installer executables for Windows (click to install); RPMs for Linux (unpack with rpm); the full source-code distribution (compile on your machine to generate an interpreter); and more. For some platforms such as PalmOS and PocketPC, Python's web site links to an offsite page where these versions are maintained. You can also find Python on CD-ROMs supplied with Linux distributions, included with some products and computer systems, sold by commercial outlets such as Dr. Dobb's Journal, and enclosed with other Python books. These tend to lag behind the current release somewhat, but usually not seriously so. In addition, a company called ActiveState also distributes Python, as part of its ActivePython package. This package combines standard CPython with extensions for Windows development, an IDE called PythonWin (described in Chapter 3), and other commonly used extensions. See ActiveState's web site, www.activestate.com, for more details on the ActivePython package. Finally, if you are interested in alternative Python implementations, try www.jython.org for Jython, and AciveState's web site for Python.NET; installation of these systems is beyond the scope of this book. A.1.2 Installation StepsOnce you have your Python, it must be installed. Installation steps are very platform-specific, but here are a few pointers for major Python platforms:
On other platforms, these details can differ widely; installing the "Pippy" port of Python for the PalmOS, for example, requires a hot synch operation with your PDA, and Python for the Sharp Zaurus Linux-based PDA comes as one or more .ipk files, which you simply run to install. Because additional install procedures for both executable and source forms are well documented, though, we'll skip further details here. A.1.3 Configuration StepsAfter you've installed Python, you can also configure system settings that impact the way Python runs your code. When you are just getting started with the language, you can probably skip this section completely; there is usually no need to make any system settings at all for basic programs. Generally speaking, though, parts of the Python interpreter's behaviour can be configured both with enironment variable settings, and command-line options. In this section, we take a brief look at Python environment variables. Python command-line options—words listed when you launch a Python program from a system prompt—are used more rarely, and have very specialized roles; see other documentation sources for Python's command-line option details. A.1.3.1 Python environment variablesEnvironment variables—known to some as shell variables, or DOS variables—live outside Python, and thus can be used to customize the interpreter's behavior each time it is run on a given computer. Python recognizes a handful of environment variable settings, but only a few are used often enough to warrant explanation here. Table A-1 summarizes the main Python-related environment variable settings.
These variables are straightforward to use, but here are a few pointers:
Note that because these environment settings (as well as .pth files) are external to Python itself, the time at which you set these is usually irrelevant. They may be set before or after Python is installed—just as long as they are set the way you require before Python is actually run. A.1.4 How to Set Configuration OptionsThe way to set these variables, and what to set them to, depends on the type of computer you will work on. And again, remember that you don't necessarily have to set these at all right away; especially when working under IDLE (described in Chapter 3), configuration is not required up-front. But suppose, for illustration, that you have generally useful module files in directories called utilities and package1 somewhere on your machine, and you want to be able to import these modules from files located in any other directory. To load a file called spam.py from the utilities directory, you want to be able to say: import spam from another file located anywhere on your computer. To make this work, you'll have to configure your module search path one way or another to imclude the directory contaiing spam.py. Here are a few tips on this process. A.1.4.1 UNIX/Linux shell variablesOn Unix systems the way to set envoronment variables depends on the shell you use. Under the csh shell, you might add a line like the following in your .cshrc or .login file, to set the Python module search path: setenv PYTHONPATH /usr/home/pycode/utilities:/usr/lib/pycode/package1 This tells Python to look for imported modules in two user-defined directories. But if you're using the ksh shell, the setting might instead appear in your .kshrc file, and look like this: export PYTHONPATH="/usr/home/pycode/utilities:/usr/lib/pycode/package1" Other shells may use different, but analogous syntax. A.1.4.2 DOS variables (Windows)If you are using MS-DOS or some older flavors of MS-Windows, you may need to add an environment variable configuration command to your C:\autoexec.bat file, and reboot your machine for the changes to take effect. The configuration command on such machines would have a syntax unique to DOS: set PYTHONPATH=c:\pycode\utilities;d:\pycode\package1 You can type such a command in a DOS console window too, but the setting will then be active only for that one console window. Changing your .bat file makes the change permanent, and global to all programs. A.1.4.3 Other Windows optionsOn more recent versions of Windows, you may instead set PYTHONPATH and others by navigating to the system environment variable GUI, without having to edit files or reboot. On XP, select Control Panel, choose the System icon, pick the Advanced tab, and click the Environment Variables button to edit or add new variables (PYTHONPATH is usually a user variable). You do not need to reboot your machine, but be sure to restart Python if open, so that it picks up your changes (it configures its path at start-up time only). If you are an experienced Windows user, you may also be able to configure the module search path by using the Windows Registry Editor. Type regedit in the Run... option of your Start button to see if the typical registry tool is on your machine, and navigate to Python's entries. This is a delicate and error-prone procedure—unless you're familiar with the registry, we suggest other options. A.1.4.4 Path filesFinally, if you choose to extend the module search path with a .pth file instead of the PYTHONPATH variable, you might instead code a text file that looks like the following on Windows (file C:\Python22\mypath.pth). Its contents will differ per platform, and its container directory may differ per both platform and Python release. Python locates this file automatically when it starts up: c:\pycode\utilities d:\pycode\package1 Directory names in path files may be absolute, or relative to the directory containing the path file; multiple .pth files can be used (all their directories are added); and .pth files may appear in various automatically-checked directories that are platform and version-specific. For example, Release 2.2 typically looks for path files in C:\Python22 and C:\Python22\Lib\site-packages on Windows, and in /usr/local/lib/python2.2/site-packages and /usr/local/lib/site-python on Unix and Linux. Because these settings are often optional, though, and because this isn't a book on operating system shells, we're going to defer to other sources for more details. Consult your system shell's manpages or other documentation for details. And if you have trouble figuring out what your settings must be, ask your system administrator or other local expert for help. |
[ Team LiB ] |