Chapter 2
In This Chapter
Obtaining a copy of Python for your system
Performing the Python installation
Finding and using Python on your system
Ensuring your installation works as planned
Creating applications requires that you have another application, unless you really want to get low level and write applications in machine code — a decidedly difficult experience that even true programmers avoid if at all possible. If you want to write an application using the Python programming language, you need the applications required to do so. These applications help you work with Python by creating Python code, providing help information as you need it, and letting you run the code you write. This chapter helps you obtain a copy of the Python application, install it on your hard drive, locate the installed applications so that you can use them, and test your installation so that you can see how it works.
Every platform (combination of computer hardware and operating system software) has special rules that it follows when running applications. The Python application hides these details from you. You type code that runs on any platform that Python supports, and the Python applications translate that code into something the platform can understand. However, in order for the translation to take place, you must have a version of Python that works on your particular platform. Python supports these platforms:
To get the right version for your platform, you need to go to http://www.python.org/download/releases/3.3.4/. The download section is initially hidden from view, so you need to scroll halfway down the page. You see a page similar to the one shown in Figure 2-1. The main part of the page contains links for Windows, Mac OS X, and Linux downloads. These links provide you with the default setup that is used in this book. The platform-specific links on the left side of the page show you alternative Python configurations that you can use when the need arises. For example, you may want to use a more advanced editor than the one provided with the default Python package, and these alternative configurations can provide one for you.
If you want to work with another platform, click the Other link on the left side of the page. You see a list of Python installations for other platforms, as shown in Figure 2-2. Many of these installations are maintained by volunteers rather than by the people who create the versions of Python for Windows, Mac OS X, and Linux. Make sure you contact these individuals when you have installation questions because they know how best to help you get a good installation on your platform.
After you download your copy of Python, it’s time to install it on your system. The downloaded file contains everything needed to get you started:
This book assumes that you’re using one of the default Python setups found at http://www.python.org/download/releases/3.3.4/. The following sections describe how to install Python on the three platforms directly supported by this book: Windows, Mac OS X, and Linux.
The installation process on a Windows system follows the same procedure that you use for other application types. The main difference is in finding the file you downloaded so that you can begin the installation process. The following procedure should work fine on any Windows system, whether you use the 32-bit or the 64-bit version of Python.
The name of this file varies, but normally it appears as python-3.3.4.msi for 32-bit systems and python-3.3.4.amd64.msi for 64-bit systems. The version number is embedded as part of the filename. In this case, the filename refers to version 3.3.4, which is the version used for this book.
(You may see an Open File – Security Warning dialog box that asks whether you want to run this file. Click Run if you see this dialog box pop up.) You see a Python Setup dialog box similar to the one shown in Figure 2-3. The exact dialog box you see depends on which version of the Python installation program you download.
Install asks you to provide the name of an installation directory for Python, as shown in Figure 2-4. Using the default destination will save you time and effort later. However, you can install Python anywhere you desire.
Using the Windows Program Files or Program Files (x86) folder is problematic for two reasons. First, the folder name has a space in it, which makes it hard to access from within the application. Second, the folder usually requires administrator access, so you’ll constantly battle the User Account Control (UAC) feature of Windows if you install Python in either folder.
Python asks you to customize its installation, as shown in Figure 2-5.
Enabling the Add python.exe to Path option will save you time later. This feature makes it possible to access Python from the Command Prompt window. Don’t worry too much about how you use this feature just yet, but it really is a good feature to have installed. The book assumes that you’ve enabled this feature. Don’t worry about the other features you see in Figure 2-5. They’re all enabled by default, which provides you with maximum access to Python functionality.
You see the installation process start. At some point, you might see a User Account Control dialog box asking whether you want to perform the install. If you see this dialog box, click Yes. The installation continues and you see an Installation Complete dialog box.
Python is ready for use.
Your Mac system likely already has Python installed on it. However, this installation is normally a few years old — or whatever the age of your system happens to be. For the purposes of this book, the installation will likely work fine. You won’t be testing the limits of Python programming technology — just getting a great start using Python.
Depending on how you use Python, you might want to update your installation at some point. Part of this process involves installing the GNU Compiler Collection (GCC) tools so that Python has access to the low-level resources it needs. The following steps get you started with installing a new version of Python on your Mac OS X system.
You see information regarding the latest version of Python, as shown in Figure 2-1.
The Python disk image begins downloading. Be patient: The disk image requires several minutes to download. Most browsers provide a method for monitoring the download process so that you can easily see how long the download will take. When the download is complete, your Mac automatically opens the disk image for you.
The disk image actually looks like a folder. Inside this folder, you see a number of files, including python.mpkg. The python.mpkg file is the one that contains the Python application. The text files contain information about the build, licensing, and any late-breaking notes.
You see a Welcome dialog box that tells you about this particular Python build.
The installation program displays late-breaking notes about Python, licensing information (click Agree when asked about the licensing information), and, finally, a destination dialog box.
The Installation Type dialog box appears. This dialog box performs two tasks:
The book assumes that you’re performing a standard installation and that you haven’t changed the installation location. However, these options are available in case you want to use them.
The installer may request your administrator password. Type the administrator name and password, if required, into the dialog box and click OK. You see an Installing Python dialog box. The contents of this dialog box will change as the installation process proceeds so that you know what part of Python the installer is working with.
After the installation is completed, you see an Install Succeeded dialog box.
Python is ready to use. (You can close the disk image at this point and remove it from your system.)
Some versions of Linux come with Python installed. For example, if you have a Red Hat Package Manager (RPM)-based distribution (such as SUSE, Red Hat, Yellow Dog, Fedora Core, and CentOS), you likely already have Python on your system and don’t need to do anything else.
You actually have two techniques to use to install Python on Linux. The following sections discuss both techniques. The first technique works on any Linux distribution; the second technique has special criteria that you must meet.
The standard Linux installation works on any system. However, it requires you to work at the Terminal and type commands to complete it. Some of the actual commands may vary by version of Linux. The information at http://docs.python.org/3/install/ provides some helpful tips that you can use in addition to the procedure that follows.
You see information regarding the latest version of Python, as shown in Figure 2-1.
The Python source files begin downloading. Be patient: The source files require a minute or two to download.
The Archive Manager window opens. After the files are extracted, you see the Python 3.3.4 folder in the Archive Manager window.
The Archive Manager extracts the files to the Python 3.3.4 subfolder of your home folder.
The Terminal window appears. If you have never built any software on your system before, you must install the build essentials, SQLite, and bzip2 or the Python installation will fail. Otherwise, you can skip to Step 10 to begin working with Python immediately.
Linux installs the Build Essential support required to build packages (see https://packages.debian.org/squeeze/build-essential for details).
Linux installs the SQLite support required by Python for database manipulation (see https://packages.debian.org/squeeze/libsqlite3-dev for details).
Linux installs the bzip2 support required by Python for archive manipulation (see https://packages.debian.org/sid/libbz2-dev for details).
Terminal changes directories to the Python 3.3.4 folder on your system.
The script begins by checking the system build type and then performs a series of tasks based on the system you’re using. This process can require a minute or two because there is a large list of items to check.
Linux executes the make script to create the Python application software. The make process can require up to a minute — it depends on the processing speed of your system.
The system may ask you for your administrator password. Type your password and press Enter. At this point, a number of tasks take place as the system installs Python on your system.
All versions of Linux support the standard installation discussed in the “Using the standard Linux installation” section of this chapter. However, a few versions of Debian-based Linux distributions, such as Ubuntu 12.x and later, provide a graphical installation technique as well. You need the administrator group (sudo) password to use this procedure, so having it handy will save you time. The following steps outline the graphical installation technique for Ubuntu, but the technique is similar for other Linux installations:
You see a listing of the most popular software available for download and installation.
You see a listing of developer tools, including Python.
The Ubuntu Software Center provides details about the Python 3.3.4 entry and offers to install it for you.
Ubuntu begins the process of installing Python. A progress bar shows the download and installation status. When the installation is complete, the Install button changes to a Remove button.
You see a Python icon added to the desktop. Python is ready for use.
After you have Python installed on your system, you need to know where to find it. In some respects, Python does everything it can to make this process easy by performing certain tasks, such as adding the Python path to the machine’s path information during installation. Even so, you need to know how to access the installation, which the following sections describe.
A Windows installation creates a new folder in the Start menu that contains your Python installation. You can access it by choosing Start⇒All Programs⇒ Python 3.3. The two items of interest in the folder when creating new applications are IDLE (Python GUI) and Python (command line).
Clicking IDLE (Python GUI) produces a graphical interactive environment like the one shown in Figure 2-6. When you open this environment, IDLE automatically displays some information so that you know you have the right application open. For example, you see the Python version number (which is 3.3.4 in this case). It also tells you what sort of system you’re using to run Python.
The Python (command line) option opens a command prompt and executes the Python command, as shown in Figure 2-7. Again, the environment automatically displays information such as the Python version and the host platform.
A third method to access Python is to open a command prompt, type Python, and press Enter. You can use this approach when you want to gain additional flexibility over the Python environment, automatically load items, or execute Python in a higher-privilege environment (in which you gain additional security rights). Python provides a significant array of command-line options that you can see by typing Python /? at the command prompt and pressing Enter. Figure 2-8 shows what you typically see. Don’t worry too much about these command-line options — you won’t need them for this book, but it’s helpful to know they exist.
None of these environment variables is used in the book. However, you can find out more about them at http://docs.python.org/3.3/using/cmdline.html#environment-variables.
When working with a Mac, you probably have Python already installed and don’t need to install it for this book. However, you still need to know where to find Python. The following sections tell you how to access Python depending on the kind of installation you performed.
The default OS X installation doesn’t include a Python-specific folder in most cases. Instead, you must open Terminal by choosing Applications⇒Utilities⇒Terminal. After Terminal is open, you can type Python and press Enter to access the command-line version of Python. The display you see is similar to the one shown in Figure 2-7. As with Windows (see the “Using Windows” section of the chapter), using Terminal to open Python offers the advantage of using command-line switches to modify the manner in which Python works.
After you perform the installation on your Mac system, open the Applications folder. Within this folder, you find a Python 3.3 folder that contains the following:
Double-clicking IDLE application opens a graphical interactive environment that looks similar to the environment shown in Figure 2-6. There are some small cosmetic differences, but the content of the window is the same. Double-clicking Python Launcher opens a command-line environment similar to the one shown in Figure 2-7. This environment uses all the Python defaults to provide a standard execution environment.
After the installation process is complete, you can find a Python 3.3 subfolder in your home folder. The physical location of Python 3.3 on your Linux system is normally the /usr/local/bin/Python3.3 folder. This is important information because you may need to modify the path for your system manually. Linux developers need to type Python3.3, rather than just Python, when working at the Terminal window to obtain access to the Python 3.3.4 installation.
To ensure that you have a usable installation, you need to test it. It’s important to know that your installation will work as expected when you need it. Of course, this means writing your first Python application. To get started, open a copy of IDLE. As previously mentioned, IDLE automatically displays the Python version and host information when you open it (refer to Figure 2-6).
To see Python work, type print(“This is my first Python program.”) and press Enter. Python displays the message you just typed, as shown in Figure 2-9. The print() command displays onscreen whatever you tell it to display. You see the print() command used quite often in this book to display the results of tasks you ask Python to perform, so this is one of the commands you work with frequently.
Notice that IDLE color codes the various entries for you so that they’re easier to see and understand. The colors codes are your indicator that you’ve done something right. Four color codes are shown in Figure 2-9 (although they’re not visible in the print edition of the book):
You know that Python works now because you were able to issue a command to it, and it responded by reacting to that command. It might be interesting to see one more command. Type 3 + 4 and press Enter. Python responds by outputting 7, as shown in Figure 2-10. Notice that 3 + 4 appears in black type because it isn’t a command. However, the 7 is still in blue type because it’s output.
It’s time to end your IDLE session. Type quit( ) and press Enter. IDLE may display a message such as the one shown in Figure 2-11. Well, you never intended to kill anything, but you will now. Click OK, and the session dies.
Notice that the quit() command has parentheses after it, just as the print() command does. All commands have parentheses like these two. That’s how you know they’re commands. However, you don’t need to tell the quit() command anything, so you simply leave the area between the parentheses blank.