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.} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 10 | In earlier versions of Python (up to and including 1.5a3), scripts or |
| 11 | modules that needed to use site-specific modules would place |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 12 | \samp{import site} somewhere near the top of their code. This is no |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 13 | longer necessary. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 14 | |
Fred Drake | 24aca83 | 1999-04-23 20:33:59 +0000 | [diff] [blame] | 15 | This will append site-specific paths to the module search path. |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 16 | \indexiii{module}{search}{path} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 17 | |
| 18 | It starts by constructing up to four directories from a head and a |
| 19 | tail part. For the head part, it uses \code{sys.prefix} and |
| 20 | \code{sys.exec_prefix}; empty heads are skipped. For |
Fred Drake | b991f8d | 1998-03-08 07:09:19 +0000 | [diff] [blame] | 21 | the tail part, it uses the empty string (on Macintosh or Windows) or |
Fred Drake | d5d0435 | 2000-09-14 20:24:17 +0000 | [diff] [blame] | 22 | it uses first \file{lib/python\shortversion/site-packages} and then |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 23 | \file{lib/site-python} (on \UNIX{}). For each of the distinct |
| 24 | head-tail combinations, it sees if it refers to an existing directory, |
| 25 | and if so, adds to \code{sys.path}, and also inspected for path |
| 26 | configuration files. |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 27 | \indexii{site-python}{directory} |
Guido van Rossum | 9cf4e2b | 1997-09-08 02:02:37 +0000 | [diff] [blame] | 28 | \indexii{site-packages}{directory} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 29 | |
| 30 | A path configuration file is a file whose name has the form |
| 31 | \file{\var{package}.pth}; its contents are additional items (one |
| 32 | per line) to be added to \code{sys.path}. Non-existing items are |
| 33 | never added to \code{sys.path}, but no check is made that the item |
| 34 | refers to a directory (rather than a file). No item is added to |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 35 | \code{sys.path} more than once. Blank lines and lines beginning with |
| 36 | \code{\#} are skipped. |
| 37 | \index{package} |
Guido van Rossum | f01dff7 | 1997-09-03 22:05:54 +0000 | [diff] [blame] | 38 | \indexiii{path}{configuration}{file} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 39 | |
| 40 | For example, suppose \code{sys.prefix} and \code{sys.exec_prefix} are |
Fred Drake | b0f77d6 | 1998-02-16 20:58:58 +0000 | [diff] [blame] | 41 | set to \file{/usr/local}. The Python \version\ library is then |
Fred Drake | d5d0435 | 2000-09-14 20:24:17 +0000 | [diff] [blame] | 42 | installed in \file{/usr/local/lib/python\shortversion} (where only the |
| 43 | first three characters of \code{sys.version} are used to form the |
| 44 | installation path name). Suppose this has a subdirectory |
| 45 | \file{/usr/local/lib/python\shortversion/site-packages} with three |
Fred Drake | b0f77d6 | 1998-02-16 20:58:58 +0000 | [diff] [blame] | 46 | subsubdirectories, \file{foo}, \file{bar} and \file{spam}, and two |
| 47 | path configuration files, \file{foo.pth} and \file{bar.pth}. Assume |
| 48 | \file{foo.pth} contains the following: |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 49 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 50 | \begin{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 51 | # foo package configuration |
| 52 | |
| 53 | foo |
| 54 | bar |
| 55 | bletch |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 56 | \end{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 57 | |
| 58 | and \file{bar.pth} contains: |
| 59 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 60 | \begin{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 61 | # bar package configuration |
| 62 | |
| 63 | bar |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 64 | \end{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 65 | |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 66 | Then the following directories are added to \code{sys.path}, in this |
| 67 | order: |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 68 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 69 | \begin{verbatim} |
Guido van Rossum | 505d80b | 1998-02-19 21:02:32 +0000 | [diff] [blame] | 70 | /usr/local/lib/python1.5/site-packages/bar |
| 71 | /usr/local/lib/python1.5/site-packages/foo |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 72 | \end{verbatim} |
Guido van Rossum | 28cad96 | 1997-08-30 20:03:28 +0000 | [diff] [blame] | 73 | |
| 74 | Note that \file{bletch} is omitted because it doesn't exist; the |
| 75 | \file{bar} directory precedes the \file{foo} directory because |
| 76 | \file{bar.pth} comes alphabetically before \file{foo.pth}; and |
| 77 | \file{spam} is omitted because it is not mentioned in either path |
| 78 | configuration file. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 79 | |
| 80 | After these path manipulations, an attempt is made to import a module |
Fred Drake | b991f8d | 1998-03-08 07:09:19 +0000 | [diff] [blame] | 81 | named \module{sitecustomize}\refmodindex{sitecustomize}, which can |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 82 | perform arbitrary site-specific customizations. If this import fails |
Fred Drake | b991f8d | 1998-03-08 07:09:19 +0000 | [diff] [blame] | 83 | with an \exception{ImportError} exception, it is silently ignored. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 84 | |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 85 | Note that for some non-\UNIX{} systems, \code{sys.prefix} and |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 86 | \code{sys.exec_prefix} are empty, and the path manipulations are |
Fred Drake | cf75754 | 1998-01-13 18:34:40 +0000 | [diff] [blame] | 87 | skipped; however the import of |
Fred Drake | b991f8d | 1998-03-08 07:09:19 +0000 | [diff] [blame] | 88 | \module{sitecustomize}\refmodindex{sitecustomize} is still attempted. |