Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 18 ■ PACKAGING YOUR PROGRAMS 375 running build running build_py creating build creating build/lib copying hello.py -> build/lib What has happened is that Distutils has created a subdirectory called build, with yet another subdirectory named lib, and placed a copy of hello.py in build/lib. The build subdirectory is a sort of working area where Distutils assembles a package (and compiles extension libraries, for example). You don’t really need to run the build command when installing—it will be run automatically, if needed, when you run the install command. Speaking of which . . . let’s try to install the module: python setup.py install ■Caution This will copy the hello.py module to some system-specific directory in your PYTHONPATH. This should not pose a risk, but if you don’t want to clutter up your system, you might want to remove it afterward; make a note of the specific location where it is placed, as output by setup.py. At the time of writing, there is no standard uninstall command, so you’ll have to uninstall the module by hand. Now you should see something like the following: running install running build running build_py running install_lib copying build/lib/hello.py -> /path/to/python/lib/python2.4/site-packages byte-compiling /path/to/python/lib/python2.4/site-packages/hello.py to hello.pyc ■Note If you’re running a version of Python that you didn’t install yourself, and don’t have the proper privileges, you may not be allowed to install the module as shown, because you don’t have write permissions to the right directory. This is the standard mechanism used to install Python modules, packages, and extensions. All you have to do is provide the little setup script.
376 CHAPTER 18 ■ PACKAGING YOUR PROGRAMS In the example script, I have only used the Distutils directive py_modules; if you want to install entire packages, you can use the directive packages in an equivalent manner (just list the package names). There are many other options you can set (some of which are covered in the section “Compiling Extensions,” later in this chapter). You can also create configuration files for Distutils to set various properties (see the section “Distutils Configuration Files” in “Installing Python Modules,” http://python.org/doc/inst/config-syntax.html). The various ways of providing options (command-line switches, keyword arguments to setup, and Distutils configuration files) let you specify such things as what to install and where to install it. The nice thing is that these options can be used for more than one thing. The following section shows you how to wrap the modules you specified for installation as an archive file, ready for distribution. Wrapping Things Up Once you’ve written a setup.py script that will let the user install your modules, you can use it yourself, to build an archive file. You do this with the sdist (for “source distribution”) command: python setup.py sdist If you run this, you will probably get quite a bit of output, including some warnings. The warnings I get include a complaint about a missing author_email option, a missing MANIFEST.in file, and a missing README file. We can safely ignore all of these (although feel free to add an author_email option to your setup.py script, similar to the author option, and a README or README.txt text file and an empty file called MANIFEST.in in the current directory). WHAT’S MANIFEST.in? When you create a source distribution, a file called MANIFEST is created, containing a list of all your files. The MANIFEST.in file is a template for the manifest, and is used when figuring out what to install. You can include lines like include somedirectory/somefile.txt or include somedirectory/* to specify files that you want to have included, if Distutils hasn’t figured it out by itself, using your setup.py script (and default includes, such as README). After the warnings you should see output like the following:
- Page 355 and 356: 324 CHAPTER 15 ■ PYTHON AND THE W
- 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 408 and 409: 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
CHAPTER 18 ■ PACKAGING YOUR PROGRAMS 375<br />
running build<br />
running build_py<br />
creating build<br />
creating build/lib<br />
copying hello.py -> build/lib<br />
What has happened is that Distutils has created a subdirec<strong>to</strong>ry called build, with yet another<br />
subdirec<strong>to</strong>ry named lib, and placed a copy of hello.py in build/lib. The build subdirec<strong>to</strong>ry is<br />
a sort of working area where Distutils assembles a package (and compiles extension libraries,<br />
for example). You don’t really need <strong>to</strong> run the build command when installing—it will be run<br />
au<strong>to</strong>matically, if needed, when you run the install command.<br />
Speaking of which . . . let’s try <strong>to</strong> install the module:<br />
python setup.py install<br />
■Caution This will copy the hello.py module <strong>to</strong> some system-specific direc<strong>to</strong>ry in your PYTHONPATH.<br />
This should not pose a risk, but if you don’t want <strong>to</strong> clutter up your system, you might want <strong>to</strong> remove it afterward;<br />
make a note of the specific location where it is placed, as output by setup.py. At the time of writing,<br />
there is no standard uninstall command, so you’ll have <strong>to</strong> uninstall the module by hand.<br />
Now you should see something like the following:<br />
running install<br />
running build<br />
running build_py<br />
running install_lib<br />
copying build/lib/hello.py -> /path/<strong>to</strong>/python/lib/python2.4/site-packages<br />
byte-compiling /path/<strong>to</strong>/python/lib/python2.4/site-packages/hello.py <strong>to</strong> hello.pyc<br />
■Note If you’re running a version of <strong>Python</strong> that you didn’t install yourself, and don’t have the proper privileges,<br />
you may not be allowed <strong>to</strong> install the module as shown, because you don’t have write permissions <strong>to</strong> the right<br />
direc<strong>to</strong>ry.<br />
This is the standard mechanism used <strong>to</strong> install <strong>Python</strong> modules, packages, and extensions.<br />
All you have <strong>to</strong> do is provide the little setup script.