Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 18 ■ PACKAGING YOUR PROGRAMS 377 writing manifest file 'MANIFEST' creating Hello-1.0 making hard links in Hello-1.0... hard linking hello.py -> Hello-1.0 hard linking setup.py -> Hello-1.0 tar -cf dist/Hello-1.0.tar Hello-1.0 gzip -f9 dist/Hello-1.0.tar removing 'Hello-1.0' (and everything under it) ■Note If you’ve run the command before, and you have a file called MANIFEST already, you will see the word reading instead of writing at the beginning. If you’ve restructured your package and want to repackage it, deleting the MANIFEST file can be a good idea, in order to start afresh. Now, in addition to the build subdirectory, you should have one called dist. Inside it, you will find a gzip’ed tar archive called Hello-1.0.tar.gz. This can now be distributed to others, and they can unpack it and install it using the included setup.py script. If you don’t want a .tar.gz file, there are plenty of other distribution formats available, and you can set them all through the command-line switch --formats. (As the plural name indicates, you can supply more than one format, separated by commas, to create more archive files in one go.) The format names available in Python 2.4 are bztar (for bzip2’ed tar files), gztar (the default, for gzip’ed tar files), tar (for uncompressed tar files), zip (for ZIP files), and ztar (for compressed tar files, using the UNIX command compress). There is also a command called bdist, which you can use (among other things) to create simple Windows installers and Linux RPM files. (You normally use this to create binary distributions, where extensions have been compiled for a particular architecture. See the following section for information about compiling extensions.) The formats available for bdist (in addition to the ones available for sdist) are rpm (for RPM packages) and wininst (for Windows executable installer). One interesting twist is that you can, in fact, build Windows installers for your package in non-Windows systems, provided that you don’t have any extensions you need to compile. If you have access to both, say, a Linux machine and a Windows box, you could try running python setup.py bdist --formats=wininst on your Linux machine, and then (after ignoring a few warnings about compiler settings) copying the file dist/Hello-1.0.win32.exe to your Windows machine and running it. You should then be presented with a rudimentary installer wizard. (You can cancel the process before actually installing the module, if you’d like.)
378 CHAPTER 18 ■ PACKAGING YOUR PROGRAMS USING A REAL INSTALLER The installer you get with the wininst format in Distutils is very basic. As with normal Distutils installation, it will not let you uninstall your packages, for example. This may not be a problem for you, but sometimes you may want a more professional look. This can be especially true if you’re creating an executable using py2exe (described later in this chapter). In this case, you might want to consider using some standard installer such as Inno Setup (http://jrsoftware.org/isinfo.php), which works very well with executables created with py2exe, and will install your program more as a normal Windows program and give you functionality such as uninstalling. A more Python-centric option is the McMillan installer (a Web search should give you an updated download location), which can also work as an alternative to py2exe when building executable programs. Other options include InstallShield (http://installshield.com), Wise installer (http://wise.com), Installer VISE (http://www.mindvision.com), Nullsoft Scriptable Install System (http://nsis.sf.net), Youseful Windows Installer (http://youseful.com), and Ghost Installer (http://ethalone.com). For more information on the Windows Installer, see Phil Wilson’s The Definitive Guide to Windows Installer (Apress, 2004). Compiling Extensions In Chapter 17, you saw how to write extensions for Python. You may agree that compiling these extensions could be a bit cumbersome at times; luckily, you can use Distutils for this as well. You may want to refer back to Chapter 17 for the source code to the program palindrome (in Listing 17-6). Assuming that you have the source file palindrome2.c in the current (empty) directory, the following setup.py script could be used to compile (and install) it: from distutils.core import setup, Extension setup(name='palindrome', version='1.0', ext_modules = [ Extension('palindrome', ['palindrome2.c']) ]) If you run the install command with this setup.py script, the palindrome extension module should be compiled automatically before it is installed. As you can see, instead of specifying a list of module names, you give the ext_modules argument a list of Extension instances. The constructor takes a name and a list of related files—this is where you would specify header (.h) files, for example. If you’d rather just compile the extension in place (resulting in a file called palindrome.so in the current directory), you can use the following command: python setup.py build_ext --inplace Now we get to a real juicy bit . . . if you’ve got SWIG installed (see Chapter 17), you can have Distutils use it directly!
- Page 357 and 358: 326 CHAPTER 15 ■ PYTHON AND THE W
- Page 359 and 360: 328 CHAPTER 15 ■ PYTHON AND THE W
- Page 361 and 362: 330 CHAPTER 15 ■ PYTHON AND THE W
- Page 363 and 364: 332 CHAPTER 15 ■ PYTHON AND THE W
- Page 365 and 366: 334 CHAPTER 15 ■ PYTHON AND THE W
- Page 367 and 368: 336 CHAPTER 15 ■ PYTHON AND THE W
- Page 369 and 370: 338 CHAPTER 15 ■ PYTHON AND THE W
- Page 372 and 373: CHAPTER 16 ■ ■ ■ Testing, 1-2
- Page 374 and 375: CHAPTER 16 ■ TESTING, 1-2-3 343 W
- Page 376 and 377: CHAPTER 16 ■ TESTING, 1-2-3 345 d
- Page 378 and 379: CHAPTER 16 ■ TESTING, 1-2-3 347 u
- Page 380 and 381: CHAPTER 16 ■ TESTING, 1-2-3 349 F
- Page 382 and 383: CHAPTER 16 ■ TESTING, 1-2-3 351 P
- Page 384 and 385: CHAPTER 16 ■ TESTING, 1-2-3 353 "
- Page 386: CHAPTER 16 ■ TESTING, 1-2-3 355 N
- Page 389 and 390: 358 CHAPTER 17 ■ EXTENDING PYTHON
- Page 391 and 392: 360 CHAPTER 17 ■ EXTENDING PYTHON
- Page 393 and 394: 362 CHAPTER 17 ■ EXTENDING PYTHON
- Page 395 and 396: 364 CHAPTER 17 ■ EXTENDING PYTHON
- Page 397 and 398: 366 CHAPTER 17 ■ EXTENDING PYTHON
- Page 399 and 400: 368 CHAPTER 17 ■ EXTENDING PYTHON
- Page 401 and 402: 370 CHAPTER 17 ■ EXTENDING PYTHON
- Page 404 and 405: CHAPTER 18 ■ ■ ■ Packaging Yo
- Page 406 and 407: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 410 and 411: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 412 and 413: CHAPTER 19 ■ ■ ■ Playful Prog
- Page 414 and 415: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 416 and 417: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 418 and 419: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 420: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 423 and 424: 392 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 425 and 426: 394 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 427 and 428: 396 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 429 and 430: 398 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 431 and 432: 400 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 433 and 434: 402 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 435 and 436: 404 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 437 and 438: 406 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 439 and 440: 408 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 442 and 443: CHAPTER 21 ■ ■ ■ Project 2: P
- Page 444 and 445: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 446 and 447: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 448 and 449: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 450 and 451: CHAPTER 21 ■ PROJECT 2: PAINTING
- Page 452 and 453: CHAPTER 22 ■ ■ ■ Project 3: X
- Page 454 and 455: CHAPTER 22 ■ PROJECT 3: XML FOR A
- Page 456 and 457: CHAPTER 22 ■ PROJECT 3: XML FOR A
378 CHAPTER 18 ■ PACKAGING YOUR PROGRAMS<br />
USING A REAL INSTALLER<br />
The installer you get with the wininst format in Distutils is very basic. As with normal Distutils installation,<br />
it will not let you uninstall your packages, for example. This may not be a problem for you, but sometimes you<br />
may want a more professional look. This can be especially true if you’re creating an executable using py2exe<br />
(described later in this chapter). In this case, you might want <strong>to</strong> consider using some standard installer such<br />
as Inno Setup (http://jrsoftware.org/isinfo.php), which works very well with executables created<br />
with py2exe, and will install your program more as a normal Windows program and give you functionality<br />
such as uninstalling.<br />
A more <strong>Python</strong>-centric option is the McMillan installer (a Web search should give you an updated download<br />
location), which can also work as an alternative <strong>to</strong> py2exe when building executable programs. Other<br />
options include InstallShield (http://installshield.com), Wise installer (http://wise.com), Installer<br />
VISE (http://www.mindvision.com), Nullsoft Scriptable Install System (http://nsis.sf.net), Youseful<br />
Windows Installer (http://youseful.com), and Ghost Installer (http://ethalone.com).<br />
For more information on the Windows Installer, see Phil Wilson’s The Definitive Guide <strong>to</strong> Windows<br />
Installer (Apress, 2004).<br />
Compiling Extensions<br />
In Chapter 17, you saw how <strong>to</strong> write extensions for <strong>Python</strong>. You may agree that compiling these<br />
extensions could be a bit cumbersome at times; luckily, you can use Distutils for this as well.<br />
You may want <strong>to</strong> refer back <strong>to</strong> Chapter 17 for the source code <strong>to</strong> the program palindrome (in<br />
Listing 17-6). Assuming that you have the source file palindrome2.c in the current (empty) direc<strong>to</strong>ry,<br />
the following setup.py script could be used <strong>to</strong> compile (and install) it:<br />
from distutils.core import setup, Extension<br />
setup(name='palindrome',<br />
version='1.0',<br />
ext_modules = [<br />
Extension('palindrome', ['palindrome2.c'])<br />
])<br />
If you run the install command with this setup.py script, the palindrome extension module<br />
should be compiled au<strong>to</strong>matically before it is installed. As you can see, instead of specifying<br />
a list of module names, you give the ext_modules argument a list of Extension instances. The<br />
construc<strong>to</strong>r takes a name and a list of related files—this is where you would specify header (.h)<br />
files, for example.<br />
If you’d rather just compile the extension in place (resulting in a file called palindrome.so<br />
in the current direc<strong>to</strong>ry), you can use the following command:<br />
python setup.py build_ext --inplace<br />
Now we get <strong>to</strong> a real juicy bit . . . if you’ve got SWIG installed (see Chapter 17), you can have<br />
Distutils use it directly!