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) or self-installing system, your setup tasks may be trivial or null.

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, or some Unix systems, Python is probably already installed on your computer, though it may be one or two releases behind the cutting edge. Here’s how to check:

  • On Windows 7 and earlier, check whether there is a Python entry in the Start button’s All Programs menu (at the bottom left of the screen). On Windows 8, look for Python in a Start screen tile, your Search tool, the “All apps” display on your Start screen, or a File Explorer in desktop mode (more on Windows 8 in an upcoming sidebar).

  • On Mac OS X, open a Terminal window (Applications→Utilities→Terminal) and type python at the prompt. Python, IDLE, and its tkinter GUI toolkit are standard components of this system.

  • On Linux and Unix, type python at a shell prompt (a.k.a. terminal window), and see what happens. Alternatively, try searching for “python” in the usual places—/usr/bin, /usr/local/bin, etc. As on Macs, Python is a standard part of Linux systems.

If you find a Python, make sure it’s a recent version. Although any recent Python will do for most of this text, this edition focuses on Python 3.3 and 2.7 specifically, so you may want to install one of these to run some of the examples in this book.

Speaking of versions, per the preface, I recommend starting out with Python 3.3 or later if you’re learning Python anew and don’t need to deal with existing 2.X code; otherwise, you should generally use Python 2.7. Some popular Python-based systems still use older releases, though (2.6 and even 2.5 are still widespread), so if you’re working with existing systems be sure to use a version relevant to your needs; the next section describes locations where you can fetch a variety of Python versions.

Where to Get Python

If there is no Python on your machine, 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 website. 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 self-installer files for Windows (run to install), Installer Disk Images for Mac OS X (installed per Mac conventions), the full source code distribution (typically compiled on Linux, Unix, or OS X machines to generate an interpreter), and more.

Although Python is standard on Linux these days, you can also find RPMs for Linux on the Web (unpack them with rpm). Python’s website also has links to pages where versions for other platforms are maintained, either at Python.org (http://www.python.org) itself or offsite. For example, you can find third-party Python installers for Google’s Android, as well as apps to install Python on Apple’s iOS.

A Google web search is another great way to find Python installation packages. Among other platforms, you can find Python prebuilt for iPods, Palm handhelds, Nokia cell phones, PlayStation and PSP, Solaris, AS/400, and Windows Mobile, though some of these are typically a few releases behind the curve.

If you find yourself pining for a Unix environment on a Windows machine, you might also be interested in installing Cygwin and its version of Python (see http://www.cygwin.com). Cygwin is a GPL-licensed library and toolset that provides full Unix functionality on Windows machines, and it includes a prebuilt Python that makes use of all the Unix tools provided.

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, you can find Python in some free and commercial development bundles. At this writing, this alternative distributions category includes:

ActiveState ActivePython

A package that combines Python with extensions for scientific, Windows, and other development needs, including PyWin32 and the PythonWin IDE

Enthought Python Distribution

A combination of Python and a host of additional libraries and tools oriented toward scientific computing needs

Portable Python

A blend of Python and add-on packages configured to run directly from a portable device

Pythonxy

A scientific-oriented Python distribution based on Qt and Spyder

Conceptive Python SDK

A bundle targeted at business, desktop, and database applications

PyIMSL Studio

A commercial distribution for numerical analysis

Anaconda Python

A distribution for analysis and visualization of large data sets

This set is prone to change, so search the Web for details on all of the above, and others. Some of these are free, some are not, and some have both free and nonfree versions. All combine the standard Python freely available at http://www.python.org with additional tools, but can simplify install tasks for many.

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 the major Python platforms (biased in volume toward Windows, only because that is the platform where most Python newcomers are likely to encounter the language first):

Windows

For Windows (including XP, Vista, 7, and 8), 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 (Tkinter in Python 2.X) GUIs, shelve databases, and the IDLE development GUI. Python 3.3 and 2.7 are normally installed in the directories C:\Python33 and C:\Python27 though this can be changed at install time.

For convenience, on Windows 7 and earlier Python shows up after the install in the Start button’s All Programs menu (see ahead for Windows 8 notes). 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, and uninstalling. Most of these options involve concepts explored in detail elsewhere in this text.

When installed on Windows, Python also automatically uses filename associations to register 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 so we’ll skip the details here (see python.org).

Three additional install-related notes for Windows users: first, be sure to see the next appendix for an introduction to the new Windows launcher shipped with 3.3; it changes some of the rules for installation, file associations, and command lines, but can be an asset if you have multiple Python versions on your computer (e.g., both 2.X and 3.X). Per Appendix B, Python 3.3’s MSI installer also has an option to set your PATH variable to include Python’s directory.

Second, Windows 8 users should see the sidebar in this appendix Using Python on Windows 8. Standard Python installs and works the same on Windows 8, where it runs in desktop mode, but you won’t get the Start button menu described earlier, and the tablet interface on top is not yet directly supported.

Finally, some Windows Vista users may run into install issues related to security features. This seems to have been resolved over time (and Vista is relatively rare these days), but if running the MSI installer file directly doesn’t work as expected, it’s probably because MSI files are not true executables and do not correctly inherit administrator permissions (they run per the registry). To fix, run the installer from a command line with appropriate permissions: Select Command Prompt, choose “Run as administrator,” cd to the directory where your Python MSI file resides, and run the MSI installer with a command line of the form: msiexec /i python-2.5.1.msi.

Linux

For Linux, if Python or your desired flavor of it is not already present, you can probably obtain it 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

For Unix systems, Python is usually compiled from its full C source code distribution. This usually only requires you to unpack the file and run 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 the installation details can differ widely, but they generally follow the platform’s normal conventions. For example, installing the “Pippy” port of Python for PalmOS required a hotsync operation with your PDA, and Python for the Sharp Zaurus Linux-based PDA was one or more .ipk files, which you simply ran to install (these likely still work, though finding the devices today may be a logistical challenge!).

More recently, Python can be installed and used on Android and iOS platforms too, but installation and usage techniques are too platform-specific to cover here. For additional install procedures and the latest on available ports, try both Python’s website and a web search.



[73] Lest that seem too sarcastic, I should note that Windows 8.1 may address some launch screen and Start button (if not menu) concerns per late-breaking rumors, and this edition’s new Windows 8 sidebar replaces one in prior editions that discussed a Windows Vista issue. Any similarities you might deduce from that are officially coincidental.