Appendix A. Installation and Configuration

This appendix provides additional installation and configuration details as a resource for people new to such topics.

Installing the Python Interpreter

Because you need the Python interpreter to run Python scripts, the first step in using Python is usually installing Python. Unless one is already available on your machine, you’ll need to fetch, install, and possibly configure a recent version of Python on your computer. You’ll only need to do this once per machine, and, if you will be running a frozen binary (described in Chapter 2), you may not need to do it at all.

Is Python Already Present?

Before you do anything else, check whether you already have a recent Python on your machine. If you are working on Linux, Mac OS X, and some Unix systems, Python is probably already installed on your computer. Type python at a shell prompt (sometimes called a terminal window), and see what happens. Alternatively, try searching for “python” in the usual places—/usr/bin, /usr/local/bin, etc.

On Windows, check whether there is a Python entry in the Start button’s All Programs menu (at the bottom left of the screen). If you find a Python, make sure it’s version 2.5 or later; although any recent Python will do for most of this text, you’ll need at least version 2.5 to run some of the examples in this edition.

Where to Fetch Python

If there is no Python to be found, you will need to install one yourself. The good news is that Python is an open source system that is freely available on the Web, and very easy to install on most platforms.

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 choose a release for the platform on which you will be working. You’ll find prebuilt Python executables (unpack and run), self-installer files for Windows (click the file’s icon to install), RPMs for Linux (unpack with rpm), the full source code distribution (compile on your machine to generate an interpreter), and more. You can find links to offsite web pages where versions for some platforms, such as PalmOS, Nokia cell phones, and Windows Mobile, are maintained either at Python.org itself, or via a Google web search.

You can also find Python on CD-ROMs supplied with Linux distributions, included with some products and computer systems, and enclosed with some other Python books. These tend to lag behind the current release somewhat, but usually not seriously so.

In addition, a company called ActiveState 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 for more details on the ActivePython package.

Finally, if you are interested in alternative Python implementations, run a web search to check out Jython (the Python port to the Java environment) and IronPython (Python for the C#/.NET world), both of which are described in Chapter 2. Installation of these systems is beyond the scope of this book.

Installation Steps

Once you’ve downloaded Python, you need to install it. Installation steps are very platform-specific, but here are a few pointers for major Python platforms:

Windows

On Windows, Python comes as a self-installer MSI program file—simply double-click on its file icon, and answer Yes or Next at every prompt to perform a default install. The default install includes Python’s documentation set and support for Tkinter GUIs, shelve databases, and the IDLE development GUI. Python 2.5 is normally installed in the directory C:Python25, though this can be changed at install time.

For convenience, after the install, Python shows up in the Start button’s All Programs menu. Python’s menu there has five entries that give quick access to common tasks: starting the IDLE user interface, reading module documentation, starting an interactive session, reading Python’s standard manuals in a web browser, and uninstalling. Most of these actions involve concepts explored in detail elsewhere in this text.

When installed on Windows, Python also automatically registers itself to be the program that opens Python files when their icons are clicked (a program launch technique described in Chapter 3). It is also possible to build Python from its source code on Windows, but this is not commonly done.

One note for Windows Vista users: security features of the current version of Vista change some of the rules for using MSI installer files. See the sidebar "The Python 2.5 MSI Installer on Windows Vista" in this appendix for assistance if the current Python installer does not work, or does not place Python in the correct place on your machine.

Linux

On Linux, Python is available as one or more RPM files, which you unpack in the usual way (consult the RPM manpage for details). Depending on which RPMs you download, there may be one for Python itself, and another that adds support for Tkinter GUIs and the IDLE environment. Because Linux is a Unix-like system, the next paragraph applies as well.

Unix

On Unix systems, Python is usually compiled from its full C source code distribution. This usually only requires unpacking the file, and running simple config and make commands; Python configures its own build procedure automatically, according to the system on which it is being compiled. However, be sure to see the package’s README file for more details on this process. Because Python is open source, its source code may be used and distributed free of charge.

On other platforms, these details can differ widely; installing the “Pippy” port of Python for PalmOS, for example, requires a hotsync 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.

Configuring Python

After you’ve installed Python, you may want to configure some system settings that impact the way Python runs your code. (If 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 for basic programs.)

Generally speaking, parts of the Python interpreter’s behavior can be configured with environment variable settings and command-line options. In this section, we’ll take a brief look at Python environment variables. Python command-line options, which are 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 details.

Python Environment Variables

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

Table A-1. Important environment variables

Variable

Role

PATH (or path)

System shell search path (for finding “python”)

PYTHONPATH

Python module search path (for imports)

PYTHONSTARTUP

Path to Python interactive startup file

TCL_LIBRARY, TK_LIBRARY

GUI extension variables (Tkinter)

These variables are straightforward to use, but here are a few pointers:

  • The PATH setting lists a set of directories that the operating system searches for executable programs. It should normally include the directory where your Python interpreter lives (the python program on Unix, or the python.exe file on Windows).

    You don’t need to set this variable at all if you are willing to work in the directory where Python resides, or type the full path to Python in command lines. On Windows, for instance, the PATH is irrelevant if you run a cd C:Python25 before running any code (to change to the directory where Python lives), or always type C:Python25python instead of just python (giving a full path). Also, note that PATH settings are mostly for launching programs from command lines; they are usually irrelevant when launching via icon clicks and IDEs.

  • The PYTHONPATH setting serves a role similar to PATH: the Python interpreter consults the PYTHONPATH variable to locate module files when you import them in a program. (For more on the module search path, refer to Chapter 18.) If used, this variable is set to a platform-dependent list of directory names, separated by colons on Unix, and semicolons on Windows. This list normally includes just your own source code directories.

    You don’t need to set this variable unless you will be performing cross-directory imports—because Python always searches the home directory of the program’s top-level file automatically, this setting is required only if a module needs to import another module that lives in a different directory. As mentioned in Chapter 18, .pth files are a recent alternative to PYTHONPATH.

  • If PYTHONSTARTUP is set to the pathname of a file of Python code, Python executes the file’s code automatically whenever you start the interactive interpreter, as though you had typed it at the interactive command line. This is a rarely used but handy way to make sure you always load certain utilities when working interactively; it saves an import.

  • If you wish to use the Tkinter GUI toolkit, you might have to set the two GUI variables in Table A-1 to the names of the source library directories of the Tcl and Tk systems (much like PYTHONPATH). However, these settings are not required on Windows systems (where Tkinter support is installed alongside Python), and are usually not required elsewhere if Tcl and Tk reside in standard directories.

Note that because these environment settings (as well as .pth files) are external to Python itself, when you set them is usually irrelevant. They may be set before or after Python is installed, as long as they are set the way you require before Python is actually run.

How to Set Configuration Options

The way to set Python-related environment variables, and what to set them to, depends on the type of computer you’re working on. And again, remember that you won’t necessarily have to set these at all right away; especially if you’re working in 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 other directories. That is, 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 include the directory containing spam.py. Here are a few tips on this process.

Unix/Linux shell variables

On Unix systems, the way to set environment 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.

DOS variables (Windows)

If you are using MS-DOS, or some older flavors of 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 has a syntax unique to DOS:


set PYTHONPATH=c:pycodeutilities;d:pycodepackage1

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.

Other Windows options

On more recent versions of Windows, you may instead set PYTHONPATH and other variables via the system environment variable GUI without having to edit files or reboot. On XP, select the 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 it’s open so that it picks up your changes (it configures its path at startup 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. Go to Start → Run . . . and type regedit. Assuming the typical registry tool is on your machine, you can then navigate to Python’s entries and make your changes. This is a delicate and error-prone procedure, though, so unless you’re familiar with the Registry, I suggest using other options.

Path files

Finally, 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:Python25mypath.pth):


c:pycodeutilities
d:pycodepackage1

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.

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.5 typically looks for path files in C:Python25 and C:Python25Libsite-packages on Windows, and in /usr/local/lib/python2.5/site-packages, and /usr/local/lib/site-python on Unix and Linux.

Because these settings are often optional, and because this isn’t a book on operating system shells, I’ll defer to other sources for more details. Consult your system shell’s manpages or other documentation for more information, and if you have trouble figuring out what your settings should be, ask your system administrator or another local expert for help.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.39.60