Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 1 ■ INSTANT HACKING: THE BASICS 5 where packagename.src.rpm is the real file name of the package you’re rebuilding. After you have done this, you should have a brand-new set of RPM files that you can install as described previously. ■Note To use the RPM installation, you must be logged in as root (the administrator account). If you don’t have root access, you should compile Python yourself, as described in the section “Compiling from Sources,” later in this chapter. Other Installation Mechanisms for Linux There are several other package systems and installation mechanisms for Linux than rpm. If you’re running a Linux system with some form of package manager, chances are you can get Python through it. ■Note You will probably have to have administrator privileges (a root account) in order to install Python using a package manager in Linux. For example, if you’re running Debian Linux, you should be able to install Python with the following command: $ apt-get install python2.4 If you’re running Gentoo Linux, you should be able to use Portage, like this: $ emerge python In both cases, $ is, of course, the bash prompt. Replace 2.4 with the most recent version number. Compiling from Sources If you don’t have a package manager, or would rather not use it, you can compile Python yourself. This may be the method of choice if you are on a UNIX box but you don’t have root access (installation privileges). This method is very flexible, and enables you to install Python wherever you want, including in your own home directory. To compile and install Python, follow these steps: 1. Go to the download page (refer to steps 1 and 2 in the instructions for installing Python on a Windows system). 2. Follow the instructions for downloading the sources. 3. Download the file with the extension .tgz. Store it in a temporary location. Assuming that you want to install Python in your home directory, you may want to put it in a directory such as ~/python. Enter this directory (e.g., using cd ~/python).
6 CHAPTER 1 ■ INSTANT HACKING: THE BASICS 4. Unpack the archive with the command tar -xzvf Python-2.4.tgz (where 2.4 is the version number of the downloaded source code). If your version of tar doesn’t support the z option, you may want to uncompress the archive with gunzip first, and then use tar -xvf afterward. If there is something wrong with the archive, try downloading it again. Sometimes errors occur during download. 5. Enter the unpacked directory: $ cd Python-2.4 Now you should be able to execute the following commands: ./configure --prefix=$(pwd) make make install You should end up with an executable file called python in the current directory. (If this doesn’t work, please consult the README file included in the distribution.) Put the current directory in your PATH environment variable, and you’re ready to rock. To find out about the other configuration directives, execute ./configure --help Macintosh If you’re using a Macintosh, follow these steps: 1. Go to the standard download page (steps 1 and 2 from the Windows instructions earlier in this chapter). 2. Follow the link for the Macintosh OS X installer. There should also be a link to the MacPython download page, which has more information. The MacPython page also has versions of Python for older versions of the Macintosh OS. ■Note As of Python version 2.4, the OS X installer is still at version 2.3. Other Distributions You now have the standard Python distribution installed. Unless you have a particular interest in alternative solutions, that should be quite all right. If you are curious (and, perhaps, feeling a bit courageous), read on . . . There are other Python distributions, with the most well-known one being ActivePython. A slightly less well-known but quite interesting distribution is Stackless Python. These distributions are based on the standard implementation of Python, written in the C programming
- Page 2 and 3: Beginning Python From Novice to Pro
- Page 4: For Ranveig
- Page 7 and 8: ■CHAPTER 23 Project 4: In the New
- Page 9 and 10: viii ■CONTENTS Strings . . . . .
- Page 11 and 12: x ■CONTENTS ■CHAPTER 5 Conditio
- Page 13 and 14: xii ■CONTENTS ■CHAPTER 8 Except
- Page 15 and 16: xiv ■CONTENTS ■CHAPTER 11 Files
- Page 17 and 18: xvi ■CONTENTS Dynamic Web Pages w
- Page 19 and 20: xviii ■CONTENTS ■CHAPTER 20 Pro
- Page 21 and 22: xx ■CONTENTS Second Implementatio
- Page 23 and 24: xxii ■CONTENTS Preparations . . .
- Page 26: About the Technical Reviewer ■JER
- Page 30 and 31: Introduction A C program is like a
- Page 32 and 33: CHAPTER 1 ■ ■ ■ Instant Hacki
- Page 34 and 35: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 38 and 39: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 40 and 41: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 42 and 43: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 44 and 45: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 46 and 47: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 48 and 49: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 50 and 51: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 52 and 53: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 54 and 55: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 56 and 57: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 58 and 59: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 60 and 61: CHAPTER 1 ■ INSTANT HACKING: THE
- Page 62 and 63: CHAPTER 2 ■ ■ ■ Lists and Tup
- Page 64 and 65: CHAPTER 2 ■ LISTS AND TUPLES 33 I
- Page 66 and 67: CHAPTER 2 ■ LISTS AND TUPLES 35 A
- Page 68 and 69: CHAPTER 2 ■ LISTS AND TUPLES 37 >
- Page 70 and 71: CHAPTER 2 ■ LISTS AND TUPLES 39 M
- Page 72 and 73: CHAPTER 2 ■ LISTS AND TUPLES 41 T
- Page 74 and 75: CHAPTER 2 ■ LISTS AND TUPLES 43 L
- Page 76 and 77: CHAPTER 2 ■ LISTS AND TUPLES 45 W
- Page 78 and 79: CHAPTER 2 ■ LISTS AND TUPLES 47
- Page 80 and 81: CHAPTER 2 ■ LISTS AND TUPLES 49 a
- Page 82 and 83: CHAPTER 2 ■ LISTS AND TUPLES 51 S
- Page 84 and 85: CHAPTER 3 ■ ■ ■ Working with
CHAPTER 1 ■ INSTANT HACKING: THE BASICS 5<br />
where packagename.src.rpm is the real file name of the package you’re rebuilding. After you<br />
have done this, you should have a brand-new set of RPM files that you can install as described<br />
previously.<br />
■Note To use the RPM installation, you must be logged in as root (the administra<strong>to</strong>r account). If you don’t<br />
have root access, you should compile <strong>Python</strong> yourself, as described in the section “Compiling from Sources,”<br />
later in this chapter.<br />
Other Installation Mechanisms for Linux<br />
There are several other package systems and installation mechanisms for Linux than rpm. If<br />
you’re running a Linux system with some form of package manager, chances are you can get<br />
<strong>Python</strong> through it.<br />
■Note You will probably have <strong>to</strong> have administra<strong>to</strong>r privileges (a root account) in order <strong>to</strong> install <strong>Python</strong><br />
using a package manager in Linux.<br />
For example, if you’re running Debian Linux, you should be able <strong>to</strong> install <strong>Python</strong> with the<br />
following command:<br />
$ apt-get install python2.4<br />
If you’re running Gen<strong>to</strong>o Linux, you should be able <strong>to</strong> use Portage, like this:<br />
$ emerge python<br />
In both cases, $ is, of course, the bash prompt. Replace 2.4 with the most recent version<br />
number.<br />
Compiling from Sources<br />
If you don’t have a package manager, or would rather not use it, you can compile <strong>Python</strong> yourself.<br />
This may be the method of choice if you are on a UNIX box but you don’t have root access<br />
(installation privileges). This method is very flexible, and enables you <strong>to</strong> install <strong>Python</strong> wherever<br />
you want, including in your own home direc<strong>to</strong>ry. To compile and install <strong>Python</strong>, follow these steps:<br />
1. Go <strong>to</strong> the download page (refer <strong>to</strong> steps 1 and 2 in the instructions for installing <strong>Python</strong><br />
on a Windows system).<br />
2. Follow the instructions for downloading the sources.<br />
3. Download the file with the extension .tgz. S<strong>to</strong>re it in a temporary location. Assuming<br />
that you want <strong>to</strong> install <strong>Python</strong> in your home direc<strong>to</strong>ry, you may want <strong>to</strong> put it in a<br />
direc<strong>to</strong>ry such as ~/python. Enter this direc<strong>to</strong>ry (e.g., using cd ~/python).