12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

and then only if you must import across directories. See also Appendix A forexamples of common ways to extend your module search path with PYTHONPATHor .pth files on various platforms.This description of the module search path is accurate, but generic; the exact configurationof the search path is prone to changing across platforms and <strong>Python</strong> releases.Depending on your platform, additional directories may automatically be added tothe module search path as well.For instance, <strong>Python</strong> may add an entry for the current working directory—the directoryfrom which you launched your program—in the search path after thePYTHONPATH directories, and before the standard library entries. When launching froma command line, the current working directory may not be the same as the homedirectory of your top-level file (i.e., the directory where your program file resides). *Because the current working directory can vary each time your program runs, younormally shouldn’t depend on its value for import purposes. †The sys.path listIf you want to see how the module search path is truly configured on your machine,you can always inspect the path as <strong>Python</strong> knows it by printing the built-in sys.pathlist (that is, the path attribute of the standard library module sys). This list of directoryname strings is the actual search path within <strong>Python</strong>; on imports, <strong>Python</strong>searches each directory in this list from left to right.Really, sys.path is the module search path. <strong>Python</strong> configures it at program startup,automatically merging any PYTHONPATH and .pth file path settings you’ve made intothe list, and setting the first entry to identify the home directory of the top-level file(possibly as an empty string).<strong>Python</strong> exposes this list for two good reasons. First, it provides a way to verify thesearch path settings you’ve made—if you don’t see your settings somewhere in thislist, you need to recheck your work. Second, if you know what you’re doing, this listalso provides a way for scripts to tailor their search paths manually. As you’ll see laterin this part of the book, by modifying the sys.path list, you can modify the search pathfor all future imports. Such changes only last for the duration of the script, however;PYTHONPATH and .pth files offer more permanent ways to modify the path. ‡* See Chapter 3 for more on launching programs from command lines.† See also Chapter 21’s discussion of the new relative import syntax in <strong>Python</strong> 2.5; this modifies the searchpath for from statements when “.” characters are used (e.g., from . import string).‡ Some programs really need to change sys.path, though. Scripts that run on web servers, for example, usuallyrun as the user “nobody” to limit machine access. Because such scripts cannot usually depend on “nobody”to have set PYTHONPATH in any particular way, they often set sys.path manually to include required sourcedirectories, prior to running any import statements. A sys.path.append(dirname) will often suffice.392 | Chapter 18: Modules: The Big Picture

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!