Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{site} --- |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 2 | Site-specific configuration hook} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | bbac432 | 1999-02-20 00:14:17 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{site} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{A standard way to reference site-specific modules.} |
| 6 | |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 8 | \strong{This module is automatically imported during initialization.} |
Fred Drake | ff381e1 | 2003-12-30 22:51:32 +0000 | [diff] [blame] | 9 | The automatic import can be suppressed using the interpreter's |
| 10 | \programopt{-S} option. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 11 | |
Fred Drake | ff381e1 | 2003-12-30 22:51:32 +0000 | [diff] [blame] | 12 | Importing this module will append site-specific paths to the module |
| 13 | search path. |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 14 | \indexiii{module}{search}{path} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 15 | |
| 16 | It starts by constructing up to four directories from a head and a |
| 17 | tail part. For the head part, it uses \code{sys.prefix} and |
| 18 | \code{sys.exec_prefix}; empty heads are skipped. For |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 19 | the tail part, it uses the empty string and then |
| 20 | \file{lib/site-packages} (on Windows) or |
| 21 | \file{lib/python\shortversion/site-packages} and then |
| 22 | \file{lib/site-python} (on \UNIX{} and Macintosh). For each of the |
| 23 | distinct head-tail combinations, it sees if it refers to an existing |
| 24 | directory, and if so, adds it to \code{sys.path} and also inspects |
| 25 | the newly added path for configuration files. |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 26 | \indexii{site-python}{directory} |
Guido van Rossum | 9cf4e2b | 1997-09-08 02:02:37 +0000 | [diff] [blame] | 27 | \indexii{site-packages}{directory} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 28 | |
| 29 | A path configuration file is a file whose name has the form |
Brett Cannon | 508c57d | 2004-03-20 21:41:28 +0000 | [diff] [blame] | 30 | \file{\var{package}.pth} and exists in one of the four directories |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 31 | mentioned above; its contents are additional items (one per line) to |
| 32 | be added to \code{sys.path}. Non-existing items are never added to |
| 33 | \code{sys.path}, but no check is made that the item refers to a |
| 34 | directory (rather than a file). No item is added to \code{sys.path} |
| 35 | more than once. Blank lines and lines beginning with \code{\#} are |
| 36 | skipped. Lines starting with \code{import} (followed by space or tab) |
| 37 | are executed. |
| 38 | |
| 39 | \versionchanged[A space or tab is now required after the import |
| 40 | keyword]{2.6} |
| 41 | |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 42 | \index{package} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 43 | \indexiii{path}{configuration}{file} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 44 | |
| 45 | For example, suppose \code{sys.prefix} and \code{sys.exec_prefix} are |
Fred Drake | b0f77d6 | 1998-02-16 20:58:58 +0000 | [diff] [blame] | 46 | set to \file{/usr/local}. The Python \version\ library is then |
Fred Drake | d5d0435 | 2000-09-14 20:24:17 +0000 | [diff] [blame] | 47 | installed in \file{/usr/local/lib/python\shortversion} (where only the |
| 48 | first three characters of \code{sys.version} are used to form the |
| 49 | installation path name). Suppose this has a subdirectory |
| 50 | \file{/usr/local/lib/python\shortversion/site-packages} with three |
Fred Drake | b0f77d6 | 1998-02-16 20:58:58 +0000 | [diff] [blame] | 51 | subsubdirectories, \file{foo}, \file{bar} and \file{spam}, and two |
| 52 | path configuration files, \file{foo.pth} and \file{bar.pth}. Assume |
| 53 | \file{foo.pth} contains the following: |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 54 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 55 | \begin{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 56 | # foo package configuration |
| 57 | |
| 58 | foo |
| 59 | bar |
| 60 | bletch |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 61 | \end{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 62 | |
| 63 | and \file{bar.pth} contains: |
| 64 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 65 | \begin{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 66 | # bar package configuration |
| 67 | |
| 68 | bar |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 69 | \end{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 70 | |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 71 | Then the following directories are added to \code{sys.path}, in this |
| 72 | order: |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 73 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 74 | \begin{verbatim} |
Neal Norwitz | ba902fd | 2002-02-19 02:58:54 +0000 | [diff] [blame] | 75 | /usr/local/lib/python2.3/site-packages/bar |
| 76 | /usr/local/lib/python2.3/site-packages/foo |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 77 | \end{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 78 | |
| 79 | Note that \file{bletch} is omitted because it doesn't exist; the |
| 80 | \file{bar} directory precedes the \file{foo} directory because |
| 81 | \file{bar.pth} comes alphabetically before \file{foo.pth}; and |
| 82 | \file{spam} is omitted because it is not mentioned in either path |
| 83 | configuration file. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 84 | |
| 85 | After these path manipulations, an attempt is made to import a module |
Fred Drake | b991f8d | 1998-03-08 07:09:19 +0000 | [diff] [blame] | 86 | named \module{sitecustomize}\refmodindex{sitecustomize}, which can |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 87 | perform arbitrary site-specific customizations. If this import fails |
Fred Drake | b991f8d | 1998-03-08 07:09:19 +0000 | [diff] [blame] | 88 | with an \exception{ImportError} exception, it is silently ignored. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 89 | |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 90 | Note that for some non-\UNIX{} systems, \code{sys.prefix} and |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 91 | \code{sys.exec_prefix} are empty, and the path manipulations are |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 92 | skipped; however the import of |
Fred Drake | b991f8d | 1998-03-08 07:09:19 +0000 | [diff] [blame] | 93 | \module{sitecustomize}\refmodindex{sitecustomize} is still attempted. |